static 관련
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public abstract class UI_Base : MonoBehaviour
{
protected Dictionary<Type, UnityEngine.Object[]> _objects = new Dictionary<Type, UnityEngine.Object[]>();
public abstract void Init();
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($"Failed 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 Text GetText(int idx) { return Get<Text>(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;
}
}
}
UIBase 코드에서 Bind랑 다르게
public static void BindEvent는 static으로 선언하신 이유가 뭔가요 ?
나중에 작성 할 Extension 클래스가 static 이라서 인가요 ?
그리고 저는 클래스에서 사용하는 static을
전역함수인데 클래스로 범위를 한번 묶어주는 정도로 생각하는데 맞나요 ?
유니티는 클래스를 만들어도 따로 new를 써서 객체를 만드는게 아니니깐
왜 static을 쓰고 어떤건 인스턴스 함수로 놔도 되는지 헷갈립니다 ㅠㅠ
답변 1
0
public static void BindEvent는 static으로 선언하신 이유가 뭔가요 ?
나중에 작성 할 Extension 클래스가 static 이라서 인가요 ?
-> 네. Extension Method를 만들려면 그렇게 해야 하기 때문입니다.
그리고 저는 클래스에서 사용하는 static을
전역함수인데 클래스로 범위를 한번 묶어주는 정도로 생각하는데 맞나요 ?
-> 그렇게 생각할 수 있죠.
유니티는 클래스를 만들어도 따로 new를 써서 객체를 만드는게 아니니깐
왜 static을 쓰고 어떤건 인스턴스 함수로 놔도 되는지 헷갈립니다 ㅠㅠ
-> 공식처럼 뭘 해야 하는게 아니라 그냥 편한대로 하시면 됩니다...
static 없이 잘 돌아가면 굳이 사용할 필요도 없구요
ResourceManager 클래스 관련 질문
1
38
2
몬스터 HP 게이지바 이동
0
24
1
매니저 관련 질문이 있습니다.
0
74
2
비쥬얼 스튜디오에서 unity연결이 없습니다.
0
81
2
UI 기능 관련 질문이 있습니다!
0
72
2
픽셀 좌표 스크린 좌표
0
58
0
전체적으로 코드 읽는게 굉장히 오래 걸리네요...
0
108
2
카메라 #2 수업 캐릭터 쓰러짐 해결
0
97
1
플레이어 움직임이 끝날때, 기울어짐 현상
0
103
1
Input 적용과 관련하여 질문 있습니다.
0
93
1
디버그 불가.. Unity에 연결 불가
0
102
1
달리기 애니메이션 에러
0
100
2
오랜만에 게임을 만들다가 사운드 관련해서 뭔가 궁금한게 있어서 봤습니다
0
75
1
GetKey 오류
0
131
2
에셋을 어디에 올려두신지 알 수 있나요?
0
114
1
재귀호출? 오류나는데 왜 이래요 ?
0
180
6
Animation blending 파라미터 계산
0
77
1
newPos와 to지점이 겹쳐져야 하는거 아닌가요?
0
114
3
오브젝트 복제하니까 자꾸 이런게 뜨면서 꺼집니다
0
97
2
질문있습니다
0
159
2
아 이거 소스코드 같은거 없나요?
0
152
3
질문 드립니다. string.IsNullOrEmpty(name)
0
94
2
@Managers 가 씬에 배치되어 있어도 문제가 되지 않나요?
0
139
2
유니티6 NavMesh 안되는분들
1
194
1





