"Unity3d" "Project learning Experience" Download resources from the resource Server (ii)

Source: Internet
Author: User

Following the foundation of the previous article to download resources from a resource server (i)

We will now continue to further complete the Loadmanager management class.

The management class should exist as a singleton because it exists in the global.

Because the Loadmanager class manages multiple loadrequest, we need to store the loadrequest that are currently being downloaded, as well as the loadrequest of the download done.

In addition, to prevent too many download threads from taking up too much resources, we have some limitations on the number of threads to download, such as the maximum number of threads in a project is 2, so we need a waiting queue for storage loadrequest.

According to the above analysis, the following are:

public static Loadmanager instance;    public int max_load_request = 2;    Private dictionary<string, loadrequest> loaddict = new dictionary<string, loadrequest> ();    Private dictionary<string, loadrequest> waitdict = new dictionary<string, loadrequest> ();    Private dictionary<string, loadparam> completedict = new dictionary<string, loadparam> ();

In addition, since there is a download queue exists, there is a download priority, we can store the priority of the download task, and then according to the priority of the download.

Of course, although the project has a download priority storage, but it is not sorted, when the download task is created, also unified use of the normal priority.

So let's start with the creation, and then sort it later when we need to use the priority.

    Private list<string> prioritylist = new list<string> ();

Next we start implementing the code:

1. First implement a single example:

    <summary>///Return instance///</summary>//    <returns></returns>    Public Static Loadmanager getinstance () {        if (instance = = null) {            instance = new Loadmanager ();        }        return instance;    }

2. We implement a function that removes a task from the wait queue to the download queue based on the priority level.

    <summary>////    based on priority, remove a task from the wait queue into the download queue///    </summary> public    Void Moverequestfromwaitdicttoloaddict () {        isloading = loaddict.count > 0;        if (Prioritylist.count > 0) {            if (Waitdict.containskey (Prioritylist[0])) {                Loadrequest request = waitdict[ Prioritylist[0]];                Waitdict.remove (Prioritylist[0]);                Prioritylist.removeat (0);                Load (Request.requesturl, Request.completefunction, Request.customparams, Request.filetype, Request.priotiry, Request.errorfunction, request.processfunction);}}}    


3. The load () function above is to load the new loadrequest.

When loading a new loadrequest, we want to make judgments, such as the same download task may be initiated by different component requests;

① This time we certainly can not download again, so just to determine whether the completedict inside has been downloaded, if it is, directly use can.

② In addition, when there is the same request, but the download is not yet complete (in the download), we just need to add the callback function into the callback queue.

③ the same, if the same request is in the waiting queue, it is treated like ②.

④ finally left is, has not downloaded the situation, at this time we want to see whether the current download thread exceeds the maximum limit, if it is, the task is added to the waiting queue, if not, you can download it now

The specific implementation is as follows:


<summary>///Read resources///</summary> public void Load (string url, loadrequest.downcompletedelegate c Ompletefunc, Object customparam = null, string fileType = "", int priority = 2, loadrequest.errordelegate errorfunc = nul L, loadrequest.processdelegate processfunc = null) {URL = URL.        Trim (); if (string.               IsNullOrEmpty (URL)) return; if (Completedict.containskey (URL)) {//downloaded resource, call callback function directly if (Customparam! = null) {com            Pletedict[url].param = Customparam;            } try {Completefunc.invoke (Completedict[url]); } catch (Exception Exception) {debug.logwarning ("Exception:" + Exception.            Message);  }} else if (Loaddict.containskey (URL)) {//has submitted the same request but no download completed loaddict[url].completefunction            + = Completefunc;            Loaddict[url].processfunction + = Processfunc;     Loaddict[url].errorfunction + = Errorfunc;       Loaddict[url].customparams.add (Customparam); } else if (Waitdict.containskey (URL)) {//has submitted the same request, but has not yet loaded loaddict[url].completefunction + = Compl            Etefunc;            Loaddict[url].processfunction + = Processfunc;            Loaddict[url].errorfunction + = Errorfunc;        Loaddict[url].customparams.add (Customparam);                } else {//not loaded if (Loaddict.count < max_load_request) {isloading = true;                Loadrequest loadrequest = new Loadrequest (URL, Customparam, FileType, Completefunc, Errorfunc, Processfunc); if (Customparam! = null && customparam.gettype (). ToString () = = "System.Collections.Generic.List ' 1[system.object]" {loadrequest.customparams = (list&lt                ;object>) Customparam;            } loaddict.add (URL, loadrequest); } else {//has reached the maximum number of loads, join the wait queue loadrequest loadrequest = new Loadrequest ();                Loadrequest.requesturl = URL;                Loadrequest.completefunction = Completefunc;                Loadrequest.errorfunction = Errorfunc;                Loadrequest.processfunction = Processfunc;                LOADREQUEST.CUSTOMPARAMS.ADD (Customparam);                Loadrequest.filetype = FileType;                Loadrequest.priotiry = priority;                Waitdict.add (URL, loadrequest);                Prioritylist.add (URL); Prioritylist = Prioritylist.orderby (s = = Waitdict[s].priotiry).            ToList (); }        }    }






















"Unity3d" "Project learning Experience" Download resources from the resource Server (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.