U3d结合NGUI做的一个Tab切换例子
错误说明:以下例子,由于不熟悉NGUI各个控件:如:wiget / panel,所以用了panel,其实更应该用的是wiget !!!!!!!不然会增加N多个Panel次的渲染。
~~~
参考Unity3D 自定义选项卡组件结合以前的AS3写的一个
使用
public UIButton[] tabButtonList;//标签按钮
public GameObject[] contentList;//标签内容
TabGroup tabGroup = new TabGroup();
GameObject[] buttonList = new GameObject[tabButtonList.Length];
for (int i = 0; i < tabButtonList.Length; i++)
{
buttonList[i] = tabButtonList[i].gameObject;
}
tabGroup.AddTabs(buttonList);
tabGroup.OnTabGroup = delegate(int selectedIndex)
{
for (int j = 0; j < contentList.Length; j++)
{
bool selected = j == selectedIndex;
contentList[j].gameObject.SetActive(selected);//显示哪个内容
}
};
tabGroup.selectedIndex = 0;//默认第一个标签
图
下载
源码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/**
* 标签页切换
* TabGroup tabGroup = new TabGroup();
tabGroup.AddTabs(GameObject[] tabList);
tabGroup.OnTabGroup = delegate(int selectedIndex){}
* */
public class TabGroup
{
//标签列表
private GameObject[] tabList;
//已选中标签id 从0开始...
private int _selectedIndex;
//标签页回调函数
public delegate void DeletegateTabGroup(int selectedIndex);
public DeletegateTabGroup OnTabGroup;
public TabGroup()
{
_selectedIndex = -1;
} z
/**
* 跳转到指定的标签
* */
public int selectedIndex
{
get { return _selectedIndex; }
set
{
if (_selectedIndex == value) return;
if (value >= tabList.Length) return;
GameObject g = tabList[value];
OnTabClick(g);
}
}
/**
* 加上标签按钮
* */
public void AddTabs(GameObject[] list)
{
tabList = list;
for (int i = 0; i < list.Length; i++)
{
GameObject o = list[i].gameObject;
if (o)
{
UIEventListener.Get(o).onClick += OnTabClick;// delegate(GameObject g)
}
}
}
private void OnTabClick(GameObject g)
{
int index = -1;
for (int i = 0; i < tabList.Length; i++)
{
GameObject o = tabList[i].gameObject;
UIButton button = null;
if (o != null) button = tabList[i].GetComponent<UIButton>();
if (o && o == g)
{
index = i;
//寻找uibutton
if (button != null)
{
button.isEnabled = false;
}
}
else
{
if (button != null)
{
button.isEnabled = true;
}
}
}
Debug.Log(" selected Index = " + index);
if (index != _selectedIndex)
{
_selectedIndex = index;
if (OnTabGroup != null)
{
OnTabGroup(index);
}
}
}
}