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;
///
/// 游戏启动器
///
public class Launcher : MonoBehaviour
{
///
/// Panel
///
private GameObject mPanel = null;
///
/// 显示百分比的文本框
///
private Text mProgressText = null;
///
/// 更新进度条
///
private Slider mUpdateSlider = null;
///
/// 显示提示的
///
private Text mTipsText = null;
///
/// 更新标题
///
private Text mUpdateTitleText = null;
///
/// 是否开始更新
///
private bool startUpdate = false;
///
/// 更新信息
///
private UpdateInfo mUpdateInfo = null;
// 结果
private CheckUpdate.CheckResult mCheckResult = null;
private string apkURL = "";
///
/// 开始
///
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));
});
}
///
/// 获取更新
///
///
///
///
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();
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();
}
///
/// 弹窗提示回调
///
private void CueHaveUpdate()
{
// 获取更新文件
UpdateHelper.Instance().GetUpdateFile(mCheckResult.cdn,()=>
{
mUpdateInfo = UpdateHelper.Instance().GetUpdateInfo();
startUpdate = true;
mPanel.gameObject.SetActive(true);
});
}
///
/// 加载字符集
///
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);
}
}
///
/// 初始化
///
public void Init()
{
GameObject obj = GameObject.Find("UI_ONUpdateWindow");
mPanel = obj.transform.Find("Panel").gameObject;
mUpdateSlider = obj.transform.Find("Panel/UpdatePanel/Slider").GetComponent();
mProgressText = obj.transform.Find("Panel/UpdatePanel/percentText").GetComponent();
mUpdateTitleText = obj.transform.Find("Panel/UpdatePanel/UpdateFileText").GetComponent();
mUpdateTitleText.text = "正在检查更新......";
mTipsText = obj.transform.Find("Panel/UpdatePanel/TipsText").GetComponent();
}
///
/// 更新
///
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) + "%";
}
}
}
}
///
/// 整包更新
///
private void UpdateAPK()
{
Application.OpenURL(apkURL);
Application.Quit();
}
///
/// 退出游戏
///
private void ExitGame()
{
Application.Quit();
}
}