Textureload.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. public class Textureload : MonoBehaviour {
  7. public string url = "https://bchy.oss-cn-qingdao.aliyuncs.com/Work/5_2_0.png";
  8. public GameObject go;
  9. string path;
  10. void Start()
  11. {
  12. PhotoButtonClick();
  13. }
  14. /// <summary>
  15. /// 拍照按钮的功能实现
  16. /// </summary>
  17. private void PhotoButtonClick()
  18. {
  19. string fName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";//用时间作为图片的名称,保证唯一性
  20. PlayerPrefs.SetString("photoName", fName);//将名字存储,为了方便获取
  21. StartCoroutine(UploadPNG("5_2_0.png", "Work"));
  22. }
  23. /// <summary>
  24. /// 下载图片
  25. /// </summary>
  26. /// <param name="fileName"></param>
  27. /// <returns></returns>
  28. private IEnumerator UploadPNG(string fileName,string dic)
  29. {
  30. WWW www = new WWW(url);
  31. yield return www;
  32. byte[] bytes = www.texture.EncodeToPNG();
  33. path = PathForFile(fileName, dic);//移动平台的判断
  34. print("文件" + path);
  35. SaveNativeFile(bytes, path);//保存图片到本地
  36. }
  37. /// <summary>
  38. /// 判断平台
  39. /// </summary>
  40. /// <param name="filename"></param>
  41. /// <returns></returns>
  42. public string PathForFile(string filename,string dic)
  43. {
  44. if (Application.platform == RuntimePlatform.IPhonePlayer)
  45. {
  46. string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.Length - 5);
  47. path = path.Substring(0, path.LastIndexOf('/'));
  48. path = Path.Combine(path, "Documents");
  49. path = Path.Combine(path, dic);
  50. if (!Directory.Exists(path))
  51. {
  52. Directory.CreateDirectory(path);
  53. }
  54. return Path.Combine(path, filename);
  55. }
  56. else if (Application.platform == RuntimePlatform.Android)
  57. {
  58. string path = Application.persistentDataPath;
  59. path = path.Substring(0, path.LastIndexOf('/'));
  60. path = Path.Combine(path, dic);
  61. if (!Directory.Exists(path))
  62. {
  63. Directory.CreateDirectory(path);
  64. }
  65. return Path.Combine(path, filename);
  66. }
  67. else
  68. {
  69. string path = Application.dataPath;
  70. path = path.Substring(0, path.LastIndexOf('/'));
  71. path = Path.Combine(path, dic);
  72. if (!Directory.Exists(path))
  73. {
  74. Directory.CreateDirectory(path);
  75. }
  76. return Path.Combine(path, filename);
  77. }
  78. }
  79. /// <summary>
  80. /// 在本地保存文件
  81. /// </summary>
  82. /// <param name="bytes"></param>
  83. /// <param name="path"></param>
  84. public void SaveNativeFile(byte[] bytes, string path)
  85. {
  86. FileStream fs = new FileStream(path, FileMode.Create);
  87. fs.Write(bytes, 0, bytes.Length);
  88. fs.Flush();
  89. fs.Close();
  90. }
  91. void OnGUI()
  92. {
  93. if (GUI.Button(new Rect(200, 200, 120, 30), "Click"))
  94. {
  95. ShowNativeTexture();
  96. }
  97. }
  98. //上述是保存在移动平台的代码,下面则是读取到移动平台的图片
  99. /// <summary>
  100. /// 显示图片
  101. /// </summary>
  102. public void ShowNativeTexture()
  103. {
  104. go.GetComponent<MeshRenderer>().material.mainTexture = GetNativeFile(path);
  105. }
  106. /// <summary>
  107. /// 获取到本地的图片
  108. /// </summary>
  109. /// <param name="path"></param>
  110. public Texture2D GetNativeFile(string path)
  111. {
  112. try
  113. {
  114. var pathName = path;
  115. var bytes = ReadFile(pathName);
  116. int width = Screen.width;
  117. int height = Screen.height;
  118. var texture = new Texture2D(width, height);
  119. texture.LoadImage(bytes);
  120. return texture;
  121. }
  122. catch (Exception c)
  123. {
  124. }
  125. return null;
  126. }
  127. public byte[] ReadFile(string filePath)
  128. {
  129. var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  130. fs.Seek(0, SeekOrigin.Begin);
  131. var binary = new byte[fs.Length];
  132. fs.Read(binary, 0, binary.Length);
  133. fs.Close();
  134. return binary;
  135. }
  136. }
  137. //Global.SetPhotoTexture("Work" ,idex, "https://bchy.oss-cn-qingdao.aliyuncs.com", (myt) => {
  138. // _cartoonObj.transform.Find("Icon").GetComponent<UITexture>().mainTexture = myt;
  139. // _cartoonObj.transform.Find("PlayerDesc_Text").GetComponent<UILabel>().text = this.LoadWorkImageDesc(go.name);
  140. // _cartoonObj.transform.Find("PlayerName_Text").GetComponent<UILabel>().text = Global.MainPlayer.GetPlayerName() + this.LoadWorkImageTitle(go.name);
  141. // this.transform.Find("CartoonPanel/Remove").gameObject.SetActive(true);
  142. // this.transform.Find("CartoonPanel/SavaPhoto").gameObject.SetActive(true);
  143. // this.transform.Find("CartoonPanel/Lottry").gameObject.SetActive(true);
  144. // if (_cartoonObj != null)
  145. // _cartoonObj.gameObject.SetActive(true);
  146. //});