MyImage.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 || CurrSpriteAtlas!= null || icon_name != "")
  259. return;
  260. string targetPath = AssetDatabase.GetAssetPath(sprite);
  261. if (string.IsNullOrEmpty(targetPath))
  262. {
  263. Debug.LogError("无法获取目标 Sprite 的源文件路径");
  264. return;
  265. }
  266. string targetMD5 = MD5Helper.FileMD5("D:/FB/XiuXianGame/" + targetPath);
  267. foreach (var allPackgInfo in UGUICacheInfo.uguiPackDB.allPackgInfos)
  268. {
  269. string path = Application.dataPath + allPackgInfo.PackageJsonPath;
  270. string jsonPath = File.ReadAllText(path);
  271. TextrueJson textrueJson = JsonUtility.FromJson<TextrueJson>(jsonPath);
  272. for (int i = 0; i < textrueJson.newTextureJson.Count; i++)
  273. {
  274. string texturePath = "/Art/UIAssets" + textrueJson.newTextureJson[i].filePath;
  275. string spriteMD5 = MD5Helper.FileMD5(Application.dataPath + texturePath);
  276. if (spriteMD5 == targetMD5)
  277. {
  278. LogTool.Log($"找到匹配的 Sprite: {textrueJson.newTextureJson[i].textrueName},所在图集: {allPackgInfo.packName},源文件: {targetPath}");
  279. packInfo = allPackgInfo;
  280. CurrSpriteAtlas = UGUICacheInfo.GetSpriteAtlas(allPackgInfo.packName);
  281. icon_name = textrueJson.newTextureJson[i].textrueName;
  282. ReashUI();
  283. return;
  284. }
  285. }
  286. }
  287. LogTool.Log("未找到任何匹配的 Sprite");
  288. }
  289. #endif
  290. private void SetPack()
  291. {
  292. #if UNITY_EDITOR
  293. if (!Application.isPlaying)
  294. {
  295. bool isoK = false;
  296. if (packInfo != null && CurrSpriteAtlas != null)
  297. {
  298. string assetName = icon_name;
  299. if (string.IsNullOrEmpty(icon_name) && sprite != null)
  300. {
  301. assetName = sprite.name;
  302. }
  303. isoK = CurrSpriteAtlas.GetSprite(assetName) != null;
  304. if (!isoK) //没在在图集里面找到图片了,需要去其他地方查找(删除图集)
  305. {
  306. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(assetName);
  307. if (newAtlas != null)
  308. {
  309. CurrSpriteAtlas = newAtlas;
  310. icon_name = assetName;
  311. }
  312. else
  313. {
  314. sprite = null;
  315. }
  316. }
  317. else if (string.IsNullOrEmpty(icon_name) || sprite == null)
  318. {
  319. icon_name = assetName;
  320. }
  321. }
  322. else if (CurrSpriteAtlas == null && packInfo != null && !string.IsNullOrEmpty(packInfo.packgJsonPath) &&
  323. sprite != null) //没有图集 但是由功能管理器添加的UI (添加图集)
  324. {
  325. string name = sprite.name;
  326. SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(name);
  327. if (newAtlas != null)
  328. {
  329. CurrSpriteAtlas = newAtlas;
  330. icon_name = _icon_name;
  331. }
  332. }
  333. }
  334. #endif
  335. }
  336. public enum GradientDirection
  337. {
  338. Vertical,
  339. Horizontal
  340. }
  341. [SerializeField] private bool useGradient = false;
  342. [SerializeField] private Color topOrLeftColor = Color.white;
  343. [SerializeField] private Color bottomOrRightColor = Color.black;
  344. [SerializeField] private GradientDirection direction = GradientDirection.Vertical;
  345. public bool UseGradient
  346. {
  347. get => useGradient;
  348. set
  349. {
  350. useGradient = value;
  351. SetVerticesDirty();
  352. }
  353. }
  354. public Color TopOrLeftColor
  355. {
  356. get => topOrLeftColor;
  357. set
  358. {
  359. topOrLeftColor = value;
  360. SetVerticesDirty();
  361. }
  362. }
  363. public Color BottomOrRightColor
  364. {
  365. get => bottomOrRightColor;
  366. set
  367. {
  368. bottomOrRightColor = value;
  369. SetVerticesDirty();
  370. }
  371. }
  372. public GradientDirection Direction
  373. {
  374. get => direction;
  375. set
  376. {
  377. direction = value;
  378. SetVerticesDirty();
  379. }
  380. }
  381. protected override void OnPopulateMesh(VertexHelper vh)
  382. {
  383. base.OnPopulateMesh(vh);
  384. if (!useGradient || vh.currentVertCount == 0)
  385. return;
  386. List<UIVertex> verts = new List<UIVertex>();
  387. vh.GetUIVertexStream(verts);
  388. for (int i = 0; i < verts.Count; i += 6)
  389. {
  390. ApplyGradientToQuad(verts, i);
  391. }
  392. vh.Clear();
  393. vh.AddUIVertexTriangleStream(verts);
  394. }
  395. private void ApplyGradientToQuad(List<UIVertex> verts, int startIndex)
  396. {
  397. if (startIndex + 5 >= verts.Count)
  398. return;
  399. float top = verts[startIndex].position.y;
  400. float bottom = verts[startIndex].position.y;
  401. float left = verts[startIndex].position.x;
  402. float right = verts[startIndex].position.x;
  403. for (int i = 1; i < 6; i++)
  404. {
  405. var pos = verts[startIndex + i].position;
  406. top = Mathf.Max(top, pos.y);
  407. bottom = Mathf.Min(bottom, pos.y);
  408. left = Mathf.Min(left, pos.x);
  409. right = Mathf.Max(right, pos.x);
  410. }
  411. for (int i = 0; i < 6; i++)
  412. {
  413. var vert = verts[startIndex + i];
  414. var pos = vert.position;
  415. float t = direction == GradientDirection.Vertical
  416. ? Mathf.InverseLerp(bottom, top, pos.y)
  417. : Mathf.InverseLerp(left, right, pos.x);
  418. Color gradientColor = Color.Lerp(bottomOrRightColor, topOrLeftColor, t);
  419. gradientColor.r *= color.r;
  420. gradientColor.g *= color.g;
  421. gradientColor.b *= color.b;
  422. gradientColor.a *= color.a;
  423. vert.color = gradientColor;
  424. verts[startIndex + i] = vert;
  425. }
  426. }
  427. }
  428. }