GameInit.cs 5.6 KB

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