你知道unity中的图片是怎样保存的吗?如果不知道,现在就来看看把。通过代码我们就可以轻松搞定。
  1. [csharp] view plaincopy
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.IO;
  5. public class DownPicture : MonoBehaviour {
  6. public GameObject plane;
  7. WWW www;
  8. string filePath;
  9. Texture2D test;
  10. Texture2D newTexture;
  11. // Use this for initialization
  12. void Start () {
  13. filePath = Application.dataPath + "/Resources/picture.jpg";
  14. if (System.IO.File.Exists(filePath))
  15. {
  16. Debug.Log("文件已存在");
  17. test = (Texture2D)Resources.Load("picture", typeof(Texture2D));
  18. plane.renderer.material.mainTexture = test;
  19. }
  20. else
  21. {
  22. Debug.Log("文件开始下载");
  23. StartCoroutine(GetImage());
  24. }
  25. }
  26. // Update is called once per frame
  27. void Update ()
  28. {
  29. }
  30. IEnumerator GetImage()
  31. {
  32. string url = "http://192.168.2.105:8080/Test/picture/1.jpg";
  33. www = new WWW(url);
  34. yield return www;
  35. newTexture = www.texture;
  36. byte[] pngData = newTexture.EncodeToPNG();
  37. File.WriteAllBytes(filePath, pngData);
  38. }
  39. void OnGUI()
  40. {
  41. if (www.isDone)
  42. {
  43. plane.renderer.material.mainTexture = newTexture;
  44. }
  45. }
  46. }