MyImage.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. [SerializeField] public PackInfo packInfo;
  60. private UILoadSpriteHand _uiLoadSpriteHand;
  61. public System.Action onSpriteAlter;
  62. protected override void Awake()
  63. {
  64. if (Application.isPlaying && isNotLoadDeftIcon)
  65. {
  66. return;
  67. }
  68. if (!string.IsNullOrEmpty(_icon_name))
  69. {
  70. icon_name = _icon_name;
  71. }
  72. }
  73. protected override void OnDestroy()
  74. {
  75. if (_uiLoadSpriteHand != null)
  76. {
  77. _uiLoadSpriteHand.ReleaseUI();
  78. _uiLoadSpriteHand = null;
  79. }
  80. base.OnDestroy();
  81. }
  82. public void ReashUI()
  83. {
  84. if (CurrSpriteAtlas != null && !string.IsNullOrEmpty(_icon_name))
  85. {
  86. Sprite loadSprite = CurrSpriteAtlas.GetSprite(_icon_name);
  87. if (loadSprite == null)
  88. {
  89. if (Application.isPlaying)
  90. {
  91. SpriteAtlas spriteAtlas = UGUIPackManager.Instance.GetSpriteAtlas(_icon_name);
  92. if (spriteAtlas != null)
  93. {
  94. loadSprite = spriteAtlas.GetSprite(_icon_name);
  95. }
  96. if (loadSprite == null)
  97. {
  98. enabled = false;
  99. UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
  100. {
  101. enabled = true;
  102. if (Application.isPlaying)
  103. {
  104. if (_uiLoadSpriteHand != null)
  105. {
  106. _uiLoadSpriteHand.ReleaseUI();
  107. }
  108. }
  109. _uiLoadSpriteHand = sprite1;
  110. if (sprite1 == null)
  111. {
  112. SetSprite(null);
  113. }
  114. else
  115. {
  116. SetSprite(sprite1.GetSprite());
  117. }
  118. });
  119. }
  120. else
  121. {
  122. SetSprite(loadSprite);
  123. }
  124. }
  125. }
  126. else
  127. {
  128. SetSprite(loadSprite);
  129. }
  130. }
  131. else if (!string.IsNullOrEmpty(_icon_name) && sprite == null)
  132. {
  133. if (Application.isPlaying)
  134. {
  135. enabled = false;
  136. UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
  137. {
  138. enabled = true;
  139. if (_uiLoadSpriteHand != null)
  140. {
  141. _uiLoadSpriteHand.ReleaseUI();
  142. }
  143. _uiLoadSpriteHand = sprite1;
  144. if (sprite1 == null)
  145. {
  146. SetSprite(null);
  147. }
  148. else
  149. {
  150. SetSprite(sprite1.GetSprite());
  151. }
  152. });
  153. }
  154. }
  155. else if (!string.IsNullOrEmpty(_icon_name))
  156. {
  157. if (Application.isPlaying)
  158. {
  159. enabled = false;
  160. UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
  161. {
  162. enabled = true;
  163. if (sprite1 != null)
  164. {
  165. Sprite sp = sprite1.GetSprite();
  166. if (sp != null)
  167. {
  168. if (!sp.name.Equals(_icon_name))
  169. {
  170. sprite1.ReleaseUI();
  171. return;
  172. }
  173. }
  174. }
  175. if (_uiLoadSpriteHand != null)
  176. {
  177. _uiLoadSpriteHand.ReleaseUI();
  178. }
  179. _uiLoadSpriteHand = sprite1;
  180. if (sprite1 == null)
  181. {
  182. SetSprite(null);
  183. }
  184. else
  185. {
  186. SetSprite(sprite1.GetSprite());
  187. }
  188. });
  189. }
  190. }
  191. }
  192. private void SetSprite(Sprite sprite)
  193. {
  194. this.sprite = sprite;
  195. if (sprite != null)
  196. {
  197. onSpriteAlter?.Invoke();
  198. }
  199. }
  200. /// <summary>
  201. /// 是否置灰
  202. /// </summary>
  203. public bool IsGray
  204. {
  205. get { return _isGray; }
  206. set
  207. {
  208. if (!_isGray.Equals(value))
  209. {
  210. _isGray = value;
  211. if (_isGray)
  212. {
  213. base.material = UIManager.Instance.uiGray;
  214. }
  215. else
  216. {
  217. if (base.material != null)
  218. {
  219. base.material = null;
  220. }
  221. base.material = defaultMaterial;
  222. }
  223. }
  224. }
  225. }
  226. private bool _isGray;
  227. public override void GraphicUpdateComplete()
  228. {
  229. SetPack();
  230. }
  231. private void SetPack()
  232. {
  233. #if UNITY_EDITOR
  234. if (!Application.isPlaying)
  235. {
  236. bool isoK = false;
  237. if (packInfo != null && CurrSpriteAtlas != null)
  238. {
  239. string assetName = icon_name;
  240. if (string.IsNullOrEmpty(icon_name) && sprite != null)
  241. {
  242. assetName = sprite.name;
  243. }
  244. isoK = CurrSpriteAtlas.GetSprite(assetName) != null;
  245. if (!isoK) //没在在图集里面找到图片了,需要去其他地方查找(删除图集)
  246. {
  247. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(assetName);
  248. if (newAtlas != null)
  249. {
  250. CurrSpriteAtlas = newAtlas;
  251. icon_name = assetName;
  252. }
  253. else
  254. {
  255. sprite = null;
  256. }
  257. }
  258. else if (string.IsNullOrEmpty(icon_name) || sprite == null)
  259. {
  260. icon_name = assetName;
  261. }
  262. }
  263. else if (CurrSpriteAtlas == null && packInfo != null && !string.IsNullOrEmpty(packInfo.packgJsonPath) &&
  264. sprite != null) //没有图集 但是由功能管理器添加的UI (添加图集)
  265. {
  266. string name = sprite.name;
  267. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(name);
  268. if (newAtlas != null)
  269. {
  270. CurrSpriteAtlas = newAtlas;
  271. icon_name = _icon_name;
  272. }
  273. }
  274. }
  275. #endif
  276. }
  277. }
  278. }