Many of the resources in the project are loaded from the resource server, which can reduce the client's package size.
So we need a dedicated class to manage the download resources.
There are many types of resources, such as JSON table, TXT file, image file, binary file, Uiatlas Atlas, Assetbundle and so on.
So, first create a class Loadfiletype that manages the resource file type. The file type can be represented by an enumeration or by a class member represented.
The class member constants are used here:
1 usingUnityengine;2 usingSystem.Collections;3 4 5 namespaceAssemblycsharp {6 Public classLoadfiletype {7 8 Public Const stringIMAGE ="Image";9 //unity3d file FormatTen Public Const stringUnity3d ="Unity3d"; One //Module Resource Packaging format A Public Const stringModule_resource ="Moduleresource"; - - Public Const stringBINARY ="binary"; the - Public Const stringTXT ="txt"; - - Public Const stringJSON ="JSON"; + //FBX packaged assetbundle format file - Public Const stringFBX ="FBX"; + A Public Const stringAUDIO ="Audio"; at //Font Files - Public Const stringFONT ="Font"; - //Binary files (for background updates) - Public Const stringBINARY_BG ="BINARY_BG"; - - } in}
Next you need to create a class to manage individual download tasks, Unity3d downloads are downloaded using WWW, and the classes we're creating need to have the following features:
① use www to download resources.
② has a delegate callback interface to facilitate the invocation of this class of objects to receive feedback, the initial callback needs: After the completion of the download callback, error callback, the download process callback.
The ③ timeout setting is determined to fail the download task over a certain period of time.
④ In addition to this, you need to record the URL of this download task, as well as the filetype of the downloaded resources.
According to the above conditions, this class is roughly:
LoadReques.cs
1 /**2 * Download Task3 * Create by Chensh 2014.10.27 10:314 */5 6 usingUnityengine;7 usingSystem.Collections;8 usingSystem.Collections.Generic;9 Ten namespaceAssemblycsharp { One Public classLoadrequest { A - Public Delegate voiddowncompletedelegate (loadparam param); - Public Delegate voiderrordelegate (loadrequest request); the Public Delegate voidProcessdelegate (floatProcessvalue,intFiletotalsize =0); - - - Publicdowncompletedelegate completefunction; + Publicerrordelegate errorfunction; - Publicprocessdelegate processfunction; + A at Public Const intTime_out_frames = -; - Private int_loadtotalframes =0;//the total number of frames loaded - Public BOOLIstimeout =false; - Public BOOLAlreadydeal =false; - - Public stringRequesturl; in Public stringFileType; - PublicWWW Wwwobject =NULL; to Publiclist<Object> customparams =Newlist<Object>(); + Public intPriotiry =Loadpriority.normal; - the * PublicLoadrequest (stringUrlObjectCustomparam =NULL,stringType ="", Downcompletedelegate Completefunc =NULL, Errordelegate Errorfunc =NULL, Processdelegate Processfunc =NULL) { $Requesturl =URL;Panax NotoginsengFileType =type; - theCompletefunction =Completefunc; + if(Completefunc! =NULL) A Customparams.add (customparam); the if(Errorfunc! =NULL) +Errorfunction =Errorfunc; - if(Processfunc! =NULL) $Processfunction =Processfunc; $ -Wwwobject =NewWWW (requesturl); -Wwwobject.threadpriority =Threadpriority.normal; the } - Wuyi Public intLoadtotalframes { the Get { - return_loadtotalframes; Wu } - Set { About_loadtotalframes =value; $ if(_loadtotalframes >loadrequest.time_out_frames) -Istimeout =true; - } - } A } + the}
Unity3d downloading resources from a resource server (i)