前言
因工作需要,unity自带的Toggle组件如满足自己的需求,使用比较复杂,咬牙自己封装一套,也提供给小伙伴们一同使用,如有任何可优化的地方,也烦请各位大佬友情提出,感恩的???
效果展示
下载链接:https://download.csdn.net/download/qq_33161296/86511183
UI结构
安装说明:
- 下载拖入工程中
- 在要设置的按钮上添加Tab脚本
- 根据添加后,Inspector上的提示,设置鼠标移入及点击时的设置
使用说明:
- TabTxt: 按钮下的text
- MultipleChoice:是否为复选框
- IsOn:当前是否为选中状态,手动勾选的话,初始化时为默认选中状态
鼠标移入状态设置
- IsHoverImgActive: 鼠标移入改变显示状态
- IsHoverTxtColor: 鼠标移入改变文字显示颜色
- IsHoverImgColor: 鼠标移入改变图片显示颜色 以上选中后根据提示设置相应内容即可
选中时状态设置
- IsOnImgActive:选中后改变图片显示状态
- IsOnTxtColor: 选中后改变文字显示颜色
- IsOnImgColor:选中后改变图片显示颜色 以上选中后根据提示设置相应内容即可 showPanel:为选中时需要显示的对象,通过setactive设置,可不设置
- Tabcontroller:父级控制器,可手动配置,如果没有手动配置,会默认在直接父级上创建
- TabName: 无实际作用,可给按钮设定标签名
API
- TabClick(广播事件):可获取当前点击的对象(类型:Tab),每次点击都能获取到
- TabClickFirstEffect(广播事件):可获取当前点击的对象(类型:Tab),仅获取第一次
- text(属性):可获取按钮下text的名称
- IsOn:设置显示状态,TabClick和TabClickFirstEffect可获取到点击事件
- IsOnWithOutEvent:仅改变状态,没有事件的触发
附主要逻辑代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace ZTools
{
[RequireComponent(typeof(Image), typeof(Button))]
public class Tab : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
[Header("按钮下text")]
[SerializeField]
private Text tabTxt;
[Header("给按钮设置标签")]
public string tabName;
[SerializeField]
[Header("是否选中")]
private bool isOn = false;
[SerializeField]
[Header("是否可以多选")]
private bool multipleChoice = false;
[Header("改变图片显示状态")]
[Header("---------------鼠标移入状态设置----------------")]
[SerializeField]
private bool isHoverImgActive;
[SerializeField]
private GameObject hoverImg;
[Header("改变图片显示颜色")]
[SerializeField]
private bool isHoverImgColor;
[Header("改变文字显示颜色")]
[SerializeField]
private bool isHoverTxtColor;
[SerializeField]
[Header("改变图片显示状态")]
[Header("---------------选中时状态设置----------------")]
private bool isOnImgActive = false;
[SerializeField]
[Header("改变文字显示颜色")]
private bool isOnTxtColor = false;
[SerializeField]
[Header("改变图片显示颜色")]
private bool isOnImgColor = false;
[Header("父级控制器,可手动配置")]
[Header("---------------------------------------------")]
[SerializeField]
private TabController tabcontroller = null;
[SerializeField]
private Image tabImg;
[SerializeField]
private Image tabChangeColorImg;
[SerializeField]
private Image hoverImage;
[SerializeField]
private Color changeTxtColor;
[SerializeField]
private Color hoverTxtColor;
[SerializeField]
private Color imgColor;
[SerializeField]
private Color hoverImageColor;
[SerializeField]
private List<GameObject> showPanel;
private Button btnTab;
public TabController tabController
{
set
{
tabcontroller = value;
tabController.AddTab(this);
}
get { return tabcontroller; }
}
private Color origTxtColor;
private Color origImgColor;
private Color origHoverImgColor;
/// <summary>
/// 点击事件,选中状态再次点击有效
/// </summary>
public event Action<Tab> TabClick;
/// <summary>
/// 点击事件,选中状态再次点击无效
/// </summary>
public event Action<Tab> TabClickFirstEffect;
private bool isInit = false;
private int hash;
public string text
{
get
{
if (null != tabTxt)
{
return tabTxt.text;
}
else
{
Debug.LogError("大佬您按钮下可没有text哦,赶快加一个吧/(ㄒoㄒ)/~~");
return "大佬您按钮下可没有text哦,赶快加一个吧/(ㄒoㄒ)/~~";
}
}
}
public bool IsOn
{
set
{
if (isOn != value && value)
if (TabClickFirstEffect != null)
TabClickFirstEffect.Invoke(this);
ChangeType(value);
if (isOn)
if (TabClick != null)
TabClick.Invoke(this);
}
get { return isOn; }
}
public bool IsOnWithOutEvent
{
set
{
ChangeType(value);
}
}
void Awake()
{
btnTab = this.GetComponent<Button>();
btnTab.transition = Selectable.Transition.None;
if (null == tabTxt)
{
tabTxt = transform.Find("Text").GetComponent<Text>();
}
hash = this.GetHashCode();
if (!isInit)
{
InitData();
}
if (isOn)
{
isOn = false;
IsOn = true;
}
btnTab.onClick.AddListener(() =>
{
IsOn = (!multipleChoice) ? true : !IsOn;
});
}
/// <summary>
/// 保存当前状态
/// </summary>
void InitData()
{
isInit = true;
if (isHoverTxtColor || isOnTxtColor)
{
if (null != tabTxt)
{
origTxtColor = tabTxt.color;
}
else
{
Debug.LogError("tabTxt is null");
}
}
if (isOnImgColor)
{
if (null != tabChangeColorImg)
{
origImgColor = tabChangeColorImg.color;
}
else
{
Debug.LogError("tabChangeColorImg is null");
}
}
if (isHoverImgColor)
{
if (null != hoverImage)
{
origHoverImgColor = hoverImage.color;
}
else
{
Debug.LogError("hoverImage is null");
}
}
if (!multipleChoice)
{
if (null == tabController)
{
tabController = (this.transform.parent.GetComponent<TabController>() == null) ? this.transform.parent.gameObject.AddComponent<TabController>() : this.transform.parent.GetComponent<TabController>();
}
else
{
tabController.AddTab(this);
}
}
}
/// <summary>
/// 改变选择的状态
/// </summary>
/// <param name="value"></param>
void ChangeType(bool value)
{
if (isOn == value) return;
isOn = value;
if (isHoverImgActive)
{
hoverImg.gameObject.SetActive(false);
}
if (isHoverImgColor)
{
hoverImage.color = origHoverImgColor;
}
if (isHoverTxtColor)
{
tabTxt.color = origTxtColor;
}
if (isOnTxtColor)
{
tabTxt.color = (value) ? changeTxtColor : origTxtColor;
}
if (isOnImgActive)
{
tabImg.gameObject.SetActive(value);
}
if (isOnImgColor)
{
tabChangeColorImg.color = (value) ? imgColor : origImgColor;
}
if (showPanel.Count > 0)
{
showPanel.ForEach(item =>
{
if (item != null)
item.SetActive(value);
});
}
if (value && !multipleChoice)
{
tabController.ChoiceTab(hash);
}
}
/// <summary>
/// 监听改变的状态
/// </summary>
/// <param name="txtName"></param>
public void ChangeTab(int tabHash)
{
if (tabHash != hash)
IsOn = false;
}
public void OnPointerEnter(PointerEventData eventData)
{
if (isHoverImgActive && !isOn)
{
hoverImg.SetActive(true);
}
if (isHoverTxtColor && !isOn)
{
tabTxt.color = hoverTxtColor;
}
if (isHoverImgColor && !isOn)
{
hoverImage.color = hoverImageColor;
}
}
public void OnPointerExit(PointerEventData eventData)
{
if (isHoverImgActive && !isOn)
{
hoverImg.SetActive(false);
}
if (isHoverTxtColor && !isOn)
{
tabTxt.color = origTxtColor;
}
if (isHoverImgColor && !isOn)
{
hoverImage.color = origHoverImgColor;
}
}
private void OnDestroy()
{
if (tabController != null)
{
tabController.RemoveTab(this);
}
}
}
}
更多好用插件会陆续更新,如有插件优化,或者更多好推荐,烦请大佬们友情指出
更多好用Unity插件请见【JacobKay的博客_CSDN博客-领域博主】
感谢观看~~~~
|