123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- 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<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 = "加载完成";
- }
- 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);
- }
- }
- }
|