MyImage.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. public enum GradientDirection
  280. {
  281. Vertical,
  282. Horizontal
  283. }
  284. [SerializeField] private bool useGradient = false;
  285. [SerializeField] private Color topOrLeftColor = Color.white;
  286. [SerializeField] private Color bottomOrRightColor = Color.black;
  287. [SerializeField] private GradientDirection direction = GradientDirection.Vertical;
  288. public bool UseGradient
  289. {
  290. get => useGradient;
  291. set
  292. {
  293. useGradient = value;
  294. SetVerticesDirty();
  295. }
  296. }
  297. public Color TopOrLeftColor
  298. {
  299. get => topOrLeftColor;
  300. set
  301. {
  302. topOrLeftColor = value;
  303. SetVerticesDirty();
  304. }
  305. }
  306. public Color BottomOrRightColor
  307. {
  308. get => bottomOrRightColor;
  309. set
  310. {
  311. bottomOrRightColor = value;
  312. SetVerticesDirty();
  313. }
  314. }
  315. public GradientDirection Direction
  316. {
  317. get => direction;
  318. set
  319. {
  320. direction = value;
  321. SetVerticesDirty();
  322. }
  323. }
  324. protected override void OnPopulateMesh(VertexHelper vh)
  325. {
  326. base.OnPopulateMesh(vh);
  327. if (!useGradient || vh.currentVertCount == 0)
  328. return;
  329. List<UIVertex> verts = new List<UIVertex>();
  330. vh.GetUIVertexStream(verts);
  331. for (int i = 0; i < verts.Count; i += 6)
  332. {
  333. ApplyGradientToQuad(verts, i);
  334. }
  335. vh.Clear();
  336. vh.AddUIVertexTriangleStream(verts);
  337. }
  338. private void ApplyGradientToQuad(List<UIVertex> verts, int startIndex)
  339. {
  340. if (startIndex + 5 >= verts.Count)
  341. return;
  342. float top = verts[startIndex].position.y;
  343. float bottom = verts[startIndex].position.y;
  344. float left = verts[startIndex].position.x;
  345. float right = verts[startIndex].position.x;
  346. for (int i = 1; i < 6; i++)
  347. {
  348. var pos = verts[startIndex + i].position;
  349. top = Mathf.Max(top, pos.y);
  350. bottom = Mathf.Min(bottom, pos.y);
  351. left = Mathf.Min(left, pos.x);
  352. right = Mathf.Max(right, pos.x);
  353. }
  354. for (int i = 0; i < 6; i++)
  355. {
  356. var vert = verts[startIndex + i];
  357. var pos = vert.position;
  358. float t = direction == GradientDirection.Vertical
  359. ? Mathf.InverseLerp(bottom, top, pos.y)
  360. : Mathf.InverseLerp(left, right, pos.x);
  361. Color gradientColor = Color.Lerp(bottomOrRightColor, topOrLeftColor, t);
  362. gradientColor.r *= color.r;
  363. gradientColor.g *= color.g;
  364. gradientColor.b *= color.b;
  365. gradientColor.a *= color.a;
  366. vert.color = gradientColor;
  367. verts[startIndex + i] = vert;
  368. }
  369. }
  370. }
  371. }