// --------------------------------------------------------------------------------------------------------------------
//
// Part of: PunCockpit
//
//
// Simple Management for Tabs, it requires a ToggleGroup, and then for each Tab, a Unique Name, the related Toggle and its associated RectTransform View
// this manager handles Tab views activation and deactivation, and provides a Unity Event Callback when a tab was selected.
//
// developer@exitgames.com
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
namespace Photon.Pun.UtilityScripts
{
///
/// Tab view manager. Handles Tab views activation and deactivation, and provides a Unity Event Callback when a tab was selected.
///
public class TabViewManager : MonoBehaviour
{
///
/// Tab change event.
///
[System.Serializable]
public class TabChangeEvent : UnityEvent { }
[Serializable]
public class Tab
{
public string ID = "";
public Toggle Toggle;
public RectTransform View;
}
///
/// The toggle group component target.
///
public ToggleGroup ToggleGroup;
///
/// all the tabs for this group
///
public Tab[] Tabs;
///
/// The on tab changed Event.
///
public TabChangeEvent OnTabChanged;
protected Tab CurrentTab;
Dictionary Tab_lut;
void Start()
{
Tab_lut = new Dictionary();
foreach (Tab _tab in this.Tabs)
{
Tab_lut[_tab.Toggle] = _tab;
_tab.View.gameObject.SetActive(_tab.Toggle.isOn);
if (_tab.Toggle.isOn)
{
CurrentTab = _tab;
}
_tab.Toggle.onValueChanged.AddListener((isSelected) =>
{
if (!isSelected)
{
return;
}
OnTabSelected(_tab);
});
}
}
///
/// Selects a given tab.
///
/// Tab Id
public void SelectTab(string id)
{
foreach (Tab _t in Tabs)
{
if (_t.ID == id)
{
_t.Toggle.isOn = true;
return;
}
}
}
///
/// final method for a tab selection routine
///
/// Tab.
void OnTabSelected(Tab tab)
{
CurrentTab.View.gameObject.SetActive(false);
CurrentTab = Tab_lut[ToggleGroup.ActiveToggles().FirstOrDefault()];
CurrentTab.View.gameObject.SetActive(true);
OnTabChanged.Invoke(CurrentTab.ID);
}
}
}