GameInit.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using Blue;
  2. using LitJson;
  3. using System.Collections;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. using UnityEngine.UI;
  9. public class GameInit : MonoBehaviour
  10. {
  11. public Text text;
  12. public Text text2;
  13. public Slider slider;
  14. string HotUpdateAllAssets = "HotUpdateAllAssets";
  15. void Start()
  16. {
  17. ArchitectureInitiator.Initiate();
  18. text.text = "正在下载主要资源";
  19. this.transform.SetParent(OpenXRCamera.Instance.head.transform);
  20. this.transform.localPosition = Vector3.zero;
  21. this.transform.localEulerAngles = Vector3.zero;
  22. JsonData data = new JsonData();
  23. string scUrl = "";
  24. try
  25. {
  26. data = JsonMapper.ToObject(HttpSDKAction.Instance.jsonData);
  27. scUrl = data[HotUpdateAllAssets].ToString();
  28. }
  29. catch
  30. {
  31. }
  32. if (scUrl != "")
  33. {
  34. if (!bool.Parse(data["isHotUpdate"].ToString()))
  35. {
  36. StartCoroutine(DownLoadAssets(false));
  37. }
  38. else if (PlayerPrefs.GetString("HotUpdateVersion") == data["HotUpdateVersion"].ToString())
  39. {
  40. StartCoroutine(DownLocalLoadAssets());
  41. }
  42. else
  43. {
  44. StartCoroutine(DownLoadAssets());
  45. }
  46. }
  47. else
  48. {
  49. StartCoroutine(DownLoadAssets(false));
  50. }
  51. // TestDownloadObject("");
  52. }
  53. public async void TestDownloadObject(string downloadPath)
  54. {
  55. Task<int> t = Task.Run(() =>
  56. {
  57. for (int i = 0; i < 999; i++)
  58. {
  59. Debug.Log("async_"+i);
  60. }
  61. return 9999999;
  62. });
  63. await t;
  64. }
  65. public static AssetBundle ablist;
  66. IEnumerator DownLocalLoadAssets()
  67. {
  68. string filePath = PlayerPrefs.GetString(HotUpdateAllAssets);
  69. Task<byte[]> Tb2 = File.ReadAllBytesAsync(filePath);
  70. yield return Tb2;
  71. text.text = "正在解析配置";
  72. slider.gameObject.SetActive(false);
  73. AssetBundleCreateRequest acr = AssetBundle.LoadFromMemoryAsync(Tb2.Result);
  74. while (!acr.isDone)
  75. {
  76. text2.text = (acr.progress * 100).ToString("F2") + "%";
  77. slider.value = float.Parse((acr.progress).ToString("F2"));
  78. yield return null;
  79. }
  80. slider.gameObject.SetActive(true);
  81. ablist = acr.assetBundle;
  82. AssetBundleRequest abr = acr.assetBundle.LoadAssetAsync<GameObject>("MRNavigatorStart");
  83. text.text = "正在加载配置";
  84. while (!abr.isDone)
  85. {
  86. text2.text = (abr.progress * 100).ToString("F2") + "%";
  87. slider.value = float.Parse((abr.progress).ToString("F2"));
  88. yield return null;
  89. }
  90. Instantiate(abr.asset);
  91. this.gameObject.SetActive(false);
  92. }
  93. IEnumerator DownLoadAssets(bool isRemote = true)
  94. {
  95. slider.gameObject.SetActive(true);
  96. string dllPath = GetWebRequestPath(HotUpdateAllAssets);
  97. if (isRemote)
  98. dllPath = GetWebRequestPath();
  99. Debug.Log($"start download asset:{dllPath}");
  100. UnityWebRequest www = UnityWebRequest.Get(dllPath);
  101. www.SendWebRequest();
  102. while (!www.isDone)
  103. {
  104. text2.text = (www.downloadProgress * 100).ToString("F2") + "%";
  105. slider.value = float.Parse((www.downloadProgress).ToString("F2"));
  106. yield return null;
  107. }
  108. #if UNITY_2020_1_OR_NEWER
  109. if (www.result != UnityWebRequest.Result.Success)
  110. {
  111. Debug.Log(www.error);
  112. }
  113. #else
  114. if (www.isHttpError || www.isNetworkError)
  115. {
  116. Debug.Log(www.error);
  117. }
  118. #endif
  119. else
  120. {
  121. text.text = "正在加载主要资源";
  122. byte[] assetData = www.downloadHandler.data;
  123. string fileName = HotUpdateAllAssets;
  124. if (!Directory.Exists(Application.persistentDataPath + "/HotUpdate"))
  125. Directory.CreateDirectory(Application.persistentDataPath + "/HotUpdate");
  126. string filePathname = Application.persistentDataPath + "/HotUpdate/" + fileName;
  127. // if (File.Exists(filePathname))
  128. // File.Delete(filePathname);
  129. File.WriteAllBytes(filePathname, www.downloadHandler.data);
  130. PlayerPrefs.SetString("HotUpdateAllAssets", filePathname);
  131. AssetBundleCreateRequest acr = AssetBundle.LoadFromMemoryAsync(assetData);
  132. while (!acr.isDone)
  133. {
  134. text2.text = (acr.progress * 100).ToString("F2") + "%";
  135. slider.value = float.Parse((acr.progress).ToString("F2"));
  136. yield return null;
  137. }
  138. slider.gameObject.SetActive(true);
  139. ablist = acr.assetBundle;
  140. AssetBundleRequest abr = acr.assetBundle.LoadAssetAsync<GameObject>("MRNavigatorStart");
  141. text.text = "正在生成资源";
  142. while (!abr.isDone)
  143. {
  144. text2.text = (abr.progress * 100).ToString("F2") + "%";
  145. slider.value = float.Parse((abr.progress).ToString("F2"));
  146. yield return null;
  147. }
  148. Instantiate(abr.asset);
  149. switch (Application.version)
  150. {
  151. case "2.3.0.1":
  152. AssetBundleRequest ERNIEBot = acr.assetBundle.LoadAssetAsync<GameObject>("ERNIEBot");
  153. while (!ERNIEBot.isDone)
  154. {
  155. yield return null;
  156. }
  157. (Instantiate(ERNIEBot.asset) as GameObject).SetActive(true);
  158. break;
  159. }
  160. if (isRemote)
  161. {
  162. JsonData data = JsonMapper.ToObject(HttpSDKAction.Instance.jsonData);
  163. PlayerPrefs.SetString("HotUpdateVersion", data["HotUpdateVersion"].ToString());
  164. }
  165. this.gameObject.SetActive(false);
  166. }
  167. }
  168. private static string GetWebRequestPath()
  169. {
  170. JsonData data = JsonMapper.ToObject(HttpSDKAction.Instance.jsonData);
  171. string artUrl = data["HotUpdateAllAssets"].ToString();
  172. return artUrl;
  173. }
  174. private string GetWebRequestPath(string asset)
  175. {
  176. var path = $"{Application.streamingAssetsPath}/{asset}";
  177. if (!path.Contains("://"))
  178. {
  179. path = "file://" + path;
  180. }
  181. return path;
  182. }
  183. }