Unity4.6 UI实现tabs标签

 

unity游戏开发中tab标签页想必会在很多地方用到,可能NGUI以及daikon forge GUI比较成熟,所以常用的控件比如listView,Table表格以及TabPage都有现成的模板例子,本篇文章我们来学习下,如何在unity new ui中实现tab标签页,不多说了,上图

下面我们来简单介绍下ugui tabpage制作过程,博主参考了NGUI的tabpage,贴上核心代码


using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.Collections.Generic;
[Serializable]
public class TabControlEntry
{
[SerializeField]
private GameObject panel = null;
public GameObject Panel { get { return panel; } }

[SerializeField]
private Button tab = null;
public Button Tab { get { return tab; } }
}

public class TabControl : MonoBehaviour
{
[SerializeField]
private List<TabControlEntry> entries = null;

[SerializeField]
private GameObject panelContainer = null;
[SerializeField]
private GameObject tabContainer = null;

[SerializeField]
private GameObject tabPrefab = null;
[SerializeField]
private GameObject panelPrefab = null;

protected virtual void Start()
{
foreach (TabControlEntry entry in entries)
{
AddButtonListener(entry);
}

if (entries.Count > 0)
{
SelectTab(entries[0]);
}
}

public void AddEntry(TabControlEntry entry)
{
entries.Add(entry);
}

private void AddButtonListener(TabControlEntry entry)
{
entry.Tab.onClick.AddListener(() => SelectTab(entry));
}

private void SelectTab(TabControlEntry selectedEntry)
{
foreach (TabControlEntry entry in entries)
{
bool isSelected = entry == selectedEntry;

entry.Tab.interactable = !isSelected;
entry.Panel.SetActive(isSelected);
}
}
}

下面是下载地址,希望您喜欢

ugui-tab.txt (下载495 )