MyImage.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. SetSprite(null);
  94. }
  95. else
  96. {
  97. SetSprite(sprite1.GetSprite());
  98. }
  99. });
  100. }
  101. else
  102. {
  103. SetSprite(loadSprite);
  104. }
  105. }
  106. }
  107. else
  108. {
  109. SetSprite( 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. SetSprite(null);
  128. }
  129. else
  130. {
  131. SetSprite( 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. SetSprite( null);
  164. }
  165. else
  166. {
  167. SetSprite(sprite1.GetSprite());
  168. }
  169. });
  170. }
  171. }
  172. }
  173. private void SetSprite(Sprite sprite)
  174. {
  175. this.sprite = sprite;
  176. if (sprite != null)
  177. {
  178. onSpriteAlter?.Invoke();
  179. }
  180. }
  181. public override void GraphicUpdateComplete()
  182. {
  183. SetPack();
  184. }
  185. private void SetPack()
  186. {
  187. #if UNITY_EDITOR
  188. if (!Application.isPlaying)
  189. {
  190. bool isoK = false;
  191. if (packInfo != null && CurrSpriteAtlas != null)
  192. {
  193. string assetName = icon_name;
  194. if (string.IsNullOrEmpty(icon_name) && sprite != null)
  195. {
  196. assetName = sprite.name;
  197. }
  198. isoK = CurrSpriteAtlas.GetSprite(assetName) != null;
  199. if (!isoK) //没在在图集里面找到图片了,需要去其他地方查找(删除图集)
  200. {
  201. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(assetName);
  202. if (newAtlas != null)
  203. {
  204. CurrSpriteAtlas = newAtlas;
  205. icon_name = assetName;
  206. }
  207. else
  208. {
  209. sprite = null;
  210. }
  211. }
  212. else if (string.IsNullOrEmpty(icon_name) || sprite == null)
  213. {
  214. icon_name = assetName;
  215. }
  216. }
  217. else if (CurrSpriteAtlas == null && packInfo != null && !string.IsNullOrEmpty(packInfo.packgJsonPath) &&
  218. sprite != null) //没有图集 但是由功能管理器添加的UI (添加图集)
  219. {
  220. string name = sprite.name;
  221. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(name);
  222. if (newAtlas != null)
  223. {
  224. CurrSpriteAtlas = newAtlas;
  225. icon_name = _icon_name;
  226. }
  227. }
  228. }
  229. #endif
  230. }
  231. }
  232. }