UnityImage.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonUI.Display;
  5. using UnityEngine;
  6. using System.IO;
  7. using CommonUI_Unity3D.Src.M3Z;
  8. using CommonLang.IO;
  9. using CommonUI_Unity3D.Impl;
  10. using UnityEngine.Internal;
  11. namespace CommonUI_Unity3D.Impl
  12. {
  13. public class UnityImage : Image, IUnityImageInterface
  14. {
  15. private Texture2D mTexture;
  16. private Texture2D mTextureMask;
  17. internal int mLogicW, mLogicH;
  18. internal float mMaxU, mMaxV;
  19. private Material mMaterial;
  20. private string mResStr;
  21. public string ResourceStr
  22. {
  23. get
  24. {
  25. return mResStr;
  26. }
  27. }
  28. public bool SupportReleaseTexture { get; set; }
  29. internal static void FilterTexture(Texture2D tex)
  30. {
  31. tex.filterMode = FilterMode.Bilinear;
  32. tex.wrapMode = TextureWrapMode.Clamp;
  33. tex.anisoLevel = 2;
  34. tex.mipMapBias = 0;
  35. }
  36. public UnityImage(Texture2D tex, string name,string res = null)
  37. {
  38. mResStr = res;
  39. SupportReleaseTexture = true;
  40. ResestTexture2D(tex, name);
  41. }
  42. public UnityImage(Texture2D tex, int logicW, int logicH, string name, string res = null)
  43. {
  44. mResStr = res;
  45. SupportReleaseTexture = true;
  46. ResestTexture2D(tex, logicW, logicH, name);
  47. }
  48. public UnityImage(byte[] data, string name, string res = null)
  49. {
  50. mResStr = res;
  51. SupportReleaseTexture = true;
  52. ResestTexture2D(data, name);
  53. }
  54. public override void ReleaseTexture()
  55. {
  56. //有ResourceStr才允许释放贴
  57. if (SupportReleaseTexture && !string.IsNullOrEmpty(mResStr))
  58. {
  59. if (mTexture != null)
  60. {
  61. Texture2D.Destroy(mTexture);
  62. mTexture = null;
  63. }
  64. if (mTextureMask != null)
  65. {
  66. Texture2D.Destroy(mTextureMask);
  67. mTextureMask = null;
  68. }
  69. if (mMaterial != null)
  70. {
  71. Material.Destroy(mMaterial);
  72. mMaterial = null;
  73. }
  74. }
  75. }
  76. private void CheckReload()
  77. {
  78. if(mTexture == null)
  79. {
  80. Driver.Instance.ReloadImage(this);
  81. }
  82. }
  83. public void ResestTexture2D(Texture2D tex, string name)
  84. {
  85. this.name = name;
  86. this.mTexture = tex;
  87. this.mTexture.name = name;
  88. FilterTexture(mTexture);
  89. this.mLogicW = tex.width;
  90. this.mLogicH = tex.height;
  91. this.mMaxU = 1f;
  92. this.mMaxV = 1f;
  93. }
  94. public void ResestTexture2D(Texture2D tex, int logicW, int logicH, string name)
  95. {
  96. this.name = name;
  97. this.mTexture = tex;
  98. this.mTexture.name = name;
  99. FilterTexture(mTexture);
  100. this.mLogicW = logicW;
  101. this.mLogicH = logicH;
  102. this.mMaxU = Math.Min(mLogicW / (float)tex.width, 1f);
  103. this.mMaxV = Math.Min(mLogicH / (float)tex.height, 1f);
  104. }
  105. public void ResestTexture2D(byte[] data, string name)
  106. {
  107. this.name = name;
  108. string ext = name.ToLower();
  109. if (ext.EndsWith(".m3z"))
  110. {
  111. using (var input = new MemoryStream(data, false))
  112. {
  113. M3ZHeader m3z = new M3ZHeader(input);
  114. InitWithM3Z(m3z);
  115. }
  116. }
  117. else if (ext.EndsWith(".g3z"))
  118. {
  119. using (var input = G3ZStream.DecompressToStream(data))
  120. {
  121. M3ZHeader m3z = new M3ZHeader(input);
  122. InitWithM3Z(m3z);
  123. }
  124. }
  125. else
  126. {
  127. this.mTexture = new Texture2D(1, 1, TextureFormat.RGBA32, false, true);
  128. this.mTexture.name = name;
  129. this.mTexture.LoadImage(data, true);
  130. FilterTexture(mTexture);
  131. this.mLogicW = mTexture.width;
  132. this.mLogicH = mTexture.height;
  133. this.mMaxU = 1;
  134. this.mMaxV = 1;
  135. }
  136. }
  137. private void InitWithM3Z(M3ZHeader header)
  138. {
  139. this.mMaxU = 1;
  140. this.mMaxV = 1;
  141. #if UNITY_EDITOR
  142. Debug.Log(string.Format("InitM3Z : {0} : format={1} src={2}x{3} pot={4}x{5}",
  143. name,
  144. header.trunks[0].type,
  145. header.srcWidth,
  146. header.srcHeight,
  147. header.trunks[0].pixelW,
  148. header.trunks[0].pixelH));
  149. #endif
  150. if (header.trunks.Length > 0)
  151. {
  152. this.mTexture = header.trunks[0].LoadRawTextureData();
  153. this.mTexture.name = name + "(Trunk0)";
  154. FilterTexture(this.mTexture);
  155. }
  156. if (header.trunks.Length > 1)
  157. {
  158. this.mTextureMask = header.trunks[1].LoadRawTextureData();
  159. this.mTextureMask.name = name + "(Trunk1)";
  160. FilterTexture(this.mTextureMask);
  161. }
  162. this.mLogicW = header.srcWidth;
  163. this.mLogicH = header.srcHeight;
  164. this.mMaxU = Math.Min(mLogicW / (float)mTexture.width, 1f);
  165. this.mMaxV = Math.Min(mLogicH / (float)mTexture.height, 1f);
  166. header = null;
  167. }
  168. //-----------------------------------------------------------------------------------------------------------
  169. public override int Width { get { return mLogicW; } }
  170. public override int Height { get { return mLogicH; } }
  171. public override float MaxU { get { return mMaxU; } }
  172. public override float MaxV { get { return mMaxV; } }
  173. public Texture Texture { get { return Texture2D; } }
  174. public Texture TextureMask { get { return Texture2DMask; } }
  175. public Texture2D Texture2D { get { CheckReload(); return mTexture; } }
  176. public Texture2D Texture2DMask { get { CheckReload(); return mTextureMask; } }
  177. public Material TextureMaterial { get { return GetMaterial(); } }
  178. //-----------------------------------------------------------------------------------------------------------
  179. public Material GetMaterial()
  180. {
  181. CheckReload();
  182. if (mMaterial == null)
  183. {
  184. mMaterial = UnityShaders.CreateMaterialUGUI(this);
  185. }
  186. return mMaterial;
  187. }
  188. protected override void Disposing()
  189. {
  190. if (mTexture != null)
  191. {
  192. Texture2D.Destroy(mTexture);
  193. mTexture = null;
  194. }
  195. if (mTextureMask != null)
  196. {
  197. Texture2D.Destroy(mTextureMask);
  198. mTextureMask = null;
  199. }
  200. if (mMaterial != null)
  201. {
  202. Material.Destroy(mMaterial);
  203. mMaterial = null;
  204. }
  205. }
  206. public override void CopyPixels(Image src, int sx, int sy, int sw, int sh, int dx, int dy)
  207. {
  208. UnityImage srci = src as UnityImage;
  209. UnityDriver.Platform.CopyPixels(srci.mTexture, sx, sy, sw, sh, this.mTexture, dx, dy);
  210. }
  211. public override void Flush()
  212. {
  213. mTexture.Apply();
  214. }
  215. public Texture GetTexture()
  216. {
  217. return mTexture;
  218. }
  219. public Texture GetTextureMask()
  220. {
  221. return mTextureMask;
  222. }
  223. //-----------------------------------------------------------------------------------------------------------
  224. }
  225. }