XMUnityABLoadAdapter.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace CommonUnity3D.XMUnity.LoadUtil
  4. {
  5. /// <summary>
  6. /// AssetBundle加载适配器.
  7. /// </summary>
  8. public class XMUnityABLoadAdapter
  9. {
  10. private static string mLoadRootPath = null;
  11. public delegate void XMUnityLoadAdapterCallBack(XMUnityAssetBundle ab);
  12. public delegate void LoadFinishCallBack(XMUnityABLoadAdapter adapter);
  13. private LoadFinishCallBack OnFinshCallBack = null;
  14. private XMUnityLoadAdapterCallBack OnLoadCallBack = null;
  15. private XMUnityLoadIml mIml;
  16. private string mErrorLog = null;
  17. private bool mHasDone = false;
  18. public bool HasDone { get { return mHasDone; } }
  19. private bool mIsDispose = false;
  20. private string mURL = null;
  21. protected bool mLoadAsync = true;
  22. public bool LoadAsync { set { mLoadAsync = value; } get { return mLoadAsync; } }
  23. XMUnityAssetBundle mXMUnityAssetBundle = null;
  24. private static int mVersionCount = 0;
  25. public static void SetLoadRootPath(string path)
  26. {
  27. mLoadRootPath = path;
  28. }
  29. public XMUnityABLoadAdapter()
  30. {
  31. }
  32. public virtual void Load(string url, XMUnityLoadAdapterCallBack callBack)
  33. {
  34. if (string.IsNullOrEmpty(url))
  35. {
  36. Debug.Log("XMUnityLoadAdapter Invalid args: " + url);
  37. }
  38. if (callBack != null)
  39. {
  40. OnLoadCallBack += callBack;
  41. }
  42. mURL = url;
  43. string loadPath = mLoadRootPath + url;
  44. StartLoad(loadPath);
  45. }
  46. public string GetURL()
  47. {
  48. return mURL;
  49. }
  50. public virtual void SetFinishCallBack(LoadFinishCallBack callBack)
  51. {
  52. OnFinshCallBack = callBack;
  53. }
  54. public virtual void AddCallBack(XMUnityLoadAdapterCallBack callBack)
  55. {
  56. OnLoadCallBack += callBack;
  57. }
  58. public virtual void RemoveCallBack(XMUnityLoadAdapterCallBack callBack)
  59. {
  60. OnLoadCallBack -= callBack;
  61. }
  62. protected virtual void StartLoad(string loadPath)
  63. {
  64. mIml = CreateLoadAdapter();
  65. mIml.LoadAsync = mLoadAsync;
  66. mIml.Load(loadPath);
  67. }
  68. public bool OnAdapterUpdate()
  69. {
  70. if (mHasDone)
  71. {
  72. return true;
  73. }
  74. return OnUpdateLoad();
  75. }
  76. protected virtual bool OnUpdateLoad()
  77. {
  78. if (IsLoadFinish())
  79. {
  80. OnLoadFinish();
  81. return true;
  82. }
  83. return false;
  84. }
  85. public virtual void OnStartLoad()
  86. {
  87. }
  88. public virtual void OnStopLoad()
  89. {
  90. }
  91. public virtual void OnLoadFinish()
  92. {
  93. if (OnFinshCallBack != null)
  94. {
  95. OnFinshCallBack.Invoke(this);
  96. OnFinshCallBack = null;
  97. }
  98. mHasDone = true;
  99. if (mIml != null)
  100. {
  101. mIml.Dispose();
  102. }
  103. }
  104. public XMUnityAssetBundle GetXMUnityAssetBundle()
  105. {
  106. if (mXMUnityAssetBundle == null)
  107. {
  108. AssetBundle ab = mIml.GetAssetBundle();
  109. if(ab != null)
  110. {
  111. mXMUnityAssetBundle = new XMUnityAssetBundle(mURL, ab, mVersionCount++);
  112. }
  113. }
  114. return mXMUnityAssetBundle;
  115. }
  116. public virtual void DispatchCallBack()
  117. {
  118. if (mIml == null)
  119. {
  120. return;
  121. }
  122. if (OnLoadCallBack != null)
  123. {
  124. var mfab = GetXMUnityAssetBundle();
  125. OnLoadCallBack(mfab);
  126. }
  127. }
  128. public virtual float GetProgress()
  129. {
  130. if (mHasDone)
  131. {
  132. return 1;
  133. }
  134. if (mIml != null)
  135. {
  136. return mIml.GetProgress();
  137. }
  138. return 0;
  139. }
  140. public virtual void Dispose()
  141. {
  142. if (OnFinshCallBack != null)
  143. {
  144. Debug.Log("XMUnityABLoadAdapter dispose before finish!");
  145. }
  146. if (mIml != null)
  147. {
  148. mIml.Dispose();
  149. mIml = null;
  150. }
  151. mIsDispose = true;
  152. OnLoadCallBack = null;
  153. }
  154. public virtual AssetBundle GetAssetBundle()
  155. {
  156. if (mIml != null)
  157. {
  158. return mIml.GetAssetBundle();
  159. }
  160. return null;
  161. }
  162. protected bool IsLoadFinish()
  163. {
  164. if (mIsDispose)
  165. {
  166. return true;
  167. }
  168. if (mIml == null)
  169. {
  170. return false;
  171. }
  172. if (mIml.IsLoadError())
  173. {
  174. OnLoadError(mIml.GetErrorLog());
  175. }
  176. if (mIml.IsLoadFinish())
  177. {
  178. return true;
  179. }
  180. return false;
  181. }
  182. protected virtual void OnLoadError(string error)
  183. {
  184. mErrorLog = error;
  185. Debug.Log("XMUnityABLoadAdapter OnLoadError: " + mErrorLog);
  186. }
  187. private static XMUnityLoadIml CreateLoadAdapter()
  188. {
  189. //工厂类,后续有需求,再新增setFactory.
  190. return XMUnityLoadImlFactory.GetInstance().CreateLoadIml();
  191. }
  192. }
  193. }