123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Fort23.Core;
- using Fort23.Mono;
- using Fort23.UTool;
- #if UNITY_EDITOR
- using System.IO;
- using UnityEditor;
- #endif
- using UnityEngine.Experimental.Rendering;
- using UnityEngine.Serialization;
- using UnityEngine.U2D;
- using UnityEngine.UI.PackgTool;
- namespace UnityEngine.UI
- {
- /// <summary>
- /// Image is a textured element in the UI hierarchy.
- /// </summary>
- [RequireComponent(typeof(CanvasRenderer))]
- [AddComponentMenu("GameObject/UI/MyImage", 12)]
- /// <summary>
- /// Displays a Sprite inside the UI System.
- /// </summary>
- public class MyImage : Image
- {
- public string icon_name
- {
- get { return _icon_name; }
- set
- {
- _icon_name = value;
- ReashUI();
- }
- }
- #if UNITY_EDITOR
- [MenuItem("GameObject/UI/图集UI", false, 1)]
- static public void AddImage(MenuCommand menuCommand)
- {
- GameObject parent = menuCommand.context as GameObject;
- if (parent == null)
- {
- return;
- }
- GameObject go = new GameObject("myImage");
- go.AddComponent<MyImage>();
- go.transform.SetParent(parent.transform);
- Selection.activeGameObject = go;
- }
- #endif
- // private Sprite _packSprite;
- //
- // public Sprite GetPackSprite
- // {
- // get { return _packSprite; }
- // }
- [SerializeField] private string _icon_name;
- [SerializeField] public bool isNotLoadDeftIcon;
- [SerializeField] public SpriteAtlas CurrSpriteAtlas;
- #if UNITY_EDITOR
- [SerializeField] public PackInfo packInfo;
- #endif
- private UILoadSpriteHand _uiLoadSpriteHand;
- public System.Action onSpriteAlter;
- protected override void Awake()
- {
- if (Application.isPlaying && isNotLoadDeftIcon)
- {
- return;
- }
- if (!string.IsNullOrEmpty(_icon_name))
- {
- icon_name = _icon_name;
- }
- }
- protected override void OnDestroy()
- {
- if (_uiLoadSpriteHand != null)
- {
- _uiLoadSpriteHand.ReleaseUI();
- _uiLoadSpriteHand = null;
- }
- base.OnDestroy();
- }
- public void ReashUI()
- {
- if (CurrSpriteAtlas != null && !string.IsNullOrEmpty(_icon_name))
- {
- Sprite loadSprite = CurrSpriteAtlas.GetSprite(_icon_name);
- if (loadSprite == null)
- {
- if (Application.isPlaying)
- {
- SpriteAtlas spriteAtlas = UGUIPackManager.Instance.GetSpriteAtlas(_icon_name);
- if (spriteAtlas != null)
- {
- loadSprite = spriteAtlas.GetSprite(_icon_name);
- }
- if (loadSprite == null)
- {
- enabled = false;
- UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
- {
- enabled = true;
- if (Application.isPlaying)
- {
- if (_uiLoadSpriteHand != null)
- {
- _uiLoadSpriteHand.ReleaseUI();
- }
- }
- _uiLoadSpriteHand = sprite1;
- if (sprite1 == null)
- {
- SetSprite(null);
- }
- else
- {
- SetSprite(sprite1.GetSprite());
- }
- });
- }
- else
- {
- SetSprite(loadSprite);
- }
- }
- }
- else
- {
- SetSprite(loadSprite);
- }
- }
- else if (!string.IsNullOrEmpty(_icon_name) && sprite == null)
- {
- if (Application.isPlaying)
- {
- enabled = false;
- UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
- {
- enabled = true;
- if (_uiLoadSpriteHand != null)
- {
- _uiLoadSpriteHand.ReleaseUI();
- }
- _uiLoadSpriteHand = sprite1;
- if (sprite1 == null)
- {
- SetSprite(null);
- }
- else
- {
- SetSprite(sprite1.GetSprite());
- }
- });
- }
- }
- else if (!string.IsNullOrEmpty(_icon_name))
- {
- if (Application.isPlaying)
- {
- enabled = false;
- UGUIIamgeTool.Load(_icon_name, delegate(UILoadSpriteHand sprite1)
- {
- enabled = true;
- if (sprite1 != null)
- {
- Sprite sp = sprite1.GetSprite();
- if (sp != null)
- {
- if (!sp.name.Equals(_icon_name))
- {
- sprite1.ReleaseUI();
- return;
- }
- }
- }
- if (_uiLoadSpriteHand != null)
- {
- _uiLoadSpriteHand.ReleaseUI();
- }
- _uiLoadSpriteHand = sprite1;
- if (sprite1 == null)
- {
- SetSprite(null);
- }
- else
- {
- SetSprite(sprite1.GetSprite());
- }
- });
- }
- }
- }
- private void SetSprite(Sprite sprite)
- {
- this.sprite = sprite;
- if (sprite != null)
- {
- onSpriteAlter?.Invoke();
- }
- }
- /// <summary>
- /// 是否置灰
- /// </summary>
- public bool IsGray
- {
- get { return _isGray; }
- set
- {
- if (!_isGray.Equals(value))
- {
- _isGray = value;
- if (_isGray)
- {
- base.material = UIManager.Instance.uiGray;
- }
- else
- {
- if (base.material != null)
- {
- base.material = null;
- }
- base.material = defaultMaterial;
- }
- }
- }
- }
- private bool _isGray;
- public override void GraphicUpdateComplete()
- {
- SetPack();
- }
- #if UNITY_EDITOR
- [ContextMenu("重新设置图片")]
- public void SetPackEditor()
- {
- if (packInfo != null && CurrSpriteAtlas != null)
- {
- string assetName = icon_name;
- if (string.IsNullOrEmpty(icon_name) && sprite != null)
- {
- assetName = sprite.name;
- }
- var loadSprite = CurrSpriteAtlas.GetSprite(assetName);
- bool isoK = loadSprite != null;
- if (isoK)
- {
- SetSprite(loadSprite);
- }
- }
- }
- [ContextMenu("自动匹配图集")]
- public void CompareSpritesInAtlas()
- {
- if(sprite == null || CurrSpriteAtlas!= null || icon_name != "")
- return;
- string targetPath = AssetDatabase.GetAssetPath(sprite);
- if (string.IsNullOrEmpty(targetPath))
- {
- Debug.LogError("无法获取目标 Sprite 的源文件路径");
- return;
- }
- string targetMD5 = MD5Helper.FileMD5("D:/FB/XiuXianGame/" + targetPath);
- foreach (var allPackgInfo in UGUICacheInfo.uguiPackDB.allPackgInfos)
- {
- string path = Application.dataPath + allPackgInfo.PackageJsonPath;
- string jsonPath = File.ReadAllText(path);
- TextrueJson textrueJson = JsonUtility.FromJson<TextrueJson>(jsonPath);
- for (int i = 0; i < textrueJson.newTextureJson.Count; i++)
- {
- string texturePath = "/Art/UIAssets" + textrueJson.newTextureJson[i].filePath;
- string spriteMD5 = MD5Helper.FileMD5(Application.dataPath + texturePath);
- if (spriteMD5 == targetMD5)
- {
- LogTool.Log($"找到匹配的 Sprite: {textrueJson.newTextureJson[i].textrueName},所在图集: {allPackgInfo.packName},源文件: {targetPath}");
- packInfo = allPackgInfo;
- CurrSpriteAtlas = UGUICacheInfo.GetSpriteAtlas(allPackgInfo.packName);
- icon_name = textrueJson.newTextureJson[i].textrueName;
- ReashUI();
- return;
- }
- }
- }
- LogTool.Log("未找到任何匹配的 Sprite");
- }
- #endif
- private void SetPack()
- {
- #if UNITY_EDITOR
- if (!Application.isPlaying)
- {
- bool isoK = false;
- if (packInfo != null && CurrSpriteAtlas != null)
- {
- string assetName = icon_name;
- if (string.IsNullOrEmpty(icon_name) && sprite != null)
- {
- assetName = sprite.name;
- }
- isoK = CurrSpriteAtlas.GetSprite(assetName) != null;
- if (!isoK) //没在在图集里面找到图片了,需要去其他地方查找(删除图集)
- {
- SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(assetName);
- if (newAtlas != null)
- {
- CurrSpriteAtlas = newAtlas;
- icon_name = assetName;
- }
- else
- {
- sprite = null;
- }
- }
- else if (string.IsNullOrEmpty(icon_name) || sprite == null)
- {
- icon_name = assetName;
- }
- }
- else if (CurrSpriteAtlas == null && packInfo != null && !string.IsNullOrEmpty(packInfo.packgJsonPath) &&
- sprite != null) //没有图集 但是由功能管理器添加的UI (添加图集)
- {
- string name = sprite.name;
- SpriteAtlas newAtlas = UGUICacheInfo.GetSpriteAtlas(name);
- if (newAtlas != null)
- {
- CurrSpriteAtlas = newAtlas;
- icon_name = _icon_name;
- }
- }
- }
- #endif
- }
- public enum GradientDirection
- {
- Vertical,
- Horizontal
- }
- [SerializeField] private bool useGradient = false;
- [SerializeField] private Color topOrLeftColor = Color.white;
- [SerializeField] private Color bottomOrRightColor = Color.black;
- [SerializeField] private GradientDirection direction = GradientDirection.Vertical;
- public bool UseGradient
- {
- get => useGradient;
- set
- {
- useGradient = value;
- SetVerticesDirty();
- }
- }
- public Color TopOrLeftColor
- {
- get => topOrLeftColor;
- set
- {
- topOrLeftColor = value;
- SetVerticesDirty();
- }
- }
- public Color BottomOrRightColor
- {
- get => bottomOrRightColor;
- set
- {
- bottomOrRightColor = value;
- SetVerticesDirty();
- }
- }
- public GradientDirection Direction
- {
- get => direction;
- set
- {
- direction = value;
- SetVerticesDirty();
- }
- }
- protected override void OnPopulateMesh(VertexHelper vh)
- {
- base.OnPopulateMesh(vh);
- if (!useGradient || vh.currentVertCount == 0)
- return;
- List<UIVertex> verts = new List<UIVertex>();
- vh.GetUIVertexStream(verts);
- for (int i = 0; i < verts.Count; i += 6)
- {
- ApplyGradientToQuad(verts, i);
- }
- vh.Clear();
- vh.AddUIVertexTriangleStream(verts);
- }
- private void ApplyGradientToQuad(List<UIVertex> verts, int startIndex)
- {
- if (startIndex + 5 >= verts.Count)
- return;
- float top = verts[startIndex].position.y;
- float bottom = verts[startIndex].position.y;
- float left = verts[startIndex].position.x;
- float right = verts[startIndex].position.x;
- for (int i = 1; i < 6; i++)
- {
- var pos = verts[startIndex + i].position;
- top = Mathf.Max(top, pos.y);
- bottom = Mathf.Min(bottom, pos.y);
- left = Mathf.Min(left, pos.x);
- right = Mathf.Max(right, pos.x);
- }
- for (int i = 0; i < 6; i++)
- {
- var vert = verts[startIndex + i];
- var pos = vert.position;
- float t = direction == GradientDirection.Vertical
- ? Mathf.InverseLerp(bottom, top, pos.y)
- : Mathf.InverseLerp(left, right, pos.x);
- Color gradientColor = Color.Lerp(bottomOrRightColor, topOrLeftColor, t);
- gradientColor.r *= color.r;
- gradientColor.g *= color.g;
- gradientColor.b *= color.b;
- gradientColor.a *= color.a;
- vert.color = gradientColor;
- verts[startIndex + i] = vert;
- }
- }
- }
- }
|