123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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<bool>("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);
- }
- }
- }
|