你知道unity中的图片是怎样保存的吗?如果不知道,现在就来看看把。通过代码我们就可以轻松搞定。
- [csharp] view plaincopy
- using UnityEngine;
- using System.Collections;
- using System.IO;
- public class DownPicture : MonoBehaviour {
- public GameObject plane;
- WWW www;
- string filePath;
- Texture2D test;
- Texture2D newTexture;
- // Use this for initialization
- void Start () {
- filePath = Application.dataPath + "/Resources/picture.jpg";
- if (System.IO.File.Exists(filePath))
- {
- Debug.Log("文件已存在");
- test = (Texture2D)Resources.Load("picture", typeof(Texture2D));
- plane.renderer.material.mainTexture = test;
- }
- else
- {
- Debug.Log("文件开始下载");
- StartCoroutine(GetImage());
- }
- }
- // Update is called once per frame
- void Update ()
- {
- }
- IEnumerator GetImage()
- {
- string url = "http://192.168.2.105:8080/Test/picture/1.jpg";
- www = new WWW(url);
- yield return www;
- newTexture = www.texture;
- byte[] pngData = newTexture.EncodeToPNG();
- File.WriteAllBytes(filePath, pngData);
- }
- void OnGUI()
- {
- if (www.isDone)
- {
- plane.renderer.material.mainTexture = newTexture;
- }
- }
- }