123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using UnityEngine;
- using System.Text;
- using CommonUI_Unity3D.Impl;
- namespace CommonUnity3D.XMUnity.LoadUtil
- {
- public class XMUnityAssetBundleLoader : XMUnityLoadIml
- {
- public enum XMUnityLoadType
- {
- MPQ,
- WWW
- }
- private static XMUnityLoadType mLoadType = XMUnityLoadType.WWW;
- private AssetBundle mAB = null;
- private string mErrorLog = null;
- private bool mHasDone = false;
- //System.Diagnostics.Stopwatch sw;
- #region 3w模式.
- private WWW m3W = null;
- private string url;
- #endregion
- #region MPQ模式.
-
- private AssetBundleCreateRequest mABRequest = null;
- #endregion
- public static void SetLoadType(XMUnityLoadType type)
- {
- mLoadType = type;
- }
- public override float GetProgress()
- {
- if(mHasDone)
- {
- return 1;
- }
- switch(mLoadType)
- {
- case XMUnityLoadType.WWW:
- return m3W.progress;
- case XMUnityLoadType.MPQ:
- if(!mLoadAsync)
- {
- if(mAB != null) { return 1; }
- else { return 0; }
- }
- else { return mABRequest.progress; }
- }
- return 0;
- }
- public override bool IsLoadFinish()
- {
- if(mHasDone) { return mHasDone; }
- switch(mLoadType)
- {
- case XMUnityLoadType.WWW:
- if(m3W.isDone)
- {
- OnLoadFinish();
- return true;
- }
- break;
- case XMUnityLoadType.MPQ:
- if(!mLoadAsync)
- {
- return mHasDone;
- }
- else
- {
- if(mABRequest != null && mABRequest.isDone)
- {
- OnLoadFinish();
- return true;
- }
- }
- break;
- }
- return false;
- }
- public override void Load(string url)
- {
- //sw = System.Diagnostics.Stopwatch.StartNew();
- this.url = url;
- StringBuilder sb = new StringBuilder();
- sb.Length = 0;
- sb.Append("mpq://");
- sb.Append(url);
-
- switch(mLoadType)
- {
- case XMUnityLoadType.WWW:
- m3W = new WWW(url);
- break;
- case XMUnityLoadType.MPQ:
- url = sb.ToString();
- if(!mLoadAsync)
- {
- mAB = UnityDriver.UnityInstance.LoadAssetBundleImmediate(url);
- mHasDone = true;
- if(mAB == null) { mErrorLog = "LoadAssetBundleImmediate Error"; }
- }
- else
- {
- mABRequest = UnityDriver.UnityInstance.LoadAssetBundle(url);
- if(mABRequest == null)
- {
- mErrorLog = "LoadAssetBundle Error: " + url;
- mHasDone = true;
- }
- }
- break;
- }
- }
- public override string GetErrorLog()
- {
- return mErrorLog;
- }
- public override bool IsLoadError()
- {
- if(!string.IsNullOrEmpty(mErrorLog))
- {
- return true;
- }
- switch(mLoadType)
- {
- case XMUnityLoadType.WWW:
- if(m3W.error != null)
- {
- mErrorLog = m3W.error.ToString() + m3W.url;
- mHasDone = true;
- }
- return !string.IsNullOrEmpty(mErrorLog);
- case XMUnityLoadType.MPQ:
- return !string.IsNullOrEmpty(mErrorLog);
- }
- return false;
- }
- public override AssetBundle GetAssetBundle()
- {
- switch(mLoadType)
- {
- case XMUnityLoadType.WWW:
- return mAB;
- case XMUnityLoadType.MPQ:
- return mAB;
- }
- return null;
- }
- public override void Dispose()
- {
- mHasDone = true;
- if(m3W != null)
- {
- m3W.Dispose();
- m3W = null;
- }
- if(mABRequest != null)
- {
- mABRequest = null;
- }
- mAB = null;
- }
- protected virtual void OnLoadFinish()
- {
- if(!string.IsNullOrEmpty(mErrorLog))
- {
- mAB = null;
- m3W.Dispose();
- }
- else
- {
- switch(mLoadType)
- {
- case XMUnityLoadType.WWW:
- mAB = m3W.assetBundle;
- m3W.Dispose();
- break;
- case XMUnityLoadType.MPQ:
- if(mLoadAsync) { mAB = mABRequest.assetBundle as AssetBundle; }
- break;
- }
- }
- //sw.Stop();
- //Debug.LogError("[yyyyyyyyyyyyyyyy] " + url.ToString() + " "+sw.ElapsedMilliseconds/1000f);
- mHasDone = true;
- }
- }
- }
|