ugui 通用页签管理器

一直是个痛点,这次解决了, ugui通用

复制代码

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

/// <summary>
/// UGUI页签管理器
///
/// 管理器挂上UITabManager
/// 页签按钮挂上UITabButton
/// 页签内容挂上继承了ITabContent接口的脚本
/// 注意面板赋值
/// </summary>
public class UITabManager : MonoBehaviour
{
//页签按钮列表
public List<UITabButton> tabButtonList;
//页签列表
private List<GameObject> tabContentList = new List<GameObject>();
//当前页签
//private GameObject curContentObj;
private UITabButton curButton;

// Start is called before the first frame update
void Start()
{
foreach (var one in tabButtonList)
{
tabContentList.Add(one.tabContent);
//默认打开第一个
if (tabContentList.Count == 1)
{
curButton = one;
OpenCurTabContent();
}
else
{
CloseTabContent(one);
}
one.btn.onClick.AddListener(() =>
{
//避免重复点击
if (curButton != one)
{
CloseTabContent(curButton);
curButton = one;
OpenCurTabContent();
}
});
}
}

// Update is called once per frame
void Update()
{

}

//打开当前页签
public void OpenCurTabContent()
{
if (curButton != null)
{
curButton.tabContent.GetComponent<ITabContent>().OpenTabContent();
curButton.select.SetActive(true);
curButton.unselect.SetActive(false);
}
}

public void CloseTabContent(UITabButton tabButton)
{
tabButton.tabContent.GetComponent<ITabContent>().CloseTabContent();
tabButton.select.SetActive(false);
tabButton.unselect.SetActive(true);
}
}
 
复制代码

按钮挂载,并拖到UITabManager的tabButtonList上去

复制代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UITabButton : MonoBehaviour
{
public Button btn;
public GameObject select;
public GameObject unselect;
public GameObject tabContent;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}
}
 
复制代码

接口

复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface ITabContent
{
    void OpenTabContent();
    void CloseTabContent();
}
复制代码

 

posted @   三页菌  阅读(523)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示