温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

[unity3d]下载网上的图片并保存到本地

发布时间:2020-08-08 12:57:46 来源:网络 阅读:701 作者:蓬莱仙羽 栏目:游戏开发

下面介绍一个下载一个网络资源保存到本地的例子,正好是现在项目中的需求。

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的,后来就多出来了,这就达到预想的效果!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI