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
{
///
/// Image is a textured element in the UI hierarchy.
///
[RequireComponent(typeof(CanvasRenderer))]
[AddComponentMenu("UI/MyImage", 11)]
///
/// Displays a Sprite inside the UI System.
///
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
}
}
}