195 lines
5.3 KiB
C#
195 lines
5.3 KiB
C#
using Thousandto.Code.Center;
|
|
using Thousandto.Core.Base;
|
|
using Thousandto.Plugins.Common;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
|
|
namespace Thousandto.GameUI.Form
|
|
{
|
|
/// <summary>
|
|
/// 直接为UISprite的对象上挂接这个脚本
|
|
/// </summary>
|
|
public class UIIconBase : MonoCacheBase
|
|
{
|
|
#region //私有变量
|
|
private UISprite _icon = null; //显示ICON
|
|
private int _iconId = -1; //ICON ID
|
|
private bool _isInit = false; //是否初始化
|
|
private bool _isNeedSnap = false;//是否需要Snap
|
|
private UIAtlas _atlas = null; //atlas
|
|
private MyAction<UIAtlas> _onCallBack;
|
|
private MyAction _loadCallBack = null;
|
|
#endregion
|
|
|
|
#region//属性
|
|
//获取图片
|
|
public UISprite IconSprite
|
|
{
|
|
get
|
|
{
|
|
InitializationWithCheck();
|
|
return _icon;
|
|
}
|
|
}
|
|
//设置是否为灰色
|
|
public bool IsGray
|
|
{
|
|
get
|
|
{
|
|
return IconSprite.IsGray;
|
|
}
|
|
set
|
|
{
|
|
|
|
IconSprite.IsGray = value;
|
|
}
|
|
}
|
|
public int IconId
|
|
{
|
|
get
|
|
{
|
|
return _iconId;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region//继承MonoBehaviour
|
|
/*
|
|
protected void Awake()
|
|
{
|
|
InitializationWithCheck();
|
|
} */
|
|
|
|
//这里使用保护类型的原因是:如果子类需要在OnEnable中处理一些事务的时候,需要在子类中使用关键字new,以及使用base.OnEnable来调用这个方法.
|
|
protected void OnEnable()
|
|
{
|
|
if (_isInit && _iconId >= 0 && _icon != null)
|
|
{
|
|
_icon.spriteName = _iconId.ToString();
|
|
LoadAltas();
|
|
}
|
|
}
|
|
//这里使用保护类型的原因是:如果子类需要在OnDisable中处理一些事务的时候,需要在子类中使用关键字new,以及使用base.OnDisable来调用这个方法.
|
|
protected void OnDisable()
|
|
{
|
|
if (_isInit && _iconId >= 0)
|
|
{
|
|
GameUICenter.UIIconAltasManager.UnUseAltasWithIconId(_iconId, GetAtlasLoadFinishCallBack());
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region//公共接口
|
|
public void UpdateIcon(int iconId, bool isNeedSnap = false, MyAction loadCallBack = null)
|
|
{
|
|
//1.进行初始化
|
|
InitializationWithCheck();
|
|
|
|
//2.对老的数据进行处理
|
|
if (_iconId != iconId )
|
|
{//判断当前Icon与原来的不一样,并且已经加载过atlas,
|
|
|
|
if (gameObject.activeInHierarchy)
|
|
{//只有在激活状态下,如果有更新,那么就卸载这个id所在的altas
|
|
|
|
GameUICenter.UIIconAltasManager.UnUseAltasWithIconId(_iconId, GetAtlasLoadFinishCallBack());
|
|
|
|
}
|
|
}
|
|
|
|
//3.保存关键数据IconID
|
|
_iconId = iconId;
|
|
|
|
|
|
//4.只有在被激活状态下才去加载Altas
|
|
if (gameObject.activeInHierarchy)
|
|
{
|
|
LoadAltas();
|
|
}
|
|
|
|
//5.下面为UISprite组件进行赋值Atlas以及SpriteName
|
|
if (_icon != null)
|
|
{
|
|
_icon.atlas = _atlas;
|
|
_icon.spriteName = _iconId.ToString();
|
|
}
|
|
_isNeedSnap = isNeedSnap;
|
|
_loadCallBack = loadCallBack;
|
|
}
|
|
|
|
public void InitializationWithCheck()
|
|
{
|
|
if (!_isInit)
|
|
{
|
|
Initialization();
|
|
}
|
|
}
|
|
|
|
public void Initialization()
|
|
{
|
|
OnInitialization();
|
|
_icon = OnLoadIconSprite();
|
|
_atlas = null;
|
|
_isInit = true;
|
|
_iconId = -1;
|
|
_isNeedSnap = false;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region//虚函数
|
|
protected virtual void OnInitialization()
|
|
{
|
|
|
|
}
|
|
|
|
protected virtual UISprite OnLoadIconSprite()
|
|
{
|
|
return GetComponent<UISprite>();
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region//私有函数
|
|
|
|
private void LoadAltas()
|
|
{
|
|
_atlas = GameUICenter.UIIconAltasManager.GetAltasWithIconId(_iconId, GetAtlasLoadFinishCallBack());
|
|
}
|
|
|
|
private void OnAltasLoadCallBack(UIAtlas atlas)
|
|
{
|
|
|
|
if (atlas != null && _icon != null)
|
|
{
|
|
//FLogger.LogError("OnAltasLoadCallBack finished ",atlas.name);
|
|
_atlas = atlas;
|
|
_icon.atlas = atlas;
|
|
_icon.MarkAsChanged();
|
|
//是否需要设置Icon为默认大小 丁华强 2020/08/05
|
|
if (_isNeedSnap)
|
|
{
|
|
_icon.MakePixelPerfect();
|
|
}
|
|
if (_loadCallBack != null)
|
|
{
|
|
_loadCallBack();
|
|
}
|
|
}
|
|
}
|
|
|
|
private MyAction<UIAtlas> GetAtlasLoadFinishCallBack()
|
|
{
|
|
if (_onCallBack == null)
|
|
{
|
|
_onCallBack = OnAltasLoadCallBack;
|
|
}
|
|
return _onCallBack;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|