Bundle.cs 5.8 KB

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