MyImage.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Fort23.UTool;
  6. using UnityEngine.Experimental.Rendering;
  7. using UnityEngine.Serialization;
  8. using UnityEngine.U2D;
  9. using UnityEngine.UI.PackgTool;
  10. namespace UnityEngine.UI
  11. {
  12. /// <summary>
  13. /// Image is a textured element in the UI hierarchy.
  14. /// </summary>
  15. [RequireComponent(typeof(CanvasRenderer))]
  16. [AddComponentMenu("UI/MyImage", 11)]
  17. /// <summary>
  18. /// Displays a Sprite inside the UI System.
  19. /// </summary>
  20. public class MyImage : Image
  21. {
  22. public string icon_name
  23. {
  24. get { return _icon_name; }
  25. set
  26. {
  27. _icon_name = value;
  28. ReashUI();
  29. }
  30. }
  31. // private Sprite _packSprite;
  32. //
  33. // public Sprite GetPackSprite
  34. // {
  35. // get { return _packSprite; }
  36. // }
  37. [SerializeField] private string _icon_name;
  38. [SerializeField] public bool isNotLoadDeftIcon;
  39. [SerializeField] public SpriteAtlas CurrSpriteAtlas;
  40. [SerializeField] public PackInfo packInfo;
  41. private UILoadSpriteHand _uiLoadSpriteHand;
  42. public System.Action onSpriteAlter;
  43. protected override void Awake()
  44. {
  45. if (Application.isPlaying && isNotLoadDeftIcon)
  46. {
  47. return;
  48. }
  49. if (!string.IsNullOrEmpty(_icon_name))
  50. {
  51. icon_name = _icon_name;
  52. }
  53. }
  54. protected override void OnDestroy()
  55. {
  56. if (_uiLoadSpriteHand != null)
  57. {
  58. _uiLoadSpriteHand.ReleaseUI();
  59. _uiLoadSpriteHand = null;
  60. }
  61. base.OnDestroy();
  62. }
  63. public void ReashUI()
  64. {
  65. if (CurrSpriteAtlas != null && !string.IsNullOrEmpty(_icon_name))
  66. {
  67. Sprite loadSprite = CurrSpriteAtlas.GetSprite(_icon_name);
  68. if (loadSprite == null)
  69. {
  70. if (Application.isPlaying)
  71. {
  72. SpriteAtlas spriteAtlas = UGUIPackManager.Instance.GetSpriteAtlas(_icon_name);
  73. if (spriteAtlas != null)
  74. {
  75. loadSprite = spriteAtlas.GetSprite(_icon_name);
  76. }
  77. if (loadSprite == null)
  78. {
  79. enabled = false;
  80. UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
  81. {
  82. enabled = true;
  83. if (Application.isPlaying)
  84. {
  85. if (_uiLoadSpriteHand != null)
  86. {
  87. _uiLoadSpriteHand.ReleaseUI();
  88. }
  89. }
  90. _uiLoadSpriteHand = sprite1;
  91. if (sprite1 == null)
  92. {
  93. sprite = null;
  94. }
  95. else
  96. {
  97. sprite = sprite1.GetSprite();
  98. }
  99. });
  100. }
  101. else
  102. {
  103. sprite = loadSprite;
  104. }
  105. }
  106. }
  107. else
  108. {
  109. sprite = loadSprite;
  110. }
  111. }
  112. else if (!string.IsNullOrEmpty(_icon_name) && sprite == null)
  113. {
  114. if (Application.isPlaying)
  115. {
  116. enabled = false;
  117. UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
  118. {
  119. enabled = true;
  120. if (_uiLoadSpriteHand != null)
  121. {
  122. _uiLoadSpriteHand.ReleaseUI();
  123. }
  124. _uiLoadSpriteHand = sprite1;
  125. if (sprite1 == null)
  126. {
  127. sprite = null;
  128. }
  129. else
  130. {
  131. sprite = sprite1.GetSprite();
  132. }
  133. });
  134. }
  135. }
  136. else if (!string.IsNullOrEmpty(_icon_name))
  137. {
  138. if (Application.isPlaying)
  139. {
  140. enabled = false;
  141. UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
  142. {
  143. enabled = true;
  144. if (sprite1 != null)
  145. {
  146. Sprite sp = sprite1.GetSprite();
  147. if (sp != null)
  148. {
  149. if (!sp.name.Equals(_icon_name))
  150. {
  151. sprite1.ReleaseUI();
  152. return;
  153. }
  154. }
  155. }
  156. if (_uiLoadSpriteHand != null)
  157. {
  158. _uiLoadSpriteHand.ReleaseUI();
  159. }
  160. _uiLoadSpriteHand = sprite1;
  161. if (sprite1 == null)
  162. {
  163. sprite = null;
  164. }
  165. else
  166. {
  167. sprite = sprite1.GetSprite();
  168. }
  169. });
  170. }
  171. }
  172. }
  173. public override void GraphicUpdateComplete()
  174. {
  175. SetPack();
  176. }
  177. private void SetPack()
  178. {
  179. #if UNITY_EDITOR
  180. if (!Application.isPlaying)
  181. {
  182. bool isoK = false;
  183. if (packInfo != null && CurrSpriteAtlas != null)
  184. {
  185. string assetName = icon_name;
  186. if (string.IsNullOrEmpty(icon_name) && sprite != null)
  187. {
  188. assetName = sprite.name;
  189. }
  190. isoK = CurrSpriteAtlas.GetSprite(assetName) != null;
  191. if (!isoK) //没在在图集里面找到图片了,需要去其他地方查找(删除图集)
  192. {
  193. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(assetName);
  194. if (newAtlas != null)
  195. {
  196. CurrSpriteAtlas = newAtlas;
  197. icon_name = assetName;
  198. }
  199. else
  200. {
  201. sprite = null;
  202. }
  203. }
  204. else if (string.IsNullOrEmpty(icon_name) || sprite == null)
  205. {
  206. icon_name = assetName;
  207. }
  208. }
  209. else if (CurrSpriteAtlas == null && packInfo != null && !string.IsNullOrEmpty(packInfo.packgJsonPath) &&
  210. sprite != null) //没有图集 但是由功能管理器添加的UI (添加图集)
  211. {
  212. string name = sprite.name;
  213. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(name);
  214. if (newAtlas != null)
  215. {
  216. CurrSpriteAtlas = newAtlas;
  217. icon_name = _icon_name;
  218. }
  219. }
  220. }
  221. #endif
  222. }
  223. }
  224. }