DownClient.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.IO;
  6. public class DownClient : MonoBehaviour
  7. {
  8. public Action InstallBackAction;
  9. void Start()
  10. {
  11. MessageCenterController.Instance.Register(GameEnum.MESSAGE_UPDATE_START, StartUpdate);
  12. }
  13. void OnDestroy()
  14. {
  15. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_UPDATE_START, StartUpdate);
  16. }
  17. private string link;
  18. private UnityEngine.UI.Text MText;
  19. private Slider3D MSlider;
  20. public void SetLink(string link)
  21. {
  22. this.link = link;
  23. }
  24. public void SetUI(UnityEngine.UI.Text Text, Slider3D Slider)
  25. {
  26. MText = Text;
  27. MSlider = Slider;
  28. }
  29. //开始更新
  30. public void StartUpdate(System.Object datas)
  31. {
  32. CDebug.Log("StartUpdate");
  33. MSlider.gameObject.SetActive(true);
  34. MText.text = "开始加载";
  35. StartCoroutine("InstallApk");
  36. }
  37. private string InstallApkName
  38. {
  39. get
  40. {
  41. string[] strs = link.Split('/');
  42. if (strs.Length >= 2)
  43. return strs[strs.Length - 1];
  44. else
  45. return link;
  46. }
  47. }
  48. private string ApkPath
  49. {
  50. get
  51. {
  52. #if UNITY_EDITOR
  53. return Application.persistentDataPath + "/" + InstallApkName;
  54. #elif UNITY_IPHONE || UNITY_ANDROID
  55. return "/data/data/com.BellCat.MREducation/files"+ "/" + InstallApkName;
  56. #endif
  57. }
  58. }
  59. private string OldPath(string url)
  60. {
  61. #if UNITY_EDITOR
  62. return Application.persistentDataPath + "/" + url;
  63. #elif UNITY_IPHONE || UNITY_ANDROID
  64. return "/data/data/com.BellCat.MREducation/files"+ "/" + url;
  65. #endif
  66. }
  67. public IEnumerator InstallApk()
  68. {
  69. //将apk写入沙盒目录
  70. string path = ApkPath;
  71. CDebug.Log("安装路径 " + path);
  72. //删除掉旧的
  73. if (File.Exists(path))
  74. {
  75. //删除文件
  76. File.Delete(path);
  77. }
  78. //if (!File.Exists(path))
  79. if(true)
  80. {
  81. WWW www = new WWW(link);
  82. //下载需要更新的apk
  83. while (true)
  84. {
  85. if (www.isDone)
  86. {
  87. break;
  88. }
  89. //MText.text = ((int)(www.progress / 1f * 100)).ToString() + "%";
  90. MSlider.value = www.progress;
  91. yield return null;
  92. }
  93. if (!string.IsNullOrEmpty(www.error))
  94. {
  95. MText.text = www.error;
  96. yield return 0;
  97. }
  98. File.WriteAllBytes(path, www.bytes);
  99. CDebug.Log("写入文件-->" + path);
  100. }
  101. else
  102. {
  103. yield return 0;
  104. }
  105. Debug.Log("-->" + path);
  106. #if UNITY_ANDROID && !UNITY_EDITOR
  107. TestAPK(path);
  108. /*
  109. AndroidJavaClass javaClass;
  110. javaClass = new AndroidJavaClass("example.administrator.myapplication.MainActivity");
  111. Debug.Log("-->" + "正在安装" + path);
  112. bool res = javaClass.CallStatic<bool>("installAPK", path);
  113. if (res)
  114. {
  115. MText.text = "加载完成";
  116. //Application.Quit();
  117. }
  118. else
  119. {
  120. MText.text = "加载失败";
  121. }
  122. */
  123. #endif
  124. //UnityEngine.SceneManagement.SceneManager.LoadScene("GameStart3D_beauty");
  125. }
  126. private void TestAPK(string fileproviderAuthority)
  127. {
  128. if (!File.Exists(fileproviderAuthority))
  129. {
  130. CDebug.Log("-->" + "文件不存在" + fileproviderAuthority);
  131. return;
  132. }
  133. CDebug.Log("-->" + "正在安装" + fileproviderAuthority);
  134. string value = SC.Tools.UpdateAPKSystem.AndroidPluginUpdateAPK.getInstant.InstallApkWithActivity(fileproviderAuthority);
  135. CDebug.Log("[" + GetType().ToString() + "]: " + "Install result:" + value);
  136. if (value == "Success")
  137. {
  138. MText.text = "加载完成" + value;
  139. }
  140. else
  141. {
  142. MText.text = "加载失败" + value;
  143. File.Delete(fileproviderAuthority);
  144. }
  145. }
  146. public void DeleteOldFile(string fileName)
  147. {
  148. if (fileName == null)
  149. {
  150. return;
  151. }
  152. //沙盒目录
  153. string path = OldPath(fileName);
  154. if (File.Exists(path))
  155. {
  156. //删除文件
  157. File.Delete(path);
  158. }
  159. }
  160. }