MyImage.cs 8.9 KB

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