123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using Fort23.UTool;
- 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("UI/MyImage", 11)]
- /// <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();
- }
- }
- // private Sprite _packSprite;
- //
- // public Sprite GetPackSprite
- // {
- // get { return _packSprite; }
- // }
- [SerializeField] private string _icon_name;
- [SerializeField] public bool isNotLoadDeftIcon;
- [SerializeField] public SpriteAtlas CurrSpriteAtlas;
- [SerializeField] public PackInfo packInfo;
- 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();
- }
- }
- public override void GraphicUpdateComplete()
- {
- SetPack();
- }
- 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
- }
- }
- }
|