139 lines
5.1 KiB
C#
139 lines
5.1 KiB
C#
|
|
using Assets.Scripts;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Net;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.Networking;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
public class DownloadController : PFUIPanel
|
|||
|
|
{
|
|||
|
|
// Start is called before the first frame update
|
|||
|
|
Transform Button,Loading,Exit;
|
|||
|
|
ScrollRect scroll;
|
|||
|
|
string path;
|
|||
|
|
[SerializeField] Transform log;
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
Button = transform.Find("Panel/BtnConfirm");
|
|||
|
|
Loading = transform.Find("Panel/LoadingContainer");
|
|||
|
|
scroll = transform.Find("Panel/Scroll View").GetComponent<ScrollRect>();
|
|||
|
|
Exit = transform.Find("Panel/Exit");
|
|||
|
|
UIManager.AddEvent(Exit.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
|
|||
|
|
{
|
|||
|
|
Utils.showToast(gameObject, "Please update to the latest version.");
|
|||
|
|
});
|
|||
|
|
SetType(false);
|
|||
|
|
//Button.GetComponent<Button>().enabled = false;
|
|||
|
|
//Button.GetComponent<Button>().interactable = false;
|
|||
|
|
path = Application.persistentDataPath + "/PowerFun.exe";
|
|||
|
|
UIManager.AddEvent(Button.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
|
|||
|
|
{
|
|||
|
|
Application.Quit();
|
|||
|
|
Utils.ExecFile(path);
|
|||
|
|
});
|
|||
|
|
//transform
|
|||
|
|
if (App.UpdateObject != null)
|
|||
|
|
{
|
|||
|
|
StartCoroutine(DownLoadExe(App.UpdateObject.Url, path, (a, b, isComplete) =>
|
|||
|
|
{
|
|||
|
|
transform.Find("Panel/LoadingContainer/Text").GetComponent<Text>().text = $"Downloaded {a.ToString("#0")}M / {b.ToString("#0")}M";// string.Format(, a, b);
|
|||
|
|
transform.Find("Panel/LoadingContainer/Loading").GetComponent<Image>().fillAmount = (float)(a / b);
|
|||
|
|
if (isComplete)
|
|||
|
|
{
|
|||
|
|
SetType(true);
|
|||
|
|
PlayerPrefs.SetString("exeVersion", App.UpdateObject.Code);
|
|||
|
|
new FileInfo(path + ".pfdownload").MoveTo(path);
|
|||
|
|
}
|
|||
|
|
}));
|
|||
|
|
for (var i = 0; i < App.UpdateObject.UpdateLog.Count; i++)
|
|||
|
|
{
|
|||
|
|
var newLog = Instantiate<Transform>(log);
|
|||
|
|
newLog.GetComponent<DownloadLog>().Initial(App.UpdateObject.UpdateLog[i]);
|
|||
|
|
if (i == App.UpdateObject.UpdateLog.Count - 1)
|
|||
|
|
{
|
|||
|
|
newLog.GetComponent<DownloadLog>().HideLine();
|
|||
|
|
}
|
|||
|
|
newLog.SetParent(scroll.content);
|
|||
|
|
newLog.localScale = Vector3.one;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/*下载文件*/
|
|||
|
|
IEnumerator DownLoadExe(string url, string desFileName, Action<double, double, bool> OnDownloadProgressEvent)
|
|||
|
|
{
|
|||
|
|
string version = null;
|
|||
|
|
if (App.UpdateObject != null)
|
|||
|
|
{
|
|||
|
|
version = App.UpdateObject.Code;
|
|||
|
|
}
|
|||
|
|
if (File.Exists(desFileName))
|
|||
|
|
{
|
|||
|
|
//if(PlayerPrefs.GetString("exeVersion")==)
|
|||
|
|
if (version == null)
|
|||
|
|
{
|
|||
|
|
yield break;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
if (version == PlayerPrefs.GetString("exeVersion"))
|
|||
|
|
{
|
|||
|
|
Application.Quit();
|
|||
|
|
Utils.ExecFile(desFileName);
|
|||
|
|
yield break;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
File.Delete(desFileName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//File.Delete(desFileName);
|
|||
|
|
}
|
|||
|
|
if (File.Exists(desFileName + ".pfdownload"))
|
|||
|
|
{
|
|||
|
|
File.Delete(desFileName + ".pfdownload");
|
|||
|
|
}
|
|||
|
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
|
|||
|
|
request.Timeout = 5000;
|
|||
|
|
WebResponse response = request.GetResponse();
|
|||
|
|
//UnityWebRequest request = UnityWebRequest.Get(url);
|
|||
|
|
//yield return request.SendWebRequest();
|
|||
|
|
int packLength = 1024 * 20;
|
|||
|
|
int nReadSize = 0;
|
|||
|
|
byte[] nbytes = new byte[packLength];
|
|||
|
|
double dDownloadedLength = 0, dTotalLength = 0;
|
|||
|
|
long countLength = response.ContentLength;
|
|||
|
|
using (FileStream fs = new FileStream(desFileName + ".pfdownload", FileMode.Create))
|
|||
|
|
using (Stream netStream = response.GetResponseStream())
|
|||
|
|
{
|
|||
|
|
nReadSize = netStream.Read(nbytes, 0, packLength);
|
|||
|
|
while (nReadSize > 0)
|
|||
|
|
{
|
|||
|
|
fs.Write(nbytes, 0, nReadSize);
|
|||
|
|
nReadSize = netStream.Read(nbytes, 0, packLength);
|
|||
|
|
dDownloadedLength = fs.Length * 1.0 / (1024 * 1024);
|
|||
|
|
dTotalLength = countLength * 1.0 / (1024 * 1024);
|
|||
|
|
OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, false);
|
|||
|
|
//string ss = string.Format("已下载 {0:F}M / {1:F}M", dDownloadedLength, dTotalLength);
|
|||
|
|
//Debug.Log(ss);
|
|||
|
|
yield return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, true);
|
|||
|
|
}
|
|||
|
|
void SetType(bool f)
|
|||
|
|
{
|
|||
|
|
// true 显示 button false 显示loading
|
|||
|
|
Button.gameObject.SetActive(f);
|
|||
|
|
Loading.gameObject.SetActive(!f);
|
|||
|
|
}
|
|||
|
|
// Update is called once per frame
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|