46 lines
1.1 KiB
C#
46 lines
1.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class TabGroup : MonoBehaviour
|
|
{
|
|
public Button[] buttons;
|
|
public GameObject[] contents;
|
|
public int selectedIndex = 0;
|
|
|
|
|
|
void OnValidate(){
|
|
if(buttons == null || buttons.Length == 0){
|
|
buttons=GetComponentsInChildren<Button>();
|
|
}
|
|
}
|
|
|
|
|
|
void Awake()
|
|
{
|
|
SetSelectedIndex(selectedIndex);
|
|
|
|
for(int i=0; i < buttons.Length; i++)
|
|
{
|
|
int index = i;
|
|
buttons[i].onClick.AddListener(() => SetSelectedIndex(index));
|
|
}
|
|
}
|
|
|
|
|
|
public void SetSelectedIndex(int index)
|
|
{
|
|
if(index < 0 || index >= buttons.Length) return;
|
|
|
|
selectedIndex = index;
|
|
for(int i = 0; i < buttons.Length; i++)
|
|
{
|
|
buttons[i].targetGraphic.color = i == index ? buttons[i].colors.normalColor : buttons[i].colors.disabledColor;
|
|
try{
|
|
contents[i].SetActive(i == index);
|
|
}catch{
|
|
Debug.LogError("Content not found for index: " + i);
|
|
}
|
|
}
|
|
}
|
|
}
|