MyImage.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Fort23.Core;
  7. using Fort23.Mono;
  8. using Fort23.UTool;
  9. #if UNITY_EDITOR
  10. using System.IO;
  11. using UnityEditor;
  12. #endif
  13. using UnityEngine.Experimental.Rendering;
  14. using UnityEngine.Serialization;
  15. using UnityEngine.U2D;
  16. using UnityEngine.UI.PackgTool;
  17. namespace UnityEngine.UI
  18. {
  19. /// <summary>
  20. /// Image is a textured element in the UI hierarchy.
  21. /// </summary>
  22. [RequireComponent(typeof(CanvasRenderer))]
  23. [AddComponentMenu("GameObject/UI/MyImage", 12)]
  24. /// <summary>
  25. /// Displays a Sprite inside the UI System.
  26. /// </summary>
  27. public class MyImage : Image
  28. {
  29. public string icon_name
  30. {
  31. get { return _icon_name; }
  32. set
  33. {
  34. _icon_name = value;
  35. ReashUI();
  36. }
  37. }
  38. #if UNITY_EDITOR
  39. [MenuItem("GameObject/UI/图集UI", false, 1)]
  40. static public void AddImage(MenuCommand menuCommand)
  41. {
  42. GameObject parent = menuCommand.context as GameObject;
  43. if (parent == null)
  44. {
  45. return;
  46. }
  47. GameObject go = new GameObject("myImage");
  48. go.AddComponent<MyImage>();
  49. go.transform.SetParent(parent.transform);
  50. Selection.activeGameObject = go;
  51. }
  52. #endif
  53. // private Sprite _packSprite;
  54. //
  55. // public Sprite GetPackSprite
  56. // {
  57. // get { return _packSprite; }
  58. // }
  59. [SerializeField] private string _icon_name;
  60. [SerializeField] public bool isNotLoadDeftIcon;
  61. [SerializeField] public SpriteAtlas CurrSpriteAtlas;
  62. #if UNITY_EDITOR
  63. [SerializeField] public PackInfo packInfo;
  64. #endif
  65. private UILoadSpriteHand _uiLoadSpriteHand;
  66. public System.Action onSpriteAlter;
  67. protected override void Awake()
  68. {
  69. if (Application.isPlaying && isNotLoadDeftIcon)
  70. {
  71. return;
  72. }
  73. if (!string.IsNullOrEmpty(_icon_name))
  74. {
  75. icon_name = _icon_name;
  76. }
  77. }
  78. protected override void OnDestroy()
  79. {
  80. if (_uiLoadSpriteHand != null)
  81. {
  82. _uiLoadSpriteHand.ReleaseUI();
  83. _uiLoadSpriteHand = null;
  84. }
  85. base.OnDestroy();
  86. }
  87. public void ReashUI()
  88. {
  89. if (CurrSpriteAtlas != null && !string.IsNullOrEmpty(_icon_name))
  90. {
  91. Sprite loadSprite = CurrSpriteAtlas.GetSprite(_icon_name);
  92. if (loadSprite == null)
  93. {
  94. if (Application.isPlaying)
  95. {
  96. SpriteAtlas spriteAtlas = UGUIPackManager.Instance.GetSpriteAtlas(_icon_name);
  97. if (spriteAtlas != null)
  98. {
  99. loadSprite = spriteAtlas.GetSprite(_icon_name);
  100. }
  101. if (loadSprite == null)
  102. {
  103. enabled = false;
  104. UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
  105. {
  106. enabled = true;
  107. if (Application.isPlaying)
  108. {
  109. if (_uiLoadSpriteHand != null)
  110. {
  111. _uiLoadSpriteHand.ReleaseUI();
  112. }
  113. }
  114. _uiLoadSpriteHand = sprite1;
  115. if (sprite1 == null)
  116. {
  117. SetSprite(null);
  118. }
  119. else
  120. {
  121. SetSprite(sprite1.GetSprite());
  122. }
  123. });
  124. }
  125. else
  126. {
  127. SetSprite(loadSprite);
  128. }
  129. }
  130. }
  131. else
  132. {
  133. SetSprite(loadSprite);
  134. }
  135. }
  136. else if (!string.IsNullOrEmpty(_icon_name) && sprite == null)
  137. {
  138. if (Application.isPlaying)
  139. {
  140. enabled = false;
  141. UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
  142. {
  143. enabled = true;
  144. if (_uiLoadSpriteHand != null)
  145. {
  146. _uiLoadSpriteHand.ReleaseUI();
  147. }
  148. _uiLoadSpriteHand = sprite1;
  149. if (sprite1 == null)
  150. {
  151. SetSprite(null);
  152. }
  153. else
  154. {
  155. SetSprite(sprite1.GetSprite());
  156. }
  157. });
  158. }
  159. }
  160. else if (!string.IsNullOrEmpty(_icon_name))
  161. {
  162. if (Application.isPlaying)
  163. {
  164. enabled = false;
  165. UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
  166. {
  167. enabled = true;
  168. if (sprite1 != null)
  169. {
  170. Sprite sp = sprite1.GetSprite();
  171. if (sp != null)
  172. {
  173. if (!sp.name.Equals(_icon_name))
  174. {
  175. sprite1.ReleaseUI();
  176. return;
  177. }
  178. }
  179. }
  180. if (_uiLoadSpriteHand != null)
  181. {
  182. _uiLoadSpriteHand.ReleaseUI();
  183. }
  184. _uiLoadSpriteHand = sprite1;
  185. if (sprite1 == null)
  186. {
  187. SetSprite(null);
  188. }
  189. else
  190. {
  191. SetSprite(sprite1.GetSprite());
  192. }
  193. });
  194. }
  195. }
  196. }
  197. private void SetSprite(Sprite sprite)
  198. {
  199. this.sprite = sprite;
  200. if (sprite != null)
  201. {
  202. onSpriteAlter?.Invoke();
  203. }
  204. }
  205. /// <summary>
  206. /// 是否置灰
  207. /// </summary>
  208. public bool IsGray
  209. {
  210. get { return _isGray; }
  211. set
  212. {
  213. if (!_isGray.Equals(value))
  214. {
  215. _isGray = value;
  216. if (_isGray)
  217. {
  218. base.material = UIManager.Instance.uiGray;
  219. }
  220. else
  221. {
  222. if (base.material != null)
  223. {
  224. base.material = null;
  225. }
  226. base.material = defaultMaterial;
  227. }
  228. }
  229. }
  230. }
  231. private bool _isGray;
  232. public override void GraphicUpdateComplete()
  233. {
  234. SetPack();
  235. }
  236. #if UNITY_EDITOR
  237. [ContextMenu("重新设置图片")]
  238. public void SetPackEditor()
  239. {
  240. if (packInfo != null && CurrSpriteAtlas != null)
  241. {
  242. string assetName = icon_name;
  243. if (string.IsNullOrEmpty(icon_name) && sprite != null)
  244. {
  245. assetName = sprite.name;
  246. }
  247. var loadSprite = CurrSpriteAtlas.GetSprite(assetName);
  248. bool isoK = loadSprite != null;
  249. if (isoK)
  250. {
  251. SetSprite(loadSprite);
  252. }
  253. }
  254. }
  255. [ContextMenu("自动匹配图集")]
  256. public void CompareSpritesInAtlas()
  257. {
  258. if (sprite == null)
  259. return;
  260. string targetPath = AssetDatabase.GetAssetPath(sprite);
  261. if (string.IsNullOrEmpty(targetPath))
  262. {
  263. LogTool.Error("无法获取目标 Sprite 的源文件路径");
  264. return;
  265. }
  266. string relativePath = targetPath.Substring("Assets/".Length);
  267. string absolutePath = Path.Combine(Application.dataPath, relativePath);
  268. string targetMD5 = MD5Helper.FileMD5(absolutePath);
  269. foreach (var allPackgInfo in UGUICacheInfo.uguiPackDB.allPackgInfos)
  270. {
  271. string path = Application.dataPath + allPackgInfo.PackageJsonPath;
  272. string jsonPath = File.ReadAllText(path);
  273. TextrueJson textrueJson = JsonUtility.FromJson<TextrueJson>(jsonPath);
  274. for (int i = 0; i < textrueJson.newTextureJson.Count; i++)
  275. {
  276. string texturePath = "Art/UIAssets" + textrueJson.newTextureJson[i].filePath;
  277. texturePath = Path.Combine(Application.dataPath, texturePath);
  278. string spriteMD5 = MD5Helper.FileMD5(texturePath);
  279. if (spriteMD5 == targetMD5)
  280. {
  281. LogTool.Log($"找到匹配的 Sprite: {textrueJson.newTextureJson[i].textrueName},所在图集: {allPackgInfo.packName},源文件: {targetPath}");
  282. packInfo = allPackgInfo;
  283. CurrSpriteAtlas = UGUICacheInfo.GetSpriteAtlas(allPackgInfo.packName);
  284. icon_name = textrueJson.newTextureJson[i].textrueName;
  285. ReashUI();
  286. return;
  287. }
  288. }
  289. }
  290. LogTool.Log("未找到任何匹配的 Sprite");
  291. }
  292. #endif
  293. private void SetPack()
  294. {
  295. #if UNITY_EDITOR
  296. if (!Application.isPlaying)
  297. {
  298. bool isoK = false;
  299. if (packInfo != null && CurrSpriteAtlas != null)
  300. {
  301. string assetName = icon_name;
  302. if (string.IsNullOrEmpty(icon_name) && sprite != null)
  303. {
  304. assetName = sprite.name;
  305. }
  306. isoK = CurrSpriteAtlas.GetSprite(assetName) != null;
  307. if (!isoK) //没在在图集里面找到图片了,需要去其他地方查找(删除图集)
  308. {
  309. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(assetName);
  310. if (newAtlas != null)
  311. {
  312. CurrSpriteAtlas = newAtlas;
  313. icon_name = assetName;
  314. }
  315. else
  316. {
  317. sprite = null;
  318. }
  319. }
  320. else if (string.IsNullOrEmpty(icon_name) || sprite == null)
  321. {
  322. icon_name = assetName;
  323. }
  324. }
  325. else if (CurrSpriteAtlas == null && packInfo != null && !string.IsNullOrEmpty(packInfo.packgJsonPath) &&
  326. sprite != null) //没有图集 但是由功能管理器添加的UI (添加图集)
  327. {
  328. string name = sprite.name;
  329. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(name);
  330. if (newAtlas != null)
  331. {
  332. CurrSpriteAtlas = newAtlas;
  333. icon_name = _icon_name;
  334. }
  335. }
  336. }
  337. #endif
  338. }
  339. public enum GradientDirection
  340. {
  341. Vertical,
  342. Horizontal
  343. }
  344. [SerializeField] private bool useGradient = false;
  345. [SerializeField] private Color topOrLeftColor = Color.white;
  346. [SerializeField] private Color bottomOrRightColor = Color.black;
  347. [SerializeField] private GradientDirection direction = GradientDirection.Vertical;
  348. public bool UseGradient
  349. {
  350. get => useGradient;
  351. set
  352. {
  353. useGradient = value;
  354. SetVerticesDirty();
  355. }
  356. }
  357. public Color TopOrLeftColor
  358. {
  359. get => topOrLeftColor;
  360. set
  361. {
  362. topOrLeftColor = value;
  363. SetVerticesDirty();
  364. }
  365. }
  366. public Color BottomOrRightColor
  367. {
  368. get => bottomOrRightColor;
  369. set
  370. {
  371. bottomOrRightColor = value;
  372. SetVerticesDirty();
  373. }
  374. }
  375. public GradientDirection Direction
  376. {
  377. get => direction;
  378. set
  379. {
  380. direction = value;
  381. SetVerticesDirty();
  382. }
  383. }
  384. protected override void OnPopulateMesh(VertexHelper vh)
  385. {
  386. base.OnPopulateMesh(vh);
  387. if (!useGradient || vh.currentVertCount == 0)
  388. return;
  389. List<UIVertex> verts = new List<UIVertex>();
  390. vh.GetUIVertexStream(verts);
  391. for (int i = 0; i < verts.Count; i += 6)
  392. {
  393. ApplyGradientToQuad(verts, i);
  394. }
  395. vh.Clear();
  396. vh.AddUIVertexTriangleStream(verts);
  397. }
  398. private void ApplyGradientToQuad(List<UIVertex> verts, int startIndex)
  399. {
  400. if (startIndex + 5 >= verts.Count)
  401. return;
  402. float top = verts[startIndex].position.y;
  403. float bottom = verts[startIndex].position.y;
  404. float left = verts[startIndex].position.x;
  405. float right = verts[startIndex].position.x;
  406. for (int i = 1; i < 6; i++)
  407. {
  408. var pos = verts[startIndex + i].position;
  409. top = Mathf.Max(top, pos.y);
  410. bottom = Mathf.Min(bottom, pos.y);
  411. left = Mathf.Min(left, pos.x);
  412. right = Mathf.Max(right, pos.x);
  413. }
  414. for (int i = 0; i < 6; i++)
  415. {
  416. var vert = verts[startIndex + i];
  417. var pos = vert.position;
  418. float t = direction == GradientDirection.Vertical
  419. ? Mathf.InverseLerp(bottom, top, pos.y)
  420. : Mathf.InverseLerp(left, right, pos.x);
  421. Color gradientColor = Color.Lerp(bottomOrRightColor, topOrLeftColor, t);
  422. gradientColor.r *= color.r;
  423. gradientColor.g *= color.g;
  424. gradientColor.b *= color.b;
  425. gradientColor.a *= color.a;
  426. vert.color = gradientColor;
  427. verts[startIndex + i] = vert;
  428. }
  429. }
  430. }
  431. }