Bundle.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using System.Collections;
  4. using System.Diagnostics;
  5. using Debug = UnityEngine.Debug;
  6. public class Bundle : MonoBehaviour
  7. {
  8. #region
  9. public static bool LoadComplete;
  10. public static Bundle Instance;
  11. public static AssetBundle UI;
  12. public static AssetBundle Skill;
  13. public static AssetBundle Config;
  14. public static AssetBundle Object;
  15. public static AssetBundle Shader;
  16. public static AssetBundle Garden;
  17. public static AssetBundle Character;
  18. public static AssetBundle TempUI;
  19. public static AssetBundle TempSkill;
  20. public static AssetBundle TempConfig;
  21. public static AssetBundle TempObject;
  22. public static AssetBundle TempShader;
  23. public static AssetBundle TempGarden;
  24. public static AssetBundle TempCharacter;
  25. #endregion
  26. public void Awake()
  27. {
  28. Instance = this;
  29. DontDestroyOnLoad(gameObject);
  30. LoadAll
  31. (
  32. () => LoadComplete = true
  33. );
  34. }
  35. public static string GetStreamPath()
  36. {
  37. if (Application.isEditor)
  38. // return "file://" + System.Environment.CurrentDirectory.Replace("\\", "/"); // Use the build output folder directly.
  39. return "file://" + Application.streamingAssetsPath + "/Bundle/Windows/";//user local stream
  40. else if (Application.isWebPlayer)
  41. return System.IO.Path.GetDirectoryName(Application.absoluteURL).Replace("\\", "/") + "/StreamingAssets";
  42. else if (Application.platform == RuntimePlatform.IPhonePlayer)
  43. return "file://" + Application.streamingAssetsPath + "/Bundle/IOS/";
  44. else if (Application.isMobilePlatform || Application.isConsolePlatform)
  45. return Application.streamingAssetsPath + "/Bundle/Android/";
  46. else // For standalone player.
  47. return "file://" + Application.streamingAssetsPath;
  48. //return "file://" + Application.streamingAssetsPath + "/Bundle/Windows/";
  49. }
  50. public void LoadAll(UnityAction callback)
  51. {
  52. StartCoroutine(ILoadAll(callback));
  53. }
  54. public static IEnumerator ILoadAll(UnityAction callback)
  55. {
  56. yield return LoadUI();
  57. yield return LoadSkill();
  58. yield return LoadConfig();
  59. yield return LoadObject();
  60. yield return LoadShader();
  61. yield return LoadGarden();
  62. yield return LoadCharacter();
  63. if (callback != null)
  64. {
  65. callback.Invoke();
  66. }
  67. }
  68. public static IEnumerator LoadUI()
  69. {
  70. WWW www = new WWW(GetStreamPath() + "ui");
  71. yield return www;
  72. if (string.IsNullOrEmpty(www.error))
  73. {
  74. UI = www.assetBundle;
  75. }
  76. else
  77. {
  78. Debug.Log(www.error);
  79. }
  80. www = new WWW(GetStreamPath() + "uitemp");
  81. yield return www;
  82. if (string.IsNullOrEmpty(www.error))
  83. {
  84. TempUI = www.assetBundle;
  85. }
  86. }
  87. public static IEnumerator LoadSkill()
  88. {
  89. WWW www = new WWW(GetStreamPath() + "skill");
  90. yield return www;
  91. if (string.IsNullOrEmpty(www.error))
  92. {
  93. Skill = www.assetBundle;
  94. }
  95. else
  96. {
  97. Debug.Log(www.error);
  98. }
  99. www = new WWW(GetStreamPath() + "skilltemp");
  100. yield return www;
  101. if (string.IsNullOrEmpty(www.error))
  102. {
  103. TempSkill = www.assetBundle;
  104. }
  105. }
  106. public static IEnumerator LoadConfig()
  107. {
  108. WWW www = new WWW(GetStreamPath() + "config");
  109. yield return www;
  110. if (string.IsNullOrEmpty(www.error))
  111. {
  112. Config = www.assetBundle;
  113. }
  114. else
  115. {
  116. Debug.Log(www.error);
  117. }
  118. www = new WWW(GetStreamPath() + "configtemp");
  119. yield return www;
  120. if (string.IsNullOrEmpty(www.error))
  121. {
  122. TempConfig = www.assetBundle;
  123. }
  124. }
  125. public static IEnumerator LoadObject()
  126. {
  127. WWW www = new WWW(GetStreamPath() + "object");
  128. yield return www;
  129. if (string.IsNullOrEmpty(www.error))
  130. {
  131. Object = www.assetBundle;
  132. }
  133. else
  134. {
  135. Debug.Log(www.error);
  136. }
  137. www = new WWW(GetStreamPath() + "objecttemp");
  138. yield return www;
  139. if (string.IsNullOrEmpty(www.error))
  140. {
  141. TempObject = www.assetBundle;
  142. }
  143. }
  144. public static IEnumerator LoadShader()
  145. {
  146. WWW www = new WWW(GetStreamPath() + "shader");
  147. yield return www;
  148. if (string.IsNullOrEmpty(www.error))
  149. {
  150. Shader = www.assetBundle;
  151. }
  152. else
  153. {
  154. Debug.Log(www.error);
  155. }
  156. www = new WWW(GetStreamPath() + "shadertemp");
  157. yield return www;
  158. if (string.IsNullOrEmpty(www.error))
  159. {
  160. TempShader = www.assetBundle;
  161. }
  162. }
  163. public static IEnumerator LoadGarden()
  164. {
  165. WWW www = new WWW(GetStreamPath() + "garden");
  166. yield return www;
  167. if (string.IsNullOrEmpty(www.error))
  168. {
  169. Garden = www.assetBundle;
  170. }
  171. else
  172. {
  173. Debug.Log(www.error);
  174. }
  175. www = new WWW(GetStreamPath() + "gardentemp");
  176. yield return www;
  177. if (string.IsNullOrEmpty(www.error))
  178. {
  179. TempGarden = www.assetBundle;
  180. }
  181. }
  182. public static IEnumerator LoadCharacter()
  183. {
  184. WWW www = new WWW(GetStreamPath() + "character");
  185. yield return www;
  186. if (string.IsNullOrEmpty(www.error))
  187. {
  188. Character = www.assetBundle;
  189. }
  190. else
  191. {
  192. Debug.Log(www.error);
  193. }
  194. www = new WWW(GetStreamPath() + "charactertemp");
  195. yield return www;
  196. if (string.IsNullOrEmpty(www.error))
  197. {
  198. TempCharacter = www.assetBundle;
  199. }
  200. }
  201. }