using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.IO; using Assets.Scenes.Ride.Scripts; 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 = $"{App.GetLocalString("Current Version:")} {App.AppVersion}"; transform.Find("Panel/LastestVersion").GetComponent().text = $"{App.GetLocalString("Latest Version:")} {jo.Value("Version")}"; data = jo; var path = Application.temporaryCachePath + "/PowerFun.apk"; StartCoroutine(DownLoadExe(data.Value("Url"), path, (p, isComplete) => { transform.Find("Panel/Size").GetComponent().text = $"{App.GetLocalString("Downloaded")} {Math.Round(p * 100, 0)}%";// string.Format(, a, b); transform.Find("Panel/Progress").GetComponent().fillAmount = (float)p; if (isComplete) { PlayerPrefs.SetString("exeVersion", data.Value("Version")); new FileInfo(path + ".pfdownload").MoveTo(path); OpenApk(path); //Application.Quit(); } })); } /*下载文件*/ IEnumerator DownLoadExe(string url, string desFileName, Action OnDownloadProgressEvent) { string version = data.Value("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 /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("currentActivity"); //Debug.Log("打开apk"+ "file://" + path); activity.Call("OpenApk", path); //Application.Quit(); #endif } }