YanCheng_Metrology/Assets/Scripts/ProjectBase/UIManager/BasePanel.cs

157 lines
3.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// ??????
/// </summary>
public class BasePanel : MonoBehaviour
{
/// <summary>
/// ??????????§á??
/// </summary>
private readonly Dictionary<string,List<UIBehaviour>> controlDic = new Dictionary<string, List<UIBehaviour>>();
/// <summary>
/// ????? ??????
/// </summary>
protected virtual void Awake()
{
FindChildrenControl<Button>();
FindChildrenControl<Image>();
FindChildrenControl<Text>();
FindChildrenControl<Toggle>();
FindChildrenControl<Slider>();
FindChildrenControl<ScrollRect>();
FindChildrenControl<InputField>();
FindChildrenControl<TextMeshPro>();
}
/// <summary>
/// ?????????????
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="controlName"></param>
/// <returns></returns>
public T GetControl<T>(string controlName) where T : UIBehaviour
{
if (controlDic.ContainsKey(controlName))
{
for (int i = 0; i < controlDic[controlName].Count; i++)
{
if (controlDic[controlName][i] is T)
return controlDic[controlName][i] as T;
}
}
return null;
}
/// <summary>
/// ?????????????
/// </summary>
/// <typeparam name="T"></typeparam>
private void FindChildrenControl<T>() where T : UIBehaviour
{
T[] controls = GetComponentsInChildren<T>(true);
for (int i = 0; i < controls.Length; i++)
{
string controlName = controls[i].gameObject.name;
if (controlDic.ContainsKey(controlName))
{
controlDic[controlName].Add(controls[i]);
}
else
{
controlDic.Add(controlName,new List<UIBehaviour>() { controls[i] });
}
if (controls[i] is Button button)
{
button.onClick.AddListener(() =>
{
OnClick(controlName);
});
}
else if(controls[i] is Toggle toggle)
{
toggle.onValueChanged.AddListener((value) =>
{
OnChangeToggle(controlName,value);
});
}
else if (controls[i] is Slider slider)
{
slider.onValueChanged.AddListener((value) =>
{
OnChangeSlider(controlName, value);
});
}
else if (controls[i] is InputField inputField)
{
inputField.onValueChanged.AddListener((value) =>
{
OnChangeInputFile(controlName,value);
});
}
}
}
/// <summary>
/// ??????
/// </summary>
public virtual void ShowMe()
{
}
/// <summary>
/// ???????
/// </summary>
public virtual void HideMe()
{
}
/// <summary>
/// ?????????
/// </summary>
/// <param name="btnName"></param>
protected virtual void OnClick(string btnName)
{
}
/// <summary>
/// Toogle???
/// </summary>
/// <param name="toogleName">toogle????</param>
/// <param name="isOn">???</param>
protected virtual void OnChangeToggle(string toogleName,bool isOn)
{
}
/// <summary>
/// Slider???
/// </summary>
/// <param name="SliderName">Slider???????</param>
/// <param name="value">???</param>
protected virtual void OnChangeSlider(string SliderName, float value)
{
}
/// <summary>
/// ?????????
/// </summary>
/// <param name="inputFileName">?????????</param>
/// <param name="value">?????????</param>
protected virtual void OnChangeInputFile(string inputFileName,string value)
{
}
}