GameInit.cs 5.6 KB

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