강의

멘토링

커뮤니티

Inflearn Community Q&A

mocusura121414's profile image
mocusura121414

asked

[MMORPG Game Development Series with C# and Unity] Part3: Unity Engine

UI Automation #2

Util.FindChild<T>(gameObject, names[i], true)와 관련하여...

Resolved

Written on

·

527

3

안녕하세요! 강의 복습 중에 또 질문이 생겨 질문드립니다!

이전 강의에서 다른 분 질문을 통해 componet.name은 결국 Button, Text 등 T 타입의 클래스를 컴포넌트로 들고 있는 게임오브젝트의 이름을 가져온다는 것은 이해가 되었습니다.

그렇다면 Util.FindChild<T>(gameObject, names[i], true) 함수의 return 값이 T component인데

디버깅한 장면에서  _objects 딕셔너리의 Value 값이  ("PointButton{Unity.Engine.UI.Button}")로 되어 있는 건 사실은 Button Component를 return 했지만 걔는 PointButton꺼야 라는 뜻인가요..?

Get 함수를 만들 때 return 값을 다시 T로 캐스팅 하는 것을 미루어 보아

Bind 할때 objects[i] = Util.FindChild<T>(gameObject, names[i], true)에서 자동으로 Object로 캐스팅 되었다가(부모 클래스이기 때문에..?)

다시 꺼내 쓸때는 T로 캐스팅 해야하는 건가요..?

C#unity

Answer 2

1

Rookiss님의 프로필 이미지
Rookiss
Instructor

복잡하게 생각하실 것 없이,
유니티에서 어지간한 리소스는 UnityEngine.Object를 상속받고 있습니다.
return을 다시 T로 캐스팅하는 이유는
Dictionary의 value를 Object[]로 관리하고 있기 때문입니다.
타입만 확실하다면 업캐스팅/다운캐스팅은 자유자재로 해도 무방합니다.
마치 Player, Monster와 같은 애들을 Creature로 묶어서 관리하는 것과 유사합니다.

디버깅한 장면에서  _objects 딕셔너리의 Value 값이  ("PointButton{Unity.Engine.UI.Button}")로 되어 있는 건 사실은 Button Component를 return 했지만 걔는 PointButton꺼야 라는 뜻인가요..?

네!

Bind 할때 objects[i] = Util.FindChild<T>(gameObject, names[i], true)에서 자동으로 Object로 캐스팅 되었다가(부모 클래스이기 때문에..?) 다시 꺼내 쓸때는 T로 캐스팅 해야하는 건가요..?

네!

0

mocusura121414님의 프로필 이미지
mocusura121414
Questioner

감사합니다!

mocusura121414's profile image
mocusura121414

asked

Ask a question