UISprite.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [AddComponentMenu("NGUI/UI/NGUI Sprite")]
  5. public class UISprite : UIBasicSprite
  6. {
  7. public override Material material
  8. {
  9. get
  10. {
  11. return (!(this.mAtlas != null)) ? null : this.mAtlas.spriteMaterial;
  12. }
  13. }
  14. public UIAtlas atlas
  15. {
  16. get
  17. {
  18. return this.mAtlas;
  19. }
  20. set
  21. {
  22. if (this.mAtlas != value)
  23. {
  24. base.RemoveFromPanel();
  25. this.mAtlas = value;
  26. this.mSpriteSet = false;
  27. this.mSprite = null;
  28. if (string.IsNullOrEmpty(this.mSpriteName) && this.mAtlas != null && this.mAtlas.spriteList.Count > 0)
  29. {
  30. this.SetAtlasSprite(this.mAtlas.spriteList[0]);
  31. this.mSpriteName = this.mSprite.name;
  32. }
  33. if (!string.IsNullOrEmpty(this.mSpriteName))
  34. {
  35. string spriteName = this.mSpriteName;
  36. this.mSpriteName = string.Empty;
  37. this.spriteName = spriteName;
  38. this.MarkAsChanged();
  39. }
  40. }
  41. }
  42. }
  43. public string spriteName
  44. {
  45. get
  46. {
  47. return this.mSpriteName;
  48. }
  49. set
  50. {
  51. if (string.IsNullOrEmpty(value))
  52. {
  53. if (string.IsNullOrEmpty(this.mSpriteName))
  54. {
  55. return;
  56. }
  57. this.mSpriteName = string.Empty;
  58. this.mSprite = null;
  59. this.mChanged = true;
  60. this.mSpriteSet = false;
  61. }
  62. else if (this.mSpriteName != value)
  63. {
  64. this.mSpriteName = value;
  65. this.mSprite = null;
  66. this.mChanged = true;
  67. this.mSpriteSet = false;
  68. }
  69. }
  70. }
  71. public bool isValid
  72. {
  73. get
  74. {
  75. return this.GetAtlasSprite() != null;
  76. }
  77. }
  78. [Obsolete("Use 'centerType' instead")]
  79. public bool fillCenter
  80. {
  81. get
  82. {
  83. return this.centerType != UIBasicSprite.AdvancedType.Invisible;
  84. }
  85. set
  86. {
  87. if (value != (this.centerType != UIBasicSprite.AdvancedType.Invisible))
  88. {
  89. this.centerType = ((!value) ? UIBasicSprite.AdvancedType.Invisible : UIBasicSprite.AdvancedType.Sliced);
  90. this.MarkAsChanged();
  91. }
  92. }
  93. }
  94. public override Vector4 border
  95. {
  96. get
  97. {
  98. UISpriteData atlasSprite = this.GetAtlasSprite();
  99. if (atlasSprite == null)
  100. {
  101. return base.border;
  102. }
  103. return new Vector4((float)atlasSprite.borderLeft, (float)atlasSprite.borderBottom, (float)atlasSprite.borderRight, (float)atlasSprite.borderTop);
  104. }
  105. }
  106. public override float pixelSize
  107. {
  108. get
  109. {
  110. return (!(this.mAtlas != null)) ? 1f : this.mAtlas.pixelSize;
  111. }
  112. }
  113. public override int minWidth
  114. {
  115. get
  116. {
  117. if (this.type == UIBasicSprite.Type.Sliced || this.type == UIBasicSprite.Type.Advanced)
  118. {
  119. Vector4 vector = this.border * this.pixelSize;
  120. int num = Mathf.RoundToInt(vector.x + vector.z);
  121. UISpriteData atlasSprite = this.GetAtlasSprite();
  122. if (atlasSprite != null)
  123. {
  124. num += atlasSprite.paddingLeft + atlasSprite.paddingRight;
  125. }
  126. return Mathf.Max(base.minWidth, ((num & 1) != 1) ? num : (num + 1));
  127. }
  128. return base.minWidth;
  129. }
  130. }
  131. public override int minHeight
  132. {
  133. get
  134. {
  135. if (this.type == UIBasicSprite.Type.Sliced || this.type == UIBasicSprite.Type.Advanced)
  136. {
  137. Vector4 vector = this.border * this.pixelSize;
  138. int num = Mathf.RoundToInt(vector.y + vector.w);
  139. UISpriteData atlasSprite = this.GetAtlasSprite();
  140. if (atlasSprite != null)
  141. {
  142. num += atlasSprite.paddingTop + atlasSprite.paddingBottom;
  143. }
  144. return Mathf.Max(base.minHeight, ((num & 1) != 1) ? num : (num + 1));
  145. }
  146. return base.minHeight;
  147. }
  148. }
  149. public override Vector4 drawingDimensions
  150. {
  151. get
  152. {
  153. Vector2 pivotOffset = base.pivotOffset;
  154. float num = -pivotOffset.x * (float)this.mWidth;
  155. float num2 = -pivotOffset.y * (float)this.mHeight;
  156. float num3 = num + (float)this.mWidth;
  157. float num4 = num2 + (float)this.mHeight;
  158. if (this.GetAtlasSprite() != null && this.mType != UIBasicSprite.Type.Tiled)
  159. {
  160. int paddingLeft = this.mSprite.paddingLeft;
  161. int paddingBottom = this.mSprite.paddingBottom;
  162. int num5 = this.mSprite.paddingRight;
  163. int num6 = this.mSprite.paddingTop;
  164. int num7 = this.mSprite.width + paddingLeft + num5;
  165. int num8 = this.mSprite.height + paddingBottom + num6;
  166. float num9 = 1f;
  167. float num10 = 1f;
  168. if (num7 > 0 && num8 > 0 && (this.mType == UIBasicSprite.Type.Simple || this.mType == UIBasicSprite.Type.Filled))
  169. {
  170. if ((num7 & 1) != 0)
  171. {
  172. num5++;
  173. }
  174. if ((num8 & 1) != 0)
  175. {
  176. num6++;
  177. }
  178. num9 = 1f / (float)num7 * (float)this.mWidth;
  179. num10 = 1f / (float)num8 * (float)this.mHeight;
  180. }
  181. if (this.mFlip == UIBasicSprite.Flip.Horizontally || this.mFlip == UIBasicSprite.Flip.Both)
  182. {
  183. num += (float)num5 * num9;
  184. num3 -= (float)paddingLeft * num9;
  185. }
  186. else
  187. {
  188. num += (float)paddingLeft * num9;
  189. num3 -= (float)num5 * num9;
  190. }
  191. if (this.mFlip == UIBasicSprite.Flip.Vertically || this.mFlip == UIBasicSprite.Flip.Both)
  192. {
  193. num2 += (float)num6 * num10;
  194. num4 -= (float)paddingBottom * num10;
  195. }
  196. else
  197. {
  198. num2 += (float)paddingBottom * num10;
  199. num4 -= (float)num6 * num10;
  200. }
  201. }
  202. Vector4 vector = (!(this.mAtlas != null)) ? Vector4.zero : (this.border * this.pixelSize);
  203. float num11 = vector.x + vector.z;
  204. float num12 = vector.y + vector.w;
  205. float x = Mathf.Lerp(num, num3 - num11, this.mDrawRegion.x);
  206. float y = Mathf.Lerp(num2, num4 - num12, this.mDrawRegion.y);
  207. float z = Mathf.Lerp(num + num11, num3, this.mDrawRegion.z);
  208. float w = Mathf.Lerp(num2 + num12, num4, this.mDrawRegion.w);
  209. return new Vector4(x, y, z, w);
  210. }
  211. }
  212. public override bool premultipliedAlpha
  213. {
  214. get
  215. {
  216. return this.mAtlas != null && this.mAtlas.premultipliedAlpha;
  217. }
  218. }
  219. public UISpriteData GetAtlasSprite()
  220. {
  221. if (!this.mSpriteSet)
  222. {
  223. this.mSprite = null;
  224. }
  225. if (this.mSprite == null && this.mAtlas != null)
  226. {
  227. if (!string.IsNullOrEmpty(this.mSpriteName))
  228. {
  229. UISpriteData sprite = this.mAtlas.GetSprite(this.mSpriteName);
  230. if (sprite == null)
  231. {
  232. return null;
  233. }
  234. this.SetAtlasSprite(sprite);
  235. }
  236. if (this.mSprite == null && this.mAtlas.spriteList.Count > 0)
  237. {
  238. UISpriteData uispriteData = this.mAtlas.spriteList[0];
  239. if (uispriteData == null)
  240. {
  241. return null;
  242. }
  243. this.SetAtlasSprite(uispriteData);
  244. if (this.mSprite == null)
  245. {
  246. Debug.LogError(this.mAtlas.name + " seems to have a null sprite!");
  247. return null;
  248. }
  249. this.mSpriteName = this.mSprite.name;
  250. }
  251. }
  252. return this.mSprite;
  253. }
  254. protected void SetAtlasSprite(UISpriteData sp)
  255. {
  256. this.mChanged = true;
  257. this.mSpriteSet = true;
  258. if (sp != null)
  259. {
  260. this.mSprite = sp;
  261. this.mSpriteName = this.mSprite.name;
  262. }
  263. else
  264. {
  265. this.mSpriteName = ((this.mSprite == null) ? string.Empty : this.mSprite.name);
  266. this.mSprite = sp;
  267. }
  268. }
  269. public override void MakePixelPerfect()
  270. {
  271. if (!this.isValid)
  272. {
  273. return;
  274. }
  275. base.MakePixelPerfect();
  276. if (this.mType == UIBasicSprite.Type.Tiled)
  277. {
  278. return;
  279. }
  280. UISpriteData atlasSprite = this.GetAtlasSprite();
  281. if (atlasSprite == null)
  282. {
  283. return;
  284. }
  285. Texture mainTexture = this.mainTexture;
  286. if (mainTexture == null)
  287. {
  288. return;
  289. }
  290. if ((this.mType == UIBasicSprite.Type.Simple || this.mType == UIBasicSprite.Type.Filled || !atlasSprite.hasBorder) && mainTexture != null)
  291. {
  292. int num = Mathf.RoundToInt(this.pixelSize * (float)(atlasSprite.width + atlasSprite.paddingLeft + atlasSprite.paddingRight));
  293. int num2 = Mathf.RoundToInt(this.pixelSize * (float)(atlasSprite.height + atlasSprite.paddingTop + atlasSprite.paddingBottom));
  294. if ((num & 1) == 1)
  295. {
  296. num++;
  297. }
  298. if ((num2 & 1) == 1)
  299. {
  300. num2++;
  301. }
  302. base.width = num;
  303. base.height = num2;
  304. }
  305. }
  306. protected override void OnInit()
  307. {
  308. if (!this.mFillCenter)
  309. {
  310. this.mFillCenter = true;
  311. this.centerType = UIBasicSprite.AdvancedType.Invisible;
  312. }
  313. base.OnInit();
  314. }
  315. protected override void OnUpdate()
  316. {
  317. base.OnUpdate();
  318. if (this.mChanged || !this.mSpriteSet)
  319. {
  320. this.mSpriteSet = true;
  321. this.mSprite = null;
  322. this.mChanged = true;
  323. }
  324. }
  325. public override void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
  326. {
  327. Texture mainTexture = this.mainTexture;
  328. if (mainTexture == null)
  329. {
  330. return;
  331. }
  332. if (this.mSprite == null)
  333. {
  334. this.mSprite = this.atlas.GetSprite(this.spriteName);
  335. }
  336. if (this.mSprite == null)
  337. {
  338. return;
  339. }
  340. Rect rect = new Rect((float)this.mSprite.x, (float)this.mSprite.y, (float)this.mSprite.width, (float)this.mSprite.height);
  341. Rect rect2 = new Rect((float)(this.mSprite.x + this.mSprite.borderLeft), (float)(this.mSprite.y + this.mSprite.borderTop), (float)(this.mSprite.width - this.mSprite.borderLeft - this.mSprite.borderRight), (float)(this.mSprite.height - this.mSprite.borderBottom - this.mSprite.borderTop));
  342. rect = NGUIMath.ConvertToTexCoords(rect, mainTexture.width, mainTexture.height);
  343. rect2 = NGUIMath.ConvertToTexCoords(rect2, mainTexture.width, mainTexture.height);
  344. int size = verts.size;
  345. base.Fill(verts, uvs, cols, rect, rect2);
  346. if (this.onPostFill != null)
  347. {
  348. this.onPostFill(this, size, verts, uvs, cols);
  349. }
  350. }
  351. [HideInInspector]
  352. [SerializeField]
  353. private UIAtlas mAtlas;
  354. [HideInInspector]
  355. [SerializeField]
  356. private string mSpriteName;
  357. [HideInInspector]
  358. [SerializeField]
  359. private bool mFillCenter = true;
  360. [NonSerialized]
  361. protected UISpriteData mSprite;
  362. [NonSerialized]
  363. private bool mSpriteSet;
  364. }