UI2DSprite.cs 7.3 KB

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