MyImage.cs 9.4 KB

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