您好,登錄后才能下訂單哦!
下面介紹一個下載一個網絡資源保存到本地的例子,正好是現在項目中的需求。
using UnityEngine; using System.Collections; using System.IO; public class Download : MonoBehaviour { public string url = "http://ww3.sinaimg.cn/large/80dfe250jw1dle1r2v4t9j.jpg"; public GUIText Test; WWW www; Color Alpha; bool Appear = false; int a = 0; IEnumerator Start() { www = new WWW(url); //定義www為WWW類型并且等于所下載下來的WWW中內容。 yield return www; //返回所下載的www的值 renderer.material.shader = Shader.Find("Transparent/Diffuse"); renderer.material.mainTexture = www.texture; Texture2D newTexture = www.texture; byte[] pngData = newTexture.EncodeToPNG(); try { if (Application.platform == RuntimePlatform.Android) { File.WriteAllBytes(Application.persistentDataPath + "/ICO.png", pngData); } else { //File.WriteAllBytes(Application.dataPath + "/download/ICO.png", pngData); File.WriteAllBytes(Application.dataPath + "/ICO.png", pngData); } } catch(IOException e) { print(e); } Alpha = renderer.material.color; Alpha.a = 0; Appear = true; renderer.enabled = true; renderer.material.color = Alpha; //將下載下來的WWW中的圖片賦予到默認物體的材質上進行渲染出來 } void Update() { Test.text = "DownLoad: " + www.progress; if(www.progress == 1 && Appear) { a++; Alpha = renderer.material.color; Alpha.a += 0.01F; renderer.material.color = Alpha; } if(Input.GetKey(KeyCode.Escape)) { Application.Quit(); } } void OnGUI() { if(GUI.Button(new Rect(Screen.width-120,Screen.height-40,120,30),"Click to XIAOWEI!")) { Application.OpenURL("https://cache.yisu.com/upload/information/20200217/48/4473.jpg的,后來就多出來了,這就達到預想的效果!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。