Unity

Unity) Serializable, SerializeField

Todah 2023. 10. 23. 21:45
반응형

Unity에서 Serialize 란?

 직렬화는 데이터 구조 또는 게임 오브젝트 상태를 Unity가 보관하고 나중에 다시 복구할 수 있는 포맷으로 변환하는 자동 프로세스다. 직렬화의 구조에 따라서 성능이 좌지우지된다고봐도 과언이 아니다.

 

 우리는 C# 스크립트의 클래스 멤버 변수를 "public" 으로 지정함으로써 유니티 에디터의 Inspector에서 해당 변수의 값을 쉽게 변경하거나 다른 클래스에서 참조할 수 있도록 하는 경우가 있다. 하지만 남발하게 된다면 다수의 인원이 협업을 통해 프로젝트를 진행하는 과정에서는 문제가 될 수 있다. 변경되지 말아야 하는 어떤 자료가 외부 객체로부터 변경될 가능성이 있기 때문이다.

 보통은 정보의 공개수준을 허가한 자료에 대해서만 외부의 접근과 조작을 허용하고, 실제로 자료가 처리되는 과정 자체는 외부에서 알 수 없도록 숨기는데, 이것을 "캡슐화"라고 한다.

 

- SerializeField

 이렇게 캡슐화된 데이터 중에서 접근자가 "private"으로 지정된 변수를 유니티 에디터 Inspector에서 변경될 수 있도록해주는 키워드가 바로 [SerializeField] 다.

 

- Serializable

 또한 System에서 제공하는 [Serializable] 키워드를 지정하면 클래스나 구조체의 정보를 Inspector에 노출시킬 수 있다.

아래와 같이 사용자 정의 클래스의 윗줄에 [Serializable] 키워드를 붙이고, Monobehaviour를 상속받고 있는 C# 스크립트의 메인 클래스에서 해당 클래스의 객체를 생성하면, 아래와 같이 Inspector에 노출되어 값을 지정할 수 있게 된다.

 

- HideInInspector

 반대로, 변수의 접근 제한자가 "public"으로 지정되어 있지만, 유니티 에디터의 Inspector에서 노출되는 것을 막기 위해서는 [HideInInspector] 키워드를 사용한다.

 주의할 점은 [SerializeField]  키워드는 키워드 아래에 있는 모든 변수를 노출시키고, [HideInInspector] 키워드는 키워드 아래에 있는 모든 변수를 노출하지 않는다.

 

 

#참조

https://m.blog.naver.com/pxkey/221307184650

 

유니티 인스펙터 「SerializeField」와 「Serializable」

안녕하세요. 창작자 픽케입니다. 객체 지향 프로그래밍(Object Oriented Programming)이 가지는 중요한 ...

blog.naver.com

https://docs.unity3d.com/kr/2018.1/Manual/script-Serialization.html

 

스크립트 직렬화 - Unity 매뉴얼

직렬화는 데이터 구조나 오브젝트 상태를 Unity 에디터가 저장하고 나중에 재구성할 수 있는 포맷으로 자동으로 변환하는 프로세스를 말합니다. Unity 에디터에서는 저장 및 로딩, 인스펙터 창, 인

docs.unity3d.com

https://forum.unity.com/threads/is-serializefield-the-same-as-making-a-variable-public.425717/

 

is SerializeField the same as making a variable public?

I'm watching a tutorial from another website and they use SerializeField rather than making the variables public like in the Unity tutorials, to make...

forum.unity.com

https://community.gamedev.tv/t/what-is-the-difference-between-serializefield-and-public/173110

 

What is the difference between [SerializeField] and public?

What is the difference between [SerializeField] and public? For example; [SerializeField] int num1 = 0; and public int num1 = 0; They both give the same output, controls in the Unity inspector tab, so what is the difference between them?

community.gamedev.tv

 

반응형

'Unity' 카테고리의 다른 글

Unity) Design Pattern  (3) 2023.10.25
Unity) Prefab  (2) 2023.10.24
Unity) Event  (2) 2023.10.20
Unity) Animator  (2) 2023.10.20
Unity) RayCast  (0) 2023.10.18