Files
MMORPG/Assets/Scripts/UI/UI_Base.cs
2026-01-27 00:34:47 +09:00

88 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public abstract class UI_Base : MonoBehaviour
{
Dictionary<Type, UnityEngine.Object[]> _objects = new Dictionary<Type, UnityEngine.Object[]>();
public abstract void Init();
// Bind<제네릭>(인자)
protected void Bind<T>(Type type) where T : UnityEngine.Object
{
string[] names = Enum.GetNames(type);
UnityEngine.Object[] objects = new UnityEngine.Object[names.Length];
_objects.Add(typeof(T), objects);
for (int i = 0; i < names.Length; i++)
{
if (typeof(T) == typeof(GameObject))
{
objects[i] = Util.FindChild(gameObject, names[i], true);
}
else
{
objects[i] = Util.FindChild<T>(gameObject, names[i], true);
}
if (objects[i] == null)
{
Debug.Log($"Fail to Bind({names[i]})");
}
}
}
protected T Get<T>(int idx) where T : UnityEngine.Object
{
UnityEngine.Object[] objects = null;
if (_objects.TryGetValue(typeof(T), out objects) == false)
{
return null;
}
return objects[idx] as T;
}
protected GameObject GetObject(int idx)
{
return Get<GameObject>(idx);
}
protected TextMeshProUGUI GetText(int idx)
{
return Get<TextMeshProUGUI>(idx);
}
protected Button GetButton(int idx)
{
return Get<Button>(idx);
}
protected Image GetImage(int idx)
{
return Get<Image>(idx);
}
public static void BindEvent(GameObject go, Action<PointerEventData> action, Define.UIEvent type = Define.UIEvent.Click)
{
UI_EventHandler evt = Util.GetOrAddComponent<UI_EventHandler>(go);
switch (type)
{
case Define.UIEvent.Click:
evt.OnClickHandler -= action;
evt.OnClickHandler += action;
break;
case Define.UIEvent.Drag:
evt.OnDragHandler -= action;
evt.OnDragHandler += action;
break;
}
}
}