zoukankan      html  css  js  c++  java
  • 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();
    }
  • 相关阅读:
    数据结构01-线性表
    java-04流程控制语句
    从0开始的Python学习002python的数据类型
    从0开始的Python学习001快速上手手册
    MySQl ifnull()和substr()
    parent.fraInterface.xxxxxx
    身份证的校验规则
    onclick="return function()"的使用情况
    jsp include 报错:illegal to have multiple occurrences of contentType with different values (old: text/html; charset=UTF-8, new: text/html; carset=UTF-8)
    Oracle数据库忘记用户名密码的解决方案
  • 原文地址:https://www.cnblogs.com/sanyejun/p/12398296.html
Copyright © 2011-2022 走看看