using System.Collections; using UnityEngine; using System.IO; using SC.InputSystem; public class CheckVersionUpdate : PointerDelegate { [SerializeField] private UnityEngine.UI.Text MText; [SerializeField] private Slider3D MSlider; [SerializeField] private PopBase pop; [SerializeField] private SCButton DebugMark;//是否是测试服的标记 private bool isSending; void Start() { DebugMark.onClick.AddListener(DebugClick); MessageCenterController.Instance.Register(GameEnum.MESSAGE_UPDATE_START, StartUpdate); CDebug.Log("当前版本 "+ CurVersionValue().ToString()); Engine.Http.HttpManager.GetInstance().AddHttpMessageEvent(HandleHttpMessage); DebugClick(); } void OnDestroy() { DebugMark.onClick.RemoveListener(DebugClick); Engine.Http.HttpManager.GetInstance().DelHttMessageEvent(HandleHttpMessage); MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_UPDATE_START, StartUpdate); } private void DebugClick() { pop.Hide(); isSending = true; HttpStaticMessage.AutoSendHttpVerson(Application.version, IsDebug); } private void Update() { if(Time.realtimeSinceStartup > lastClickTime + 5) { clickCt = 0; } } //神秘代码部分 private int clickCt;//点击的计数器 private int OpenCt = 10; private float lastClickTime; private float maxResetClickCD = 0.5f; protected override void partAnyKeyDownDelegate(InputKeyCode keyCode, InputDevicePartBase part) { if(!isSending && API_InputSystem.Target != null) { CDebug.Log(API_InputSystem.Target.name); CheckResetClickCt(); clickCt +=1; lastClickTime = Time.realtimeSinceStartup; CDebug.Log("clickCt " + clickCt); if(clickCt == OpenCt) { //pop.Hide(); DebugMark.gameObject.SetActive(true); } else { DebugMark.gameObject.SetActive(false); } } } private void CheckResetClickCt() { if(!IsDebug && Time.realtimeSinceStartup > lastClickTime + maxResetClickCD) { clickCt = 0; } } private bool IsDebug{ get{return DebugMark.gameObject.activeSelf;} } void LateUpdate() { OnUpdate(CStaticMethod.SystemFrameTime()); } void FixedUpdate() { OnFixedUpdate(CStaticMethod.SystemFrameTime()); } public void OnUpdate(int nTime) { Engine.Http.HttpManager.GetInstance().OnUpdate(nTime); } public void OnFixedUpdate(int nTime) { //Http请求 Engine.Http.HttpManager.GetInstance().OnFixedUpdate(nTime); } private bool HandleHttpMessage(Engine.Http.HttpResponse httpResponse) { if (httpResponse == null) { return false; } if(!httpResponse.isSuccess) { return false; } CDebug.Log ("接受消息"+httpResponse.Code); switch (httpResponse.Code) { //获取角色信息 case MsgConst.HTTP_MSG_VERSION: CheckRes(httpResponse); break; } return false; } private string link; private void CheckRes(Engine.Http.HttpResponse httpResponse) { isSending = false; bool state = httpResponse.ReadBool("state"); if (state) { if(IsDebug) { this.link = httpResponse.ReadString("debug_url"); } else { this.link = httpResponse.ReadString("url"); } MText.text = ""; MSlider.gameObject.SetActive(false); pop.Show(IsDebug? httpResponse.ReadString("debug_ver"): httpResponse.ReadString("online_ver")); } else { clickCt = 0; pop.Hide(); CDebug.Log("StartCoroutine ChangeScene"); StartCoroutine("ChangeScene"); DeleteOldFile(httpResponse.ReadString("debug_url")); DeleteOldFile(httpResponse.ReadString("url")); } } private IEnumerator ChangeScene() { yield return new WaitForSeconds(3f); while(clickCt != 0) { CDebug.Log("等待计数器 " + clickCt); yield return new WaitForSeconds(1.0f); } CDebug.Log("开始切换场景"); UnityEngine.SceneManagement.SceneManager.LoadScene("GameStart3D_beauty"); } private float CurVersionValue() { float v; if(float.TryParse(Application.version, out v)) { return v; } return 0; } //开始更新 private 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)) { 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 = "加载完成"; } else { MText.text = "加载失败"; File.Delete(fileproviderAuthority); } } private void DeleteOldFile(string fileName) { if(fileName == null) { return; } //沙盒目录 string path = OldPath(fileName); if(File.Exists(path)) { //删除文件 File.Delete(path); } } }