Bundle.cs 3.9 KB

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