Bundle.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. using System.Collections;
  5. using System.Diagnostics;
  6. using Debug = UnityEngine.Debug;
  7. public class Bundle : Regist
  8. {
  9. #region
  10. public static bool LoadComplete;
  11. public static Bundle Instance;
  12. public static AssetBundle UI;
  13. public static AssetBundle Atlas;
  14. public static AssetBundle Scene;
  15. public static AssetBundle Config;
  16. public static AssetBundle Shader;
  17. #endregion
  18. public override void RegistImmed()
  19. {
  20. if (RegistFlag)
  21. {
  22. return;
  23. }
  24. else
  25. {
  26. RegistFlag = true;
  27. }
  28. enabled = true;
  29. Instance = this;
  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. if (!Logo.LogoComplete)
  58. {
  59. ManaReso.AddAsyncLoad<ObjRoot>("Canvas", Folder.UI, ObjType.Canvas);
  60. ManaReso.AddAsyncLoad<ObjRoot>("EventSystem", Folder.UI, ObjType.EventSystem);
  61. ManaReso.AddAsyncLoad<ObjRoot>("MainCamera", Folder.UI, ObjType.MainCamera);
  62. }
  63. yield return LoadScene();
  64. if (!Logo.LogoComplete)
  65. {
  66. ManaReso.AddAsyncLoad<Flower>("Flower", Folder.Scene, ObjType.Flower);
  67. ManaReso.AddAsyncLoad<ObjRoot>("Page", Folder.Scene, ObjType.Page);
  68. ManaReso.AddAsyncLoad<ObjRoot>("Garden", Folder.Scene, ObjType.Garden);
  69. ManaReso.AddAsyncLoad<ObjRoot>("MusicMini", Folder.Scene, ObjType.MusicMini);
  70. ManaReso.AddAsyncLoad<ObjRoot>("MusicTheme", Folder.Scene, ObjType.MusicTheme);
  71. }
  72. yield return LoadAtlas();
  73. yield return LoadConfig();
  74. yield return LoadShader();
  75. if (callback != null)
  76. {
  77. callback.Invoke();
  78. }
  79. }
  80. public static IEnumerator LoadUI()
  81. {
  82. WWW www = new WWW(GetStreamPath() + "ui");
  83. yield return www;
  84. if (string.IsNullOrEmpty(www.error))
  85. {
  86. UI = www.assetBundle;
  87. }
  88. else
  89. {
  90. Debug.Log(www.error);
  91. }
  92. }
  93. public static IEnumerator LoadAtlas()
  94. {
  95. WWW www = new WWW(GetStreamPath() + "atlas");
  96. yield return www;
  97. if (string.IsNullOrEmpty(www.error))
  98. {
  99. Atlas = www.assetBundle;
  100. }
  101. else
  102. {
  103. Debug.Log(www.error);
  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. }
  119. public static IEnumerator LoadShader()
  120. {
  121. WWW www = new WWW(GetStreamPath() + "shader");
  122. yield return www;
  123. if (string.IsNullOrEmpty(www.error))
  124. {
  125. Shader = www.assetBundle;
  126. }
  127. else
  128. {
  129. Debug.Log(www.error);
  130. }
  131. }
  132. public static IEnumerator LoadScene()
  133. {
  134. WWW www = new WWW(GetStreamPath() + "scene");
  135. yield return www;
  136. if (string.IsNullOrEmpty(www.error))
  137. {
  138. Scene = www.assetBundle;
  139. }
  140. else
  141. {
  142. Debug.Log(www.error);
  143. }
  144. }
  145. }