Unity uses WWW class to synchronously load resources and unity to load Resources
Unity can Load Resources in two ways: 1. Resources. Load (). 2. AssetBundle
The former is synchronous loading, while the latter is generally considered asynchronous loading. Unity officially only provides asynchronous loading. In fact, it can also be synchronized. (It's annoying to press Ctrl + S)
Asynchronous loading mode:
IEnumerator LoadAssets (string path)
{
WWW www = new WWW (path );
Yield return www;
}
Void StartLoad ()
{
StartCoroutine (LoadAssets (path ));
}
StartCoroutine is called coroutine in the help of unity3d, which means to start an auxiliary thread. In C #, The coroutine must be defined as the IEnumerator (Implementation Interface) type. It declares that the class implementing this interface can be used as an iterator. So it is asynchronous loading.
However, when Resources (synchronous) are used to load Resources in the early stage of the development project and incremental updates are required in the later stage, the WWW class must be used for loading (I heard there are other non-WWW loading methods, I hope you can share it with me). Changing the synchronous mode to asynchronous mode is not something that can be changed. Many logics need to be processed again. In fact, using the WWW class for loading is not necessarily asynchronous. It can also be processed as synchronous loading (also makes me Tangle for a morning, I suddenly thought I could use IEnumerable to browse the Web page, but I did not expect it to succeed, haha (I am still too lazy and don't want to change the code in a large area). In fact, I can understand it at a Glance. The following code is directly used:
Using UnityEngine;
Using System. Collections;
Using System. Collections. Generic;
Public class LoadAsset
{
Private string m_sPath = string. Empty; // resource path
Private WWW m_www = null;
Private int m_iCountRetry = 5; // number of repeated downloads
Public LoadAsset (string path)
{
M_sPath = path;
}
Public UnityEngine. Object StarLoad ()
{
UnityEngine. Object result = null;
While (result = null)
{
Foreach (UnityEngine. Object obj in LoadWWW ())
{
Result = obj;
}
If (m_iCountRetry> 0)
{
If (result! = Null)
{
Break;
}
M_iCountRetry --;
}
Else
{
Break;
}
}
DeInit ();
Return result;
}
Public IEnumerable <UnityEngine. Object> LoadWWW ()
{
M_www = new WWW (m_sPath );
Yield return m_www.assetBundle.mainAsset;
}
Private void DeInit ()
{
If (m_www! = Null)
{
M_www.Dispose ();
}
M_www = null;
}
}
Public class ResLoad
{
Private static ResLoad instance = null;
Public static ResLoad Instance
{
Get
{
If (instance = null)
{
Instance = new ResLoad ();
}
Return instance;
}
}
Public UnityEngine. Object StarLoad (string path)
{
UnityEngine. Object obj = null;
LoadAsset asset = new LoadAsset (path );
Obj = asset. StarLoad ();
Return obj;
}
Public T StarLoad <T> (string path)
{
LoadAsset asset = new LoadAsset (path );
Object obj = asset. StarLoad () as object;
Return (T) obj );
}
}
The above is the source code of my development project. copy can be used directly! Isn't it easy? If you haven't mentioned anything, I hope you can add more things!
Reprint please note output: http://blog.csdn.net/sf575909352/article/details/44573305