XMUnityAssetBundleLoader.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using UnityEngine;
  2. using System.Text;
  3. using CommonUI_Unity3D.Impl;
  4. namespace CommonUnity3D.XMUnity.LoadUtil
  5. {
  6. public class XMUnityAssetBundleLoader : XMUnityLoadIml
  7. {
  8. public enum XMUnityLoadType
  9. {
  10. MPQ,
  11. WWW
  12. }
  13. private static XMUnityLoadType mLoadType = XMUnityLoadType.WWW;
  14. private AssetBundle mAB = null;
  15. private string mErrorLog = null;
  16. private bool mHasDone = false;
  17. //System.Diagnostics.Stopwatch sw;
  18. #region 3w模式.
  19. private WWW m3W = null;
  20. private string url;
  21. #endregion
  22. #region MPQ模式.
  23. private AssetBundleCreateRequest mABRequest = null;
  24. #endregion
  25. public static void SetLoadType(XMUnityLoadType type)
  26. {
  27. mLoadType = type;
  28. }
  29. public override float GetProgress()
  30. {
  31. if(mHasDone)
  32. {
  33. return 1;
  34. }
  35. switch(mLoadType)
  36. {
  37. case XMUnityLoadType.WWW:
  38. return m3W.progress;
  39. case XMUnityLoadType.MPQ:
  40. if(!mLoadAsync)
  41. {
  42. if(mAB != null) { return 1; }
  43. else { return 0; }
  44. }
  45. else { return mABRequest.progress; }
  46. }
  47. return 0;
  48. }
  49. public override bool IsLoadFinish()
  50. {
  51. if(mHasDone) { return mHasDone; }
  52. switch(mLoadType)
  53. {
  54. case XMUnityLoadType.WWW:
  55. if(m3W.isDone)
  56. {
  57. OnLoadFinish();
  58. return true;
  59. }
  60. break;
  61. case XMUnityLoadType.MPQ:
  62. if(!mLoadAsync)
  63. {
  64. return mHasDone;
  65. }
  66. else
  67. {
  68. if(mABRequest != null && mABRequest.isDone)
  69. {
  70. OnLoadFinish();
  71. return true;
  72. }
  73. }
  74. break;
  75. }
  76. return false;
  77. }
  78. public override void Load(string url)
  79. {
  80. //sw = System.Diagnostics.Stopwatch.StartNew();
  81. this.url = url;
  82. StringBuilder sb = new StringBuilder();
  83. sb.Length = 0;
  84. sb.Append("mpq://");
  85. sb.Append(url);
  86. switch(mLoadType)
  87. {
  88. case XMUnityLoadType.WWW:
  89. m3W = new WWW(url);
  90. break;
  91. case XMUnityLoadType.MPQ:
  92. url = sb.ToString();
  93. if(!mLoadAsync)
  94. {
  95. mAB = UnityDriver.UnityInstance.LoadAssetBundleImmediate(url);
  96. mHasDone = true;
  97. if(mAB == null) { mErrorLog = "LoadAssetBundleImmediate Error"; }
  98. }
  99. else
  100. {
  101. mABRequest = UnityDriver.UnityInstance.LoadAssetBundle(url);
  102. if(mABRequest == null)
  103. {
  104. mErrorLog = "LoadAssetBundle Error: " + url;
  105. mHasDone = true;
  106. }
  107. }
  108. break;
  109. }
  110. }
  111. public override string GetErrorLog()
  112. {
  113. return mErrorLog;
  114. }
  115. public override bool IsLoadError()
  116. {
  117. if(!string.IsNullOrEmpty(mErrorLog))
  118. {
  119. return true;
  120. }
  121. switch(mLoadType)
  122. {
  123. case XMUnityLoadType.WWW:
  124. if(m3W.error != null)
  125. {
  126. mErrorLog = m3W.error.ToString() + m3W.url;
  127. mHasDone = true;
  128. }
  129. return !string.IsNullOrEmpty(mErrorLog);
  130. case XMUnityLoadType.MPQ:
  131. return !string.IsNullOrEmpty(mErrorLog);
  132. }
  133. return false;
  134. }
  135. public override AssetBundle GetAssetBundle()
  136. {
  137. switch(mLoadType)
  138. {
  139. case XMUnityLoadType.WWW:
  140. return mAB;
  141. case XMUnityLoadType.MPQ:
  142. return mAB;
  143. }
  144. return null;
  145. }
  146. public override void Dispose()
  147. {
  148. mHasDone = true;
  149. if(m3W != null)
  150. {
  151. m3W.Dispose();
  152. m3W = null;
  153. }
  154. if(mABRequest != null)
  155. {
  156. mABRequest = null;
  157. }
  158. mAB = null;
  159. }
  160. protected virtual void OnLoadFinish()
  161. {
  162. if(!string.IsNullOrEmpty(mErrorLog))
  163. {
  164. mAB = null;
  165. m3W.Dispose();
  166. }
  167. else
  168. {
  169. switch(mLoadType)
  170. {
  171. case XMUnityLoadType.WWW:
  172. mAB = m3W.assetBundle;
  173. m3W.Dispose();
  174. break;
  175. case XMUnityLoadType.MPQ:
  176. if(mLoadAsync) { mAB = mABRequest.assetBundle as AssetBundle; }
  177. break;
  178. }
  179. }
  180. //sw.Stop();
  181. //Debug.LogError("[yyyyyyyyyyyyyyyy] " + url.ToString() + " "+sw.ElapsedMilliseconds/1000f);
  182. mHasDone = true;
  183. }
  184. }
  185. }