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);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/UI_Base.cs.meta
Normal file
2
Assets/Scripts/UI/UI_Base.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1e84740b977012741997a345570e2db4
|
||||
46
Assets/Scripts/UI/UI_Button.cs
Normal file
46
Assets/Scripts/UI/UI_Button.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class UI_Button : UI_Base
|
||||
{
|
||||
enum Buttons
|
||||
{
|
||||
PointButton,
|
||||
}
|
||||
|
||||
enum Texts
|
||||
{
|
||||
PointText,
|
||||
ScoreText,
|
||||
}
|
||||
|
||||
enum GameObjects
|
||||
{
|
||||
TestObject
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Reflection
|
||||
Bind<Button>(typeof(Buttons));
|
||||
// Text 대신 TextMeshProUGUI로 변경
|
||||
Bind<TextMeshProUGUI>(typeof(Texts));
|
||||
Bind<GameObject>(typeof(GameObjects));
|
||||
|
||||
// Get 호출 시에도 타입을 맞춰줍니다.
|
||||
// Get<TextMeshProUGUI>((int)Texts.ScoreText).text = "Bind Test";
|
||||
GetText((int)Texts.ScoreText).text = "Bind Test";
|
||||
}
|
||||
|
||||
private int _score = 0;
|
||||
|
||||
public void OnButtonClicked()
|
||||
{
|
||||
// GetComponentInChildren<TextMeshProUGUI>().text = "텍스트 변경 완료!";
|
||||
|
||||
_score += 1;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/UI/UI_Button.cs.meta
Normal file
11
Assets/Scripts/UI/UI_Button.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 16e71df7503657545a650786979749eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user