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:
Using unityengine;using system.collections;namespace assemblycsharp {public class Loadfiletype {public const String image = "image"; Unity3d file Format public const string unity3d = "Unity3d"; Module Resource Packaging Format public const string module_resource = "Moduleresource"; Public const string binary = "binary"; Public const STRING txt = "txt"; Public Const string JSON = "json"; FBX packaged assetbundle format file public const string FBX = "FBX"; Public Const string audio = "Audio"; Font file public const string font = "Font"; Binary file (for background update) public const string BINARY_BG = "BINARY_BG"; }}
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
/** * Download Task * Create by Chensh 2014.10.27 10:31 */using unityengine;using system.collections;using System.Collections.Gener Ic;namespace Assemblycsharp {public class Loadrequest {public delegate void Downcompletedelegate (Loadparam par AM); public delegate void Errordelegate (Loadrequest request); public delegate void Processdelegate (float processvalue, int filetotalsize = 0); Public Downcompletedelegate completefunction; Public Errordelegate errorfunction; Public Processdelegate processfunction; public const int time_out_frames = 300; private int _loadtotalframes = 0; Total number of frames loaded public bool istimeout = FALSE; public bool Alreadydeal = FALSE; public string Requesturl; public string FileType; Public WWW wwwobject = null; Public list<object> customparams = new list<object> (); public int priotiry = Loadpriority.normal; Public loadrequest (string url, Object cuStomparam = NULL, String type = "", downcompletedelegate Completefunc = null, errordelegate Errorfunc = null, Processdeleg ate processfunc = null) {Requesturl = URL; FileType = type; Completefunction = Completefunc; if (completefunc! = null) Customparams.add (Customparam); if (errorfunc! = null) errorfunction = Errorfunc; if (processfunc! = null) processfunction = Processfunc; Wwwobject = new WWW (Requesturl); wwwobject.threadpriority = Threadpriority.normal; } public int Loadtotalframes {get {return _loadtotalframes; } set {_loadtotalframes = value; if (_loadtotalframes > Loadrequest.time_out_frames) istimeout = true; } } } }
"Unity3d" "Project learning Experience" Download resources from the resource server (i)