1.新建一个UGUI的Image.
2.新建一个脚本wwwTest.cs:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using System.IO; 5 using UnityEngine.UI; 6 7 8 //图片下载测试 9 public class wwwTest : MonoBehaviour { 10 11 WWW www; //请求 12 string filePath; //保存的文件路径 13 Texture2D texture2D; //下载的图片 14 public Image m_image; //场景中的一个image 15 16 void Start () 17 { 18 //保存路径 19 filePath = Application.dataPath + "/Resources/picture.jpg"; 20 StartCoroutine(LoadImg());//开启一个协成 21 } 22 23 IEnumerator LoadImg() 24 { 25 //开始下载图片 26 www = new WWW("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510112762201&di=0cb1080fa257b5b5c89620f6ff0cbade&imgtype=jpg&src=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D909222868%2C1660214404%26fm%3D214%26gp%3D0.jpg"); 27 28 yield return www; 29 30 //下载完成,保存图片到路径filePath 31 texture2D = www.texture; 32 byte[] bytes = texture2D.EncodeToPNG(); 33 File.WriteAllBytes(filePath, bytes); 34 } 35 }
3.把wwwTest.cs拖到相机上,再把Image游戏对象拖到相机的wwwTest.cs脚本上.
4.运行.