UITexture.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/UI/NGUI Texture")]
  5. public class UITexture : UIBasicSprite
  6. {
  7. public override Texture mainTexture
  8. {
  9. get
  10. {
  11. if (this.mTexture != null)
  12. {
  13. return this.mTexture;
  14. }
  15. if (this.mMat != null)
  16. {
  17. return this.mMat.mainTexture;
  18. }
  19. return null;
  20. }
  21. set
  22. {
  23. if (this.mTexture != value)
  24. {
  25. if (this.drawCall != null && this.drawCall.widgetCount == 1 && this.mMat == null)
  26. {
  27. this.mTexture = value;
  28. this.drawCall.mainTexture = value;
  29. }
  30. else
  31. {
  32. base.RemoveFromPanel();
  33. this.mTexture = value;
  34. this.mPMA = -1;
  35. this.MarkAsChanged();
  36. }
  37. }
  38. }
  39. }
  40. public override Material material
  41. {
  42. get
  43. {
  44. return this.mMat;
  45. }
  46. set
  47. {
  48. if (this.mMat != value)
  49. {
  50. base.RemoveFromPanel();
  51. this.mShader = null;
  52. this.mMat = value;
  53. this.mPMA = -1;
  54. this.MarkAsChanged();
  55. }
  56. }
  57. }
  58. public override Shader shader
  59. {
  60. get
  61. {
  62. if (this.mMat != null)
  63. {
  64. return this.mMat.shader;
  65. }
  66. if (this.mShader == null)
  67. {
  68. this.mShader = Shader.Find("Unlit/Transparent Colored");
  69. }
  70. return this.mShader;
  71. }
  72. set
  73. {
  74. if (this.mShader != value)
  75. {
  76. if (this.drawCall != null && this.drawCall.widgetCount == 1 && this.mMat == null)
  77. {
  78. this.mShader = value;
  79. this.drawCall.shader = value;
  80. }
  81. else
  82. {
  83. base.RemoveFromPanel();
  84. this.mShader = value;
  85. this.mPMA = -1;
  86. this.mMat = null;
  87. this.MarkAsChanged();
  88. }
  89. }
  90. }
  91. }
  92. public override bool premultipliedAlpha
  93. {
  94. get
  95. {
  96. if (this.mPMA == -1)
  97. {
  98. Material material = this.material;
  99. this.mPMA = ((!(material != null) || !(material.shader != null) || !material.shader.name.Contains("Premultiplied")) ? 0 : 1);
  100. }
  101. return this.mPMA == 1;
  102. }
  103. }
  104. public override Vector4 border
  105. {
  106. get
  107. {
  108. return this.mBorder;
  109. }
  110. set
  111. {
  112. if (this.mBorder != value)
  113. {
  114. this.mBorder = value;
  115. this.MarkAsChanged();
  116. }
  117. }
  118. }
  119. public Rect uvRect
  120. {
  121. get
  122. {
  123. return this.mRect;
  124. }
  125. set
  126. {
  127. if (this.mRect != value)
  128. {
  129. this.mRect = value;
  130. this.MarkAsChanged();
  131. }
  132. }
  133. }
  134. public override Vector4 drawingDimensions
  135. {
  136. get
  137. {
  138. Vector2 pivotOffset = base.pivotOffset;
  139. float num = -pivotOffset.x * (float)this.mWidth;
  140. float num2 = -pivotOffset.y * (float)this.mHeight;
  141. float num3 = num + (float)this.mWidth;
  142. float num4 = num2 + (float)this.mHeight;
  143. if (this.mTexture != null && this.mType != UIBasicSprite.Type.Tiled)
  144. {
  145. int width = this.mTexture.width;
  146. int height = this.mTexture.height;
  147. int num5 = 0;
  148. int num6 = 0;
  149. float num7 = 1f;
  150. float num8 = 1f;
  151. if (width > 0 && height > 0 && (this.mType == UIBasicSprite.Type.Simple || this.mType == UIBasicSprite.Type.Filled))
  152. {
  153. if ((width & 1) != 0)
  154. {
  155. num5++;
  156. }
  157. if ((height & 1) != 0)
  158. {
  159. num6++;
  160. }
  161. num7 = 1f / (float)width * (float)this.mWidth;
  162. num8 = 1f / (float)height * (float)this.mHeight;
  163. }
  164. if (this.mFlip == UIBasicSprite.Flip.Horizontally || this.mFlip == UIBasicSprite.Flip.Both)
  165. {
  166. num += (float)num5 * num7;
  167. }
  168. else
  169. {
  170. num3 -= (float)num5 * num7;
  171. }
  172. if (this.mFlip == UIBasicSprite.Flip.Vertically || this.mFlip == UIBasicSprite.Flip.Both)
  173. {
  174. num2 += (float)num6 * num8;
  175. }
  176. else
  177. {
  178. num4 -= (float)num6 * num8;
  179. }
  180. }
  181. float num9;
  182. float num10;
  183. if (this.mFixedAspect)
  184. {
  185. num9 = 0f;
  186. num10 = 0f;
  187. }
  188. else
  189. {
  190. Vector4 border = this.border;
  191. num9 = border.x + border.z;
  192. num10 = border.y + border.w;
  193. }
  194. float x = Mathf.Lerp(num, num3 - num9, this.mDrawRegion.x);
  195. float y = Mathf.Lerp(num2, num4 - num10, this.mDrawRegion.y);
  196. float z = Mathf.Lerp(num + num9, num3, this.mDrawRegion.z);
  197. float w = Mathf.Lerp(num2 + num10, num4, this.mDrawRegion.w);
  198. return new Vector4(x, y, z, w);
  199. }
  200. }
  201. public bool fixedAspect
  202. {
  203. get
  204. {
  205. return this.mFixedAspect;
  206. }
  207. set
  208. {
  209. if (this.mFixedAspect != value)
  210. {
  211. this.mFixedAspect = value;
  212. this.mDrawRegion = new Vector4(0f, 0f, 1f, 1f);
  213. this.MarkAsChanged();
  214. }
  215. }
  216. }
  217. public override void MakePixelPerfect()
  218. {
  219. base.MakePixelPerfect();
  220. if (this.mType == UIBasicSprite.Type.Tiled)
  221. {
  222. return;
  223. }
  224. Texture mainTexture = this.mainTexture;
  225. if (mainTexture == null)
  226. {
  227. return;
  228. }
  229. if ((this.mType == UIBasicSprite.Type.Simple || this.mType == UIBasicSprite.Type.Filled || !base.hasBorder) && mainTexture != null)
  230. {
  231. int num = mainTexture.width;
  232. int num2 = mainTexture.height;
  233. if ((num & 1) == 1)
  234. {
  235. num++;
  236. }
  237. if ((num2 & 1) == 1)
  238. {
  239. num2++;
  240. }
  241. base.width = num;
  242. base.height = num2;
  243. }
  244. }
  245. protected override void OnUpdate()
  246. {
  247. base.OnUpdate();
  248. if (this.mFixedAspect)
  249. {
  250. Texture mainTexture = this.mainTexture;
  251. if (mainTexture != null)
  252. {
  253. int num = mainTexture.width;
  254. int num2 = mainTexture.height;
  255. if ((num & 1) == 1)
  256. {
  257. num++;
  258. }
  259. if ((num2 & 1) == 1)
  260. {
  261. num2++;
  262. }
  263. float num3 = (float)this.mWidth;
  264. float num4 = (float)this.mHeight;
  265. float num5 = num3 / num4;
  266. float num6 = (float)num / (float)num2;
  267. if (num6 < num5)
  268. {
  269. float num7 = (num3 - num4 * num6) / num3 * 0.5f;
  270. base.drawRegion = new Vector4(num7, 0f, 1f - num7, 1f);
  271. }
  272. else
  273. {
  274. float num8 = (num4 - num3 / num6) / num4 * 0.5f;
  275. base.drawRegion = new Vector4(0f, num8, 1f, 1f - num8);
  276. }
  277. }
  278. }
  279. }
  280. public override void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
  281. {
  282. Texture mainTexture = this.mainTexture;
  283. if (mainTexture == null)
  284. {
  285. return;
  286. }
  287. Rect rect = new Rect(this.mRect.x * (float)mainTexture.width, this.mRect.y * (float)mainTexture.height, (float)mainTexture.width * this.mRect.width, (float)mainTexture.height * this.mRect.height);
  288. Rect inner = rect;
  289. Vector4 border = this.border;
  290. inner.xMin += border.x;
  291. inner.yMin += border.y;
  292. inner.xMax -= border.z;
  293. inner.yMax -= border.w;
  294. float num = 1f / (float)mainTexture.width;
  295. float num2 = 1f / (float)mainTexture.height;
  296. rect.xMin *= num;
  297. rect.xMax *= num;
  298. rect.yMin *= num2;
  299. rect.yMax *= num2;
  300. inner.xMin *= num;
  301. inner.xMax *= num;
  302. inner.yMin *= num2;
  303. inner.yMax *= num2;
  304. int size = verts.size;
  305. base.Fill(verts, uvs, cols, rect, inner);
  306. if (this.onPostFill != null)
  307. {
  308. this.onPostFill(this, size, verts, uvs, cols);
  309. }
  310. }
  311. [HideInInspector]
  312. [SerializeField]
  313. private Rect mRect = new Rect(0f, 0f, 1f, 1f);
  314. [HideInInspector]
  315. [SerializeField]
  316. private Texture mTexture;
  317. [HideInInspector]
  318. [SerializeField]
  319. private Material mMat;
  320. [HideInInspector]
  321. [SerializeField]
  322. private Shader mShader;
  323. [HideInInspector]
  324. [SerializeField]
  325. private Vector4 mBorder = Vector4.zero;
  326. [HideInInspector]
  327. [SerializeField]
  328. private bool mFixedAspect;
  329. [NonSerialized]
  330. private int mPMA = -1;
  331. }