131 lines
4.1 KiB
C#
131 lines
4.1 KiB
C#
|
|
using Newtonsoft.Json.Linq;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.Networking;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
public class AndroidUpdate : PFUIPanel
|
|||
|
|
{
|
|||
|
|
// Start is called before the first frame update
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Update is called once per frame
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
JObject data { get; set; }
|
|||
|
|
public void StartDownload(JObject jo)
|
|||
|
|
{
|
|||
|
|
transform.Find("Panel/CurrentVersion").GetComponent<Text>().text = $"Current Version: {App.AppVersion}";
|
|||
|
|
transform.Find("Panel/LastestVersion").GetComponent<Text>().text = $"Latest Version: {jo.Value<string>("Version")}";
|
|||
|
|
data = jo;
|
|||
|
|
var path = Application.temporaryCachePath + "/PowerFun.apk";
|
|||
|
|
StartCoroutine(DownLoadExe(data.Value<string>("Url"), path, (p, isComplete) =>
|
|||
|
|
{
|
|||
|
|
transform.Find("Panel/Size").GetComponent<Text>().text = $"Downloaded {Math.Round(p * 100, 0)}%";// string.Format(, a, b);
|
|||
|
|
transform.Find("Panel/Progress").GetComponent<Image>().fillAmount = (float)p;
|
|||
|
|
if (isComplete)
|
|||
|
|
{
|
|||
|
|
PlayerPrefs.SetString("exeVersion", data.Value<string>("Version"));
|
|||
|
|
new FileInfo(path + ".pfdownload").MoveTo(path);
|
|||
|
|
OpenApk(path);
|
|||
|
|
//Application.Quit();
|
|||
|
|
}
|
|||
|
|
}));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/*下载文件*/
|
|||
|
|
IEnumerator DownLoadExe(string url, string desFileName, Action<double, bool> OnDownloadProgressEvent)
|
|||
|
|
{
|
|||
|
|
string version = data.Value<string>("Version");
|
|||
|
|
if (File.Exists(desFileName))
|
|||
|
|
{
|
|||
|
|
//if(PlayerPrefs.GetString("exeVersion")==)
|
|||
|
|
if (version == null)
|
|||
|
|
{
|
|||
|
|
yield break;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
if (version == PlayerPrefs.GetString("exeVersion"))
|
|||
|
|
{
|
|||
|
|
OpenApk(desFileName);
|
|||
|
|
//Application.Quit();
|
|||
|
|
yield break;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
File.Delete(desFileName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//File.Delete(desFileName);
|
|||
|
|
}
|
|||
|
|
if (File.Exists(desFileName + ".pfdownload"))
|
|||
|
|
{
|
|||
|
|
File.Delete(desFileName + ".pfdownload");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
using (var uwr = UnityWebRequest.Get(url))
|
|||
|
|
{
|
|||
|
|
var operation = uwr.SendWebRequest();
|
|||
|
|
|
|||
|
|
while (!operation.isDone)
|
|||
|
|
{
|
|||
|
|
/*
|
|||
|
|
* as BugFinder metnioned in the comments
|
|||
|
|
* what you want to track is uwr.downloadProgress
|
|||
|
|
*/
|
|||
|
|
var downloadDataProgress = uwr.downloadProgress * 100;
|
|||
|
|
|
|||
|
|
/*
|
|||
|
|
* use a float division here
|
|||
|
|
* I don't know what type downloadDataProgress is
|
|||
|
|
* but if it is an int than you will always get
|
|||
|
|
* an int division <somethingSmallerThan100>/100 = 0
|
|||
|
|
*/
|
|||
|
|
//progressBar.fillAmount = downloadDataProgress / 100.0f;
|
|||
|
|
OnDownloadProgressEvent.Invoke(uwr.downloadProgress, false);
|
|||
|
|
Debug.Log("Download: " + downloadDataProgress);
|
|||
|
|
yield return null;
|
|||
|
|
}
|
|||
|
|
ByteArrayToFile(desFileName + ".pfdownload", uwr.downloadHandler.data);
|
|||
|
|
OnDownloadProgressEvent.Invoke(1, true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
bool ByteArrayToFile(string fileName, byte[] byteArray)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
|
|||
|
|
{
|
|||
|
|
fs.Write(byteArray, 0, byteArray.Length);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Debug.LogError(ex);
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void OpenApk(string path)
|
|||
|
|
{
|
|||
|
|
#if !UNITY_EDITOR
|
|||
|
|
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
|||
|
|
var activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
|
|||
|
|
//Debug.Log("打开apk"+ "file://" + path);
|
|||
|
|
activity.Call("OpenApk", path);
|
|||
|
|
Application.Quit();
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
}
|