Bundle.cs 4.7 KB

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