123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using Newtonsoft.Json;
- using System.Text;
- using System.IO;
- using UnityEngine.Networking;
- using Newtonsoft.Json.Linq;
- using YLSJ;
- /// <summary>
- /// 游戏启动器
- /// </summary>
- public class Launcher : MonoBehaviour
- {
- /// <summary>
- /// Panel
- /// </summary>
- private GameObject mPanel = null;
- /// <summary>
- /// 显示百分比的文本框
- /// </summary>
- private Text mProgressText = null;
- /// <summary>
- /// 更新进度条
- /// </summary>
- private Slider mUpdateSlider = null;
- /// <summary>
- /// 显示提示的
- /// </summary>
- private Text mTipsText = null;
- /// <summary>
- /// 更新标题
- /// </summary>
- private Text mUpdateTitleText = null;
- /// <summary>
- /// 是否开始更新
- /// </summary>
- private bool startUpdate = false;
- /// <summary>
- /// 更新信息
- /// </summary>
- private UpdateInfo mUpdateInfo = null;
- // 结果
- private CheckUpdate.CheckResult mCheckResult = null;
- private string apkURL = "";
- /// <summary>
- /// 开始
- /// </summary>
- private void Start()
- {
- // 友盟统计: 开始更新之前
- AndroidInteractive.CallJavaFunction("onEvent", "e_k_9", "1", "1");
- StatHelper.Instance.AddEvent( "e_k_9", "1", "1");
- Init();
- string plat = "";
- string channel = "TapTap";
- if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- plat = "IOS";
- }
- else
- {
- plat = "Android";
- }
- // 检测更新
- UpdateHelper.Instance().GetCurVersion((ver) =>
- {
- StartCoroutine(IE_GetUpdate(ver, plat, channel));
- });
- }
- /// <summary>
- /// 获取更新
- /// </summary>
- /// <param name="myVer"></param>
- /// <param name="plat"></param>
- /// <returns></returns>
- private IEnumerator IE_GetUpdate(int myVer, string plat, string channel)
- {
- string url = CheckUpdate.Check_URL;
- UnityWebRequest www = UnityWebRequest.Get(url + "?clientVer=" + myVer + "&plat=" + plat + "&channel=" + channel);
- yield return www.SendWebRequest();
- while (!www.isDone)
- {
- yield return new WaitForSeconds(0.02f);
- }
- try
- {
- JObject jObj = JObject.Parse(www.downloadHandler.text);
- mCheckResult = jObj.ToObject<CheckUpdate.CheckResult>();
- int a = 1;
- }
- catch
- {
- mCheckResult = null;
- UI_CueDialog.Instance().Open("获取服务器地址失败!", "更新提示", E_DialogType.OneButton, ExitGame);
- }
- if (mCheckResult != null)
- {
- // 无更新
- if (mCheckResult.err == 0)
- {
- mPanel.SetActive(false);
- LoadAssembly();
- }
- // 整包更新
- else if (mCheckResult.err == 1031)
- {
- apkURL = mCheckResult.fullDownloadPage;
- UI_CueDialog.Instance().Open(mCheckResult.msg, "更新提示", E_DialogType.OneButton, UpdateAPK);
- }
- // 有更新
- else if (mCheckResult.err == 1032)
- {
- UI_CueDialog.Instance().Open(mCheckResult.msg, "更新提示", E_DialogType.TwoBntton, CueHaveUpdate, ExitGame);
- }
- // 通知,不可继续游戏
- else if (mCheckResult.err == 1033)
- {
- UI_CueDialog.Instance().Open(mCheckResult.msg, "更新提示", E_DialogType.OneButton, ExitGame);
- }
- }
- www.Dispose();
- }
- /// <summary>
- /// 弹窗提示回调
- /// </summary>
- private void CueHaveUpdate()
- {
- // 获取更新文件
- UpdateHelper.Instance().GetUpdateFile(mCheckResult.cdn,()=>
- {
- mUpdateInfo = UpdateHelper.Instance().GetUpdateInfo();
- startUpdate = true;
- mPanel.gameObject.SetActive(true);
- });
- }
- /// <summary>
- /// 加载字符集
- /// </summary>
- private void LoadAssembly()
- {
- mPanel.SetActive(false);
- mUpdateTitleText.text = "正在初始化中......";
- // 开启更新检测
- if (GlobalConfig.is_Update)
- {
- AssemblyHelper.Instance.InitializeAssembly(Constant.AssemblyFilePath, Constant.AssemblyName, () =>
- {
- AssemblyHelper.Instance.BindScript("Entrance_Launcher", this.gameObject);
- });
- }
- else
- {
- mPanel.SetActive(false);
- AssemblyHelper.Instance.BindScript("Entrance_Launcher", this.gameObject);
- }
- }
- /// <summary>
- /// 初始化
- /// </summary>
- public void Init()
- {
- GameObject obj = GameObject.Find("UI_ONUpdateWindow");
- mPanel = obj.transform.Find("Panel").gameObject;
- mUpdateSlider = obj.transform.Find("Panel/UpdatePanel/Slider").GetComponent<Slider>();
- mProgressText = obj.transform.Find("Panel/UpdatePanel/percentText").GetComponent<Text>();
- mUpdateTitleText = obj.transform.Find("Panel/UpdatePanel/UpdateFileText").GetComponent<Text>();
- mUpdateTitleText.text = "正在检查更新......";
- mTipsText = obj.transform.Find("Panel/UpdatePanel/TipsText").GetComponent<Text>();
- }
- /// <summary>
- /// 更新
- /// </summary>
- private void Update()
- {
- if (startUpdate == false)
- {
- return;
- }
- if (mUpdateInfo != null)
- {
- // 更新完成,切换到登陆关卡
- if (mUpdateInfo.mUpdateComplete)
- {
- mUpdateSlider.value = 1;
- mUpdateInfo = null;
- mUpdateTitleText.text = "更新完成";
- mTipsText.text = "开始初始化游戏,此过程不消耗流量......";
- mProgressText.text = "100%";
- startUpdate = false;
- LoadAssembly();
- AndroidInteractive.CallJavaFunction("onEvent", "e_k_1", "1", "1"); // 资源更新完成
- StatHelper.Instance.AddEvent( "e_k_1", "complete", "1"); // 资源更新完成
- }
- else
- {
- if (mUpdateInfo.mTotalFileCount > 0)
- {
- mUpdateTitleText.text = "正在更新中......";
- mTipsText.text = "更新进度: " + (mUpdateInfo.mCurFileIndex + 1) + "/" + mUpdateInfo.mTotalFileCount;
- mUpdateSlider.value = mUpdateInfo.mCurFileProgress;
- mProgressText.text = (int)(mUpdateInfo.mCurFileProgress * 100) + "%";
- }
- }
- }
- }
- /// <summary>
- /// 整包更新
- /// </summary>
- private void UpdateAPK()
- {
- Application.OpenURL(apkURL);
- Application.Quit();
- }
- /// <summary>
- /// 退出游戏
- /// </summary>
- private void ExitGame()
- {
- Application.Quit();
- }
- }
|