Unity下載檔案並顯示進度
阿新 • • 發佈:2019-01-22
public class ItemScript : MonoBehaviour { //非同步物件 private WWW downloadOperation; // Use this for initialization void Start () { } // Update is called once per frame void Update () { //判斷非同步物件並且非同步物件沒有載入完畢,顯示進度 if(downloadOperation!=null&&!downloadOperation.isDone) { Text ProgressText = (Text)GameObject.Find("Canvas/ProgressBackGround/ProgressRect/ProgressText").GetComponent<Text>(); if(ProgressText != null) { ProgressText.text = string.Format("下載進度:{0:F}%",downloadOperation.progress*100.0); } } } public void OnClickRescourceItem() { //顯示下載進度面板 GameObject bg = GameObject.Find("Canvas").transform.Find("ProgressBackGround").gameObject; bg.SetActive(true); //開始下載 StartCoroutine(DownloadVideo(ItemAddress, ItemLocalFile)); } public IEnumerator DownloadVideo(string url, string localPath) { downloadOperation = new WWW(url); yield return downloadOperation; //下載完成,關閉進度面板 GameObject bg = GameObject.Find("Canvas").transform.Find("ProgressBackGround").gameObject; bg.SetActive(false); //生成檔案 Byte[] b = downloadOperation.bytes; File.WriteAllBytes(localPath, b); } }