UI 자동화를 위해 바인딩 기능 구현
- 유니티 에셋 인증 오류로 meta 재생성
This commit is contained in:
62
Assets/Scripts/UI/UI_Base.cs
Normal file
62
Assets/Scripts/UI/UI_Base.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class UI_Base : MonoBehaviour
|
||||
{
|
||||
Dictionary<Type, UnityEngine.Object[]> _objects = new Dictionary<Type, UnityEngine.Object[]>();
|
||||
|
||||
// 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 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user