using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.IO; public class DownClient : MonoBehaviour { public Action InstallBackAction; void Start() { MessageCenterController.Instance.Register(GameEnum.MESSAGE_UPDATE_START, StartUpdate); } void OnDestroy() { MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_UPDATE_START, StartUpdate); } private string link; private UnityEngine.UI.Text MText; private Slider3D MSlider; public void SetLink(string link) { this.link = link; } public void SetUI(UnityEngine.UI.Text Text, Slider3D Slider) { MText = Text; MSlider = Slider; } //开始更新 public void StartUpdate(System.Object datas) { CDebug.Log("StartUpdate"); MSlider.gameObject.SetActive(true); MText.text = "开始加载"; StartCoroutine("InstallApk"); } private string InstallApkName { get { string[] strs = link.Split('/'); if (strs.Length >= 2) return strs[strs.Length - 1]; else return link; } } private string ApkPath { get { #if UNITY_EDITOR return Application.persistentDataPath + "/" + InstallApkName; #elif UNITY_IPHONE || UNITY_ANDROID return "/data/data/com.BellCat.MREducation/files"+ "/" + InstallApkName; #endif } } private string OldPath(string url) { #if UNITY_EDITOR return Application.persistentDataPath + "/" + url; #elif UNITY_IPHONE || UNITY_ANDROID return "/data/data/com.BellCat.MREducation/files"+ "/" + url; #endif } public IEnumerator InstallApk() { //将apk写入沙盒目录 string path = ApkPath; CDebug.Log("安装路径 " + path); //删除掉旧的 if (File.Exists(path)) { //删除文件 File.Delete(path); } //if (!File.Exists(path)) if(true) { WWW www = new WWW(link); //下载需要更新的apk while (true) { if (www.isDone) { break; } //MText.text = ((int)(www.progress / 1f * 100)).ToString() + "%"; MSlider.value = www.progress; yield return null; } if (!string.IsNullOrEmpty(www.error)) { MText.text = www.error; yield return 0; } File.WriteAllBytes(path, www.bytes); CDebug.Log("写入文件-->" + path); } else { yield return 0; } Debug.Log("-->" + path); #if UNITY_ANDROID && !UNITY_EDITOR TestAPK(path); /* AndroidJavaClass javaClass; javaClass = new AndroidJavaClass("example.administrator.myapplication.MainActivity"); Debug.Log("-->" + "正在安装" + path); bool res = javaClass.CallStatic("installAPK", path); if (res) { MText.text = "加载完成"; //Application.Quit(); } else { MText.text = "加载失败"; } */ #endif //UnityEngine.SceneManagement.SceneManager.LoadScene("GameStart3D_beauty"); } private void TestAPK(string fileproviderAuthority) { if (!File.Exists(fileproviderAuthority)) { CDebug.Log("-->" + "文件不存在" + fileproviderAuthority); return; } CDebug.Log("-->" + "正在安装" + fileproviderAuthority); string value = SC.Tools.UpdateAPKSystem.AndroidPluginUpdateAPK.getInstant.InstallApkWithActivity(fileproviderAuthority); CDebug.Log("[" + GetType().ToString() + "]: " + "Install result:" + value); if (value == "Success") { MText.text = "加载完成" + value; } else { MText.text = "加载失败" + value; File.Delete(fileproviderAuthority); } } public void DeleteOldFile(string fileName) { if (fileName == null) { return; } //沙盒目录 string path = OldPath(fileName); if (File.Exists(path)) { //删除文件 File.Delete(path); } } }