Game Dev/Unity ECS & DOTS

4. ECS 워크플로 이해하기- ECS & DOTS 완전 정복하기. Feat. 안 반가운 큐브 프로젝트

Septentrions 2024. 11. 6. 22:11

*해당 글은 Entities 1.3.5 버전, Unity 6 LTS (0.25)  버전을 기준으로 작성되었습니다.

 

이번 포스팅은 Understand the ECS workflow 의 번역 및 소개를 하려고 한다.

가장 기초적인 걸음이 중요한 법이다.

 

원문 : https://docs.unity3d.com/Packages/com.unity.entities@1.3/manual/ecs-workflow-intro.html

 

Understand the ECS workflow | Entities | 1.3.5

Understand the ECS workflow The workflow to create applications with Unity's entity component system (ECS) framework differs from the one you would use to create object-oriented Unity applications in both principle and implementation. It's useful to unders

docs.unity3d.com

 

 

 

ECS의 워크플로는 크게 4단계로 이루어진다.

 

(1) SubScene

(2) Component

(3) Entity

(4) System

 

 


 

ECS의 패키지를 설치 했으면, 어떻게 프로젝트에 적용할 것인가?에 대한 부분이다.

 

(1) Create a subscene

 

ECS uses subscenes to contain the content of your application. You add GameObjects and MonoBehaviour components to a subscene, and bakers convert the GameObjects and MonoBehaviour components into entities and ECS components.

ECS 는 Subscenes를 사용한다. GameObject과 Component 들을 SubScene에 넣으면,
Baker가 두 요소들을 엔티티화 시켜준다.

 

히어라키 창에서 우클릭을 눌러 SubScene을 추가할 수 있다.

Empty 혹은 Bake할 게임 오브젝트를 SubScene 생성과 동시에 추가가 가능하다.

 

SubScene에는 우리가 Entity로 만들 GameObject를 등록 (Authoring)하고,

플레이 모드에 진입하면서 런 타임에서 사용 가능하도록 Entity로 Baking 한다.

Entity 는 필요할 때마다 중간에 새롭게 만들어줄 수가 없다.

 

 

ECS / DOTS 를 처음 접하면서 가장 혼란스러운 건, Baker 와 SubScene 개념이다.

왜!!! 이(딴)걸 써야 하지? 라는 물음이 자연스럽게 나올 것이다.

 

참조 : https://discussions.unity.com/t/bakers-why/913893/11

다음, 나와 같은 생각을 가진 사람이 쓴 징징글(Discussion) 과 전문가들의 반박을 볼 수 있다.

(프레임워크 개발자도 있고, 굵직한 ECS 관련 논의에 빠지지 않고 나타나는 네임드들 ㄷㄷ)

Baking 워크플로에 개선 사항이 필요하지만, 런 타임 도중에 엔티티 변환 (따위)을 지원하는 것보다

좋은 방향이라고 입을 모아 말하고 있다.

 

편의성을 버릴 것인가, 유니티가 제시한 워크플로우를 동의하지 못해 수만개 오브젝트 연출을 포기할 것인가? 는 개발자의 몫이라는 사실을 알아두면 될 것 같다.

 

그러니까 내 로망인 수만개 연출을 포기하지 못하니 ECS를 공부하지!!

 

나중에 다룰 것 이지만, Baker 와 SubScene에 대한 참조도 공유한다.

https://docs.unity3d.com/Packages/com.unity.entities@1.3/manual/conversion-subscenes.html

 

Subscenes overview | Entities | 1.3.5

Subscenes overview The entity component system (ECS) uses subscenes instead of scenes to manage the content of your application. This is because Unity's core scene system is incompatible with ECS. You can add GameObjects and MonoBehaviour components to a s

docs.unity3d.com

 

https://docs.unity3d.com/Packages/com.unity.entities@1.3/manual/baking-baker-overview.html

 

Baker overview | Entities | 1.3.5

Baker overview A simple baking system uses a baker to read data from the input authoring scene, and writes the data as components on entities as the output. To create a baker, you use the Baker managed generic class and pass an authoring component as the t

docs.unity3d.com

 


(2) Create ECS Components

Components store data for your application. To create behavior in your application, systems provide logic that reads from and writes to ECS component data. The ECS workflow is data-oriented, so it's good practice to plan out your data layout and create ECS components before you work on systems or create any entities.
There are different kinds of ECS components that serve different purposes. For more information, refer to Component types.

컴포넌트들은 데이터를 저장한다. 어떤 행동을 만들기 위해서 시스템들은 ECS 컴포넌트 데이터를 읽고 쓸 수있는 로직을 제공한다. 
ECS 워크플로는 데이터 지향적이기 때문에, 컴포넌트에 들어갈 구성요소들이나 System들에 대한 설계를 잘 하는 것이 중요하다.
ECS 컴포넌트들은 여러가지 종류가 있다.

 

 

 

컴포넌트들은 일종의 데이터를 가진 꼬리표라고 이해하면 편하다.

시스템 상에서 특정 엔티티들을 가져올 때, 컴포넌트를 가진 엔티티들로 필터링해서 가져올 수 있다.

 

 

컴포넌트 타입들은 여러가지가 존재한다.

해당 사항들에 대해서는 나중에 다루도록 하겠다.

 


 

(3) Create entities

 

Entities represent distinct things that exist in an application. To create entities in the Editor, you add GameObjects to a subscene. The baking process converts these GameObjects into entities. Optionally, to attach ECS components to the converted entities, you create bakers. When you create a baker, you define which MonoBehaviour component it's for and then write code that uses the MonoBehaviour component data to create and attach ECS components to the converted entity.

You can also create additional entities from the baker and attach ECS components to them too. In this workflow, the MonoBehaviour component is called an authoring component.

엔티티들은 어플리케이션 상에서 어떠한 것들로 표현된다.
우선 오브젝트들을 SubScene에 넣으면, Baking 프로세스에서 GameObject들을 엔티티로 넘겨준다.
넘겨주면서 Components들을 부착시킬 수도 있다.
MonoBehaviour  데이터를 사용하여 변환된 엔티티에 ECS 구성 요소를 만들고 연결하는 코드를 작성한다.

베이커에서 추가 엔티티를 만들고 여기에 ECS Component를 연결할 수도 있다. 이 워크플로에서 MonoBehaviour 구성 요소를 Authoring Component 라고 한다.

 

엔티티들은 Baking 프로세스에서 생성되며

Component 뿐만 아니라 Scriptable Object,  Preafab 형태에서도 엔티티화가 가능하다.

SO, Prefab 모두 일종의 Struct라 가능한 것 같다.

 


 

(4) Create systems

 

Systems create the behavior in your application.To do this they can query and transform ECS component data, create and destroy entities, and add and remove ECS components from entities.

By default, when you create a system, Unity instantiates it and adds it to the default world.

There are different types of systems that serve different purposes. For more information, refer to System types.

시스템들은 어떠한 행동들을 만든다. 구현부 역할을 하기 위해 시스템들은 Query 와 ECS 컴포넌트 데이터 변환, 엔티티들을 생성하거나 삭제 그리고 ECS 컴포넌트들을 추가하거나 삭제할 수 있다.

기본적으로 System을 구현만 하면, 유니티는 Default World에 시스템들을 생성하고 추가한다.

여러 목적을 위해 여러 타입이 존재한다.

 

시스템들은 로직을 담당한다.

엔티티들을 Archtype 조합에 맞게 질의하여 가져오거나 (Query)

ECS 컴포넌트 데이터들을 사용할 수 있고

Entity에 대한 수정을 가할 수 있다.

 

System 들은 유니티에서 항상 돌고 있으며

조건이 맞으면 Update가 실행되거나 삭제 될 수 있다.

 

시스템들은 기본적으로 Main Thread 안에서 동기적으로 동작한다.

Burst 나 Job System을 이용하면 서브 스레드에서도 동작하도록 할 수 있다.

 

 

 

이번 포스팅에서는

ECS 워크플로에 대해서 알아봤다.

상당히 지루한 내용의 연속이고 계속 알아야 하는 단어들이 튀어나오고 있지만

반드시 숙지해야 할 내용들이니 그냥 지나치지 말자.