UIAtlas.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [AddComponentMenu("NGUI/UI/Atlas")]
  5. public class UIAtlas : MonoBehaviour
  6. {
  7. public Material spriteMaterial
  8. {
  9. get
  10. {
  11. return (!(this.mReplacement != null)) ? this.material : this.mReplacement.spriteMaterial;
  12. }
  13. set
  14. {
  15. if (this.mReplacement != null)
  16. {
  17. this.mReplacement.spriteMaterial = value;
  18. }
  19. else if (this.material == null)
  20. {
  21. this.mPMA = 0;
  22. this.material = value;
  23. }
  24. else
  25. {
  26. this.MarkAsChanged();
  27. this.mPMA = -1;
  28. this.material = value;
  29. this.MarkAsChanged();
  30. }
  31. }
  32. }
  33. public bool premultipliedAlpha
  34. {
  35. get
  36. {
  37. if (this.mReplacement != null)
  38. {
  39. return this.mReplacement.premultipliedAlpha;
  40. }
  41. if (this.mPMA == -1)
  42. {
  43. Material spriteMaterial = this.spriteMaterial;
  44. this.mPMA = ((!(spriteMaterial != null) || !(spriteMaterial.shader != null) || !spriteMaterial.shader.name.Contains("Premultiplied")) ? 0 : 1);
  45. }
  46. return this.mPMA == 1;
  47. }
  48. }
  49. public List<UISpriteData> spriteList
  50. {
  51. get
  52. {
  53. if (this.mReplacement != null)
  54. {
  55. return this.mReplacement.spriteList;
  56. }
  57. if (this.mSprites.Count == 0)
  58. {
  59. this.Upgrade();
  60. }
  61. return this.mSprites;
  62. }
  63. set
  64. {
  65. if (this.mReplacement != null)
  66. {
  67. this.mReplacement.spriteList = value;
  68. }
  69. else
  70. {
  71. this.mSprites = value;
  72. }
  73. }
  74. }
  75. public Texture texture
  76. {
  77. get
  78. {
  79. return (!(this.mReplacement != null)) ? ((!(this.material != null)) ? null : this.material.mainTexture) : this.mReplacement.texture;
  80. }
  81. }
  82. public float pixelSize
  83. {
  84. get
  85. {
  86. return (!(this.mReplacement != null)) ? this.mPixelSize : this.mReplacement.pixelSize;
  87. }
  88. set
  89. {
  90. if (this.mReplacement != null)
  91. {
  92. this.mReplacement.pixelSize = value;
  93. }
  94. else
  95. {
  96. float num = Mathf.Clamp(value, 0.25f, 4f);
  97. if (this.mPixelSize != num)
  98. {
  99. this.mPixelSize = num;
  100. this.MarkAsChanged();
  101. }
  102. }
  103. }
  104. }
  105. public UIAtlas replacement
  106. {
  107. get
  108. {
  109. return this.mReplacement;
  110. }
  111. set
  112. {
  113. UIAtlas uiatlas = value;
  114. if (uiatlas == this)
  115. {
  116. uiatlas = null;
  117. }
  118. if (this.mReplacement != uiatlas)
  119. {
  120. if (uiatlas != null && uiatlas.replacement == this)
  121. {
  122. uiatlas.replacement = null;
  123. }
  124. if (this.mReplacement != null)
  125. {
  126. this.MarkAsChanged();
  127. }
  128. this.mReplacement = uiatlas;
  129. if (uiatlas != null)
  130. {
  131. this.material = null;
  132. }
  133. this.MarkAsChanged();
  134. }
  135. }
  136. }
  137. public UISpriteData GetSprite(string name)
  138. {
  139. if (this.mReplacement != null)
  140. {
  141. return this.mReplacement.GetSprite(name);
  142. }
  143. if (!string.IsNullOrEmpty(name))
  144. {
  145. if (this.mSprites.Count == 0)
  146. {
  147. this.Upgrade();
  148. }
  149. if (this.mSprites.Count == 0)
  150. {
  151. return null;
  152. }
  153. if (this.mSpriteIndices.Count != this.mSprites.Count)
  154. {
  155. this.MarkSpriteListAsChanged();
  156. }
  157. int num;
  158. if (this.mSpriteIndices.TryGetValue(name, out num))
  159. {
  160. if (num > -1 && num < this.mSprites.Count)
  161. {
  162. return this.mSprites[num];
  163. }
  164. this.MarkSpriteListAsChanged();
  165. return (!this.mSpriteIndices.TryGetValue(name, out num)) ? null : this.mSprites[num];
  166. }
  167. else
  168. {
  169. int i = 0;
  170. int count = this.mSprites.Count;
  171. while (i < count)
  172. {
  173. UISpriteData uispriteData = this.mSprites[i];
  174. if (!string.IsNullOrEmpty(uispriteData.name) && name == uispriteData.name)
  175. {
  176. this.MarkSpriteListAsChanged();
  177. return uispriteData;
  178. }
  179. i++;
  180. }
  181. }
  182. }
  183. return null;
  184. }
  185. public string GetRandomSprite(string startsWith)
  186. {
  187. if (this.GetSprite(startsWith) == null)
  188. {
  189. List<UISpriteData> spriteList = this.spriteList;
  190. List<string> list = new List<string>();
  191. foreach (UISpriteData uispriteData in spriteList)
  192. {
  193. if (uispriteData.name.StartsWith(startsWith))
  194. {
  195. list.Add(uispriteData.name);
  196. }
  197. }
  198. return (list.Count <= 0) ? null : list[UnityEngine.Random.Range(0, list.Count)];
  199. }
  200. return startsWith;
  201. }
  202. public void MarkSpriteListAsChanged()
  203. {
  204. this.mSpriteIndices.Clear();
  205. int i = 0;
  206. int count = this.mSprites.Count;
  207. while (i < count)
  208. {
  209. this.mSpriteIndices[this.mSprites[i].name] = i;
  210. i++;
  211. }
  212. }
  213. public void SortAlphabetically()
  214. {
  215. this.mSprites.Sort((UISpriteData s1, UISpriteData s2) => s1.name.CompareTo(s2.name));
  216. }
  217. public BetterList<string> GetListOfSprites()
  218. {
  219. if (this.mReplacement != null)
  220. {
  221. return this.mReplacement.GetListOfSprites();
  222. }
  223. if (this.mSprites.Count == 0)
  224. {
  225. this.Upgrade();
  226. }
  227. BetterList<string> betterList = new BetterList<string>();
  228. int i = 0;
  229. int count = this.mSprites.Count;
  230. while (i < count)
  231. {
  232. UISpriteData uispriteData = this.mSprites[i];
  233. if (uispriteData != null && !string.IsNullOrEmpty(uispriteData.name))
  234. {
  235. betterList.Add(uispriteData.name);
  236. }
  237. i++;
  238. }
  239. return betterList;
  240. }
  241. public BetterList<string> GetListOfSprites(string match)
  242. {
  243. if (this.mReplacement)
  244. {
  245. return this.mReplacement.GetListOfSprites(match);
  246. }
  247. if (string.IsNullOrEmpty(match))
  248. {
  249. return this.GetListOfSprites();
  250. }
  251. if (this.mSprites.Count == 0)
  252. {
  253. this.Upgrade();
  254. }
  255. BetterList<string> betterList = new BetterList<string>();
  256. int i = 0;
  257. int count = this.mSprites.Count;
  258. while (i < count)
  259. {
  260. UISpriteData uispriteData = this.mSprites[i];
  261. if (uispriteData != null && !string.IsNullOrEmpty(uispriteData.name) && string.Equals(match, uispriteData.name, StringComparison.OrdinalIgnoreCase))
  262. {
  263. betterList.Add(uispriteData.name);
  264. return betterList;
  265. }
  266. i++;
  267. }
  268. string[] array = match.Split(new char[]
  269. {
  270. ' '
  271. }, StringSplitOptions.RemoveEmptyEntries);
  272. for (int j = 0; j < array.Length; j++)
  273. {
  274. array[j] = array[j].ToLower();
  275. }
  276. int k = 0;
  277. int count2 = this.mSprites.Count;
  278. while (k < count2)
  279. {
  280. UISpriteData uispriteData2 = this.mSprites[k];
  281. if (uispriteData2 != null && !string.IsNullOrEmpty(uispriteData2.name))
  282. {
  283. string text = uispriteData2.name.ToLower();
  284. int num = 0;
  285. for (int l = 0; l < array.Length; l++)
  286. {
  287. if (text.Contains(array[l]))
  288. {
  289. num++;
  290. }
  291. }
  292. if (num == array.Length)
  293. {
  294. betterList.Add(uispriteData2.name);
  295. }
  296. }
  297. k++;
  298. }
  299. return betterList;
  300. }
  301. private bool References(UIAtlas atlas)
  302. {
  303. return !(atlas == null) && (atlas == this || (this.mReplacement != null && this.mReplacement.References(atlas)));
  304. }
  305. public static bool CheckIfRelated(UIAtlas a, UIAtlas b)
  306. {
  307. return !(a == null) && !(b == null) && (a == b || a.References(b) || b.References(a));
  308. }
  309. public void MarkAsChanged()
  310. {
  311. if (this.mReplacement != null)
  312. {
  313. this.mReplacement.MarkAsChanged();
  314. }
  315. UISprite[] array = NGUITools.FindActive<UISprite>();
  316. int i = 0;
  317. int num = array.Length;
  318. while (i < num)
  319. {
  320. UISprite uisprite = array[i];
  321. if (UIAtlas.CheckIfRelated(this, uisprite.atlas))
  322. {
  323. UIAtlas atlas = uisprite.atlas;
  324. uisprite.atlas = null;
  325. uisprite.atlas = atlas;
  326. }
  327. i++;
  328. }
  329. UIFont[] array2 = Resources.FindObjectsOfTypeAll(typeof(UIFont)) as UIFont[];
  330. int j = 0;
  331. int num2 = array2.Length;
  332. while (j < num2)
  333. {
  334. UIFont uifont = array2[j];
  335. if (UIAtlas.CheckIfRelated(this, uifont.atlas))
  336. {
  337. UIAtlas atlas2 = uifont.atlas;
  338. uifont.atlas = null;
  339. uifont.atlas = atlas2;
  340. }
  341. j++;
  342. }
  343. UILabel[] array3 = NGUITools.FindActive<UILabel>();
  344. int k = 0;
  345. int num3 = array3.Length;
  346. while (k < num3)
  347. {
  348. UILabel uilabel = array3[k];
  349. if (uilabel.bitmapFont != null && UIAtlas.CheckIfRelated(this, uilabel.bitmapFont.atlas))
  350. {
  351. UIFont bitmapFont = uilabel.bitmapFont;
  352. uilabel.bitmapFont = null;
  353. uilabel.bitmapFont = bitmapFont;
  354. }
  355. k++;
  356. }
  357. }
  358. private bool Upgrade()
  359. {
  360. if (this.mReplacement)
  361. {
  362. return this.mReplacement.Upgrade();
  363. }
  364. if (this.mSprites.Count == 0 && this.sprites.Count > 0 && this.material)
  365. {
  366. Texture mainTexture = this.material.mainTexture;
  367. int width = (!(mainTexture != null)) ? 512 : mainTexture.width;
  368. int height = (!(mainTexture != null)) ? 512 : mainTexture.height;
  369. for (int i = 0; i < this.sprites.Count; i++)
  370. {
  371. UIAtlas.Sprite sprite = this.sprites[i];
  372. Rect outer = sprite.outer;
  373. Rect inner = sprite.inner;
  374. if (this.mCoordinates == UIAtlas.Coordinates.TexCoords)
  375. {
  376. NGUIMath.ConvertToPixels(outer, width, height, true);
  377. NGUIMath.ConvertToPixels(inner, width, height, true);
  378. }
  379. UISpriteData uispriteData = new UISpriteData();
  380. uispriteData.name = sprite.name;
  381. uispriteData.x = Mathf.RoundToInt(outer.xMin);
  382. uispriteData.y = Mathf.RoundToInt(outer.yMin);
  383. uispriteData.width = Mathf.RoundToInt(outer.width);
  384. uispriteData.height = Mathf.RoundToInt(outer.height);
  385. uispriteData.paddingLeft = Mathf.RoundToInt(sprite.paddingLeft * outer.width);
  386. uispriteData.paddingRight = Mathf.RoundToInt(sprite.paddingRight * outer.width);
  387. uispriteData.paddingBottom = Mathf.RoundToInt(sprite.paddingBottom * outer.height);
  388. uispriteData.paddingTop = Mathf.RoundToInt(sprite.paddingTop * outer.height);
  389. uispriteData.borderLeft = Mathf.RoundToInt(inner.xMin - outer.xMin);
  390. uispriteData.borderRight = Mathf.RoundToInt(outer.xMax - inner.xMax);
  391. uispriteData.borderBottom = Mathf.RoundToInt(outer.yMax - inner.yMax);
  392. uispriteData.borderTop = Mathf.RoundToInt(inner.yMin - outer.yMin);
  393. this.mSprites.Add(uispriteData);
  394. }
  395. this.sprites.Clear();
  396. return true;
  397. }
  398. return false;
  399. }
  400. [HideInInspector]
  401. [SerializeField]
  402. private Material material;
  403. [HideInInspector]
  404. [SerializeField]
  405. private List<UISpriteData> mSprites = new List<UISpriteData>();
  406. [HideInInspector]
  407. [SerializeField]
  408. private float mPixelSize = 1f;
  409. [HideInInspector]
  410. [SerializeField]
  411. private UIAtlas mReplacement;
  412. [HideInInspector]
  413. [SerializeField]
  414. private UIAtlas.Coordinates mCoordinates;
  415. [HideInInspector]
  416. [SerializeField]
  417. private List<UIAtlas.Sprite> sprites = new List<UIAtlas.Sprite>();
  418. private int mPMA = -1;
  419. private Dictionary<string, int> mSpriteIndices = new Dictionary<string, int>();
  420. [Serializable]
  421. private class Sprite
  422. {
  423. public bool hasPadding
  424. {
  425. get
  426. {
  427. return this.paddingLeft != 0f || this.paddingRight != 0f || this.paddingTop != 0f || this.paddingBottom != 0f;
  428. }
  429. }
  430. public string name = "Unity Bug";
  431. public Rect outer = new Rect(0f, 0f, 1f, 1f);
  432. public Rect inner = new Rect(0f, 0f, 1f, 1f);
  433. public bool rotated;
  434. public float paddingLeft;
  435. public float paddingRight;
  436. public float paddingTop;
  437. public float paddingBottom;
  438. }
  439. private enum Coordinates
  440. {
  441. Pixels,
  442. TexCoords
  443. }
  444. }