Files
Main/Assets/GameAssets/Resources/GameUI/Common/UIComponentContainer/UIButtonInfo.cs
2025-01-25 04:38:09 +08:00

229 lines
6.1 KiB
C#

using Thousandto.Core.Base;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Thousandto.Plugins.Common
{
/// <summary>
/// 功能按钮类
/// </summary>
public class UIButtonInfo : IUIComponent<UIButtonInfo, IButtonFunctionInfo>
{
//按钮组件
private UIButton _btn;
//Sprite组件
private UISprite _icon;
//文本组件
private UILabel _name;
//红点
private GameObject _redPointGo;
//效果对象
private GameObject _effectGo;
//当前按钮的对象
private Transform _trans;
//按钮的功能信息
private IButtonFunctionInfo _data;
//设置的参数
private object _param;
//Sprite组件的名字
private string _iconGoName = "Icon";
//文本组件的名字
private string _textGoName = "Name";
//红点
private string _redPointName = "RedPoint";
//特效名字
private string _effectName = "Effect";
//回调处理
private MyAction<IButtonFunctionInfo,object> _callBack;
//对象
public Transform Trans
{
get { return _trans; }
}
public UIButtonInfo(Transform trans, string iconGoName = "Icon", string textGoName = "Name", string redPointName = "RedPoint", string effectName = "Effect", MyAction<IButtonFunctionInfo, object> callBack = null)
{
_trans = trans;
_callBack = callBack;
_btn = trans.GetComponent<UIButton>();
if (!string.IsNullOrEmpty(iconGoName))
{
var tmpTrans = trans.Find(iconGoName);
if (tmpTrans != null)
_icon = tmpTrans.GetComponent<UISprite>();
}
if (!string.IsNullOrEmpty(textGoName))
{
var tmpTrans = trans.Find(textGoName);
if (tmpTrans != null)
_name = tmpTrans.GetComponent<UILabel>();
}
if (!string.IsNullOrEmpty(redPointName))
{
var tmpTrans = trans.Find(redPointName);
if (tmpTrans != null)
{
_redPointGo = tmpTrans.gameObject;
}
}
if (!string.IsNullOrEmpty(effectName))
{
var tmpTrans = trans.Find(effectName);
if (tmpTrans != null)
{
_effectGo = tmpTrans.gameObject;
}
}
_iconGoName = iconGoName;
_textGoName = textGoName;
_redPointName = redPointName;
_effectName = effectName;
_btn.onClick.Clear();
_btn.onClick.Add(new EventDelegate(OnBtnClick));
_trans.gameObject.SetActive(false);
}
//以这个对象为蓝本克隆一个
public UIButtonInfo Clone()
{
var newGo = GameObject.Instantiate(_trans.gameObject) as GameObject;
var ep = new UIButtonInfo(newGo.transform, _iconGoName, _textGoName, _redPointName, _effectName,_callBack);
ep._trans.parent = _trans.parent;
UnityUtils.Reset(ep._trans);
return ep;
}
//设置激活
public void SetActive(bool isActive)
{
_trans.gameObject.SetActive(isActive);
if (!isActive)
{
_data = null;
_param = null;
}
}
//设置配置
public void SetData(IButtonFunctionInfo data)
{
_data = data;
if (_data != null)
{
SetIcon(_data.Icon);
SetText(_data.Text);
SetRedPoint(_data.IsShowRedPoint);
SetEffectState(_data.IsEffectShow);
_trans.name = string.Format("{0:D4}", _data.SortNum);
}
}
public void RefreshData()
{
if (_data != null)
{
SetIcon(_data.Icon);
SetText(_data.Text);
SetRedPoint(_data.IsShowRedPoint);
SetEffectState(_data.IsEffectShow);
_trans.name = string.Format("{0:D4}", _data.SortNum);
}
}
public void SetName(string name)
{
_trans.name = name;
}
public void SetNameColor( Color color )
{
_name.color = color;
}
//设置参数
public void SetParam(object param)
{
_param = param;
}
//设置Icon
public void SetIcon(string spriteName)
{
if (_icon != null)
{
if (string.IsNullOrEmpty(spriteName))
{
_icon.spriteName = string.Empty;
}
else
{
_icon.spriteName = spriteName;
}
}
}
//设置文本
public void SetText(string text)
{
if (_name != null)
{
_name.text = text;
}
}
//显示红色点点
public void SetRedPoint(bool isRedPoint)
{
if (_redPointGo != null)
{
_redPointGo.SetActive(isRedPoint);
}
}
//效果状态
public void SetEffectState(bool isShowEffect)
{
if (_effectGo != null)
{
_effectGo.SetActive(isShowEffect);
}
}
//点击按钮
private void OnBtnClick()
{
if (_data != null)
{
if (_callBack == null)
{
_data.OnClickHandler(_param);
}
else
{
_callBack(_data, _param);
}
if (_data != null)
SetEffectState(_data.IsEffectShow);
}
}
}
}