您好,登錄后才能下訂單哦!
下面介紹一個(gè)下載一個(gè)網(wǎng)絡(luò)資源保存到本地的例子,正好是現(xiàn)在項(xiàng)目中的需求。
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中內(nèi)容。 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中的圖片賦予到默認(rèn)物體的材質(zhì)上進(jìn)行渲染出來 } 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的,后來就多出來了,這就達(dá)到預(yù)想的效果!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。