码迷,mamicode.com
首页 > 其他好文 > 详细

ugui 通用页签管理器

时间:2020-03-02 23:21:00      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:manage   before   com   cli   print   var   一个   get   new   

一直是个痛点,这次解决了, 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;

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

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

    //打开当前页签
    public void OpenCurTabContent()
    {
        if (curContentObj != null)
        {
            print(curContentObj.name);
            curContentObj.GetComponent<ITabContent>().OpenTabContent();    
        }
    }
}

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

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

public class UITabButton : MonoBehaviour
{
    public Button btn;
    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();
}

 

ugui 通用页签管理器

标签:manage   before   com   cli   print   var   一个   get   new   

原文地址:https://www.cnblogs.com/sanyejun/p/12398296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!