Bundle.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Atlas;
  14. public static AssetBundle Config;
  15. public static AssetBundle Object;
  16. public static AssetBundle Shader;
  17. public static AssetBundle Garden;
  18. public static AssetBundle Character;
  19. #endregion
  20. public void Awake()
  21. {
  22. Instance = this;
  23. DontDestroyOnLoad(gameObject);
  24. LoadAll
  25. (
  26. () => LoadComplete = true
  27. );
  28. }
  29. public static string GetStreamPath()
  30. {
  31. if (Application.isEditor)
  32. // return "file://" + System.Environment.CurrentDirectory.Replace("\\", "/"); // Use the build output folder directly.
  33. return "file://" + Application.streamingAssetsPath + "/Bundle/Windows/";//user local stream
  34. else if (Application.isWebPlayer)
  35. return System.IO.Path.GetDirectoryName(Application.absoluteURL).Replace("\\", "/") + "/StreamingAssets";
  36. else if (Application.platform == RuntimePlatform.IPhonePlayer)
  37. return "file://" + Application.streamingAssetsPath + "/Bundle/IOS/";
  38. else if (Application.isMobilePlatform || Application.isConsolePlatform)
  39. return Application.streamingAssetsPath + "/Bundle/Android/";
  40. else // For standalone player.
  41. return "file://" + Application.streamingAssetsPath;
  42. //return "file://" + Application.streamingAssetsPath + "/Bundle/Windows/";
  43. }
  44. public void LoadAll(UnityAction callback)
  45. {
  46. StartCoroutine(ILoadAll(callback));
  47. }
  48. public static IEnumerator ILoadAll(UnityAction callback)
  49. {
  50. yield return LoadUI();
  51. yield return LoadSkill();
  52. yield return LoadAtlas();
  53. yield return LoadConfig();
  54. yield return LoadObject();
  55. yield return LoadShader();
  56. yield return LoadGarden();
  57. yield return LoadCharacter();
  58. if (callback != null)
  59. {
  60. callback.Invoke();
  61. }
  62. }
  63. public static IEnumerator LoadUI()
  64. {
  65. WWW www = new WWW(GetStreamPath() + "ui");
  66. yield return www;
  67. if (string.IsNullOrEmpty(www.error))
  68. {
  69. UI = www.assetBundle;
  70. }
  71. else
  72. {
  73. Debug.Log(www.error);
  74. }
  75. }
  76. public static IEnumerator LoadSkill()
  77. {
  78. WWW www = new WWW(GetStreamPath() + "skill");
  79. yield return www;
  80. if (string.IsNullOrEmpty(www.error))
  81. {
  82. Skill = www.assetBundle;
  83. }
  84. else
  85. {
  86. Debug.Log(www.error);
  87. }
  88. }
  89. public static IEnumerator LoadAtlas()
  90. {
  91. WWW www = new WWW(GetStreamPath() + "atlas");
  92. yield return www;
  93. if (string.IsNullOrEmpty(www.error))
  94. {
  95. Atlas = www.assetBundle;
  96. }
  97. else
  98. {
  99. Debug.Log(www.error);
  100. }
  101. }
  102. public static IEnumerator LoadConfig()
  103. {
  104. WWW www = new WWW(GetStreamPath() + "config");
  105. yield return www;
  106. if (string.IsNullOrEmpty(www.error))
  107. {
  108. Config = www.assetBundle;
  109. }
  110. else
  111. {
  112. Debug.Log(www.error);
  113. }
  114. }
  115. public static IEnumerator LoadObject()
  116. {
  117. WWW www = new WWW(GetStreamPath() + "object");
  118. yield return www;
  119. if (string.IsNullOrEmpty(www.error))
  120. {
  121. Object = www.assetBundle;
  122. }
  123. else
  124. {
  125. Debug.Log(www.error);
  126. }
  127. }
  128. public static IEnumerator LoadShader()
  129. {
  130. WWW www = new WWW(GetStreamPath() + "shader");
  131. yield return www;
  132. if (string.IsNullOrEmpty(www.error))
  133. {
  134. Shader = www.assetBundle;
  135. }
  136. else
  137. {
  138. Debug.Log(www.error);
  139. }
  140. }
  141. public static IEnumerator LoadGarden()
  142. {
  143. WWW www = new WWW(GetStreamPath() + "garden");
  144. yield return www;
  145. if (string.IsNullOrEmpty(www.error))
  146. {
  147. Garden = www.assetBundle;
  148. }
  149. else
  150. {
  151. Debug.Log(www.error);
  152. }
  153. }
  154. public static IEnumerator LoadCharacter()
  155. {
  156. WWW www = new WWW(GetStreamPath() + "character");
  157. yield return www;
  158. if (string.IsNullOrEmpty(www.error))
  159. {
  160. Character = www.assetBundle;
  161. }
  162. else
  163. {
  164. Debug.Log(www.error);
  165. }
  166. }
  167. }