⭐ 개념 정리
✅ World
ECS
에서 World
는 Entity
와 System
등을 관리하는 Container 역할
World
는 특정 씬(Scene)에 종속되지 않고, 모든 ECS 데이터를 포함하여 관리한다.
World
가 관리하는 핵심 구성 요소 4가지
EntityManager
→Entity
와Component
관리Systems
→ ECS 시스템 실행 및 업데이트Archetypes
→Entity
구조 저장 및 관리Queries
→ 특정 조건을 만족하는Entity
검색
✅ SubScene
SubScene
은 씬(Scene) 내부에서 특정 ECS 데이터(Entity
)를 포함하는 컨테이너 역할을 한다.
SubScene
이 로드되면, 그 안의 GameObject들이 자동으로 Entity
로 변환(Entity Conversion)되어
에 등록World
➡️ SubScene의 로드 과정
-
SubScene
로드 →SceneSystem
이SubScene
을
에 추가World
- GameObject →
Entity
변환 - EntityManager가 모든
Entity
관리 - ECS 시스템이 해당
Entity
를 처리
✅ World와 SubScene의 관계
는 World
MainScene
과 SubScene
을 구분하지 않고, 모든 ECS 데이터를 포함하여 관리
SubScene
이 로드되면 해당 오브젝트들이 ECS Entity
로 변환되어
에 등록됨World
자체는 씬(Scene)을 직접적으로 관리하지 않지만, World
가 관리하는 World
SceneSystem
을 사용해 SubScene
을 동적으로 로드/언로드할 수는 있음
즉,
는 씬(Scene) 개념과 독립적이지만, World
SubScene
과는 밀접한 관계를 가짐
❓ World는 SubScene을 포함하는 상위 컨테이너인가?
Yes, 하지만 직접적인 포함 관계는 아님
는 ECS 데이터를 관리하는 컨테이너이므로, World
SubScene
에서 생성된 ECS 데이터(Entity
, Component
, System
등)는 결국
에 포함됨.World
하지만, Unity의 SubScene
자체가 World의 일부는 아니다. (SubScene
은 씬(Scene)의 일부, World는 ECS의 컨테이너)
❓ 서로 다른 World에서 같은 SubScene을 공유할 수 있을까?
일반적으로 Unity ECS에서는 SubScene
은 하나의 World
에 속하는 것이 원칙
특정한 방식으로 다른 World
에서 동일한 SubScene
을 공유하는 것도 가능할 수 있음
➡️ 서로 다른 World에서 같은 SubScene을 공유하는 방법
1. 각 World에서 같은 SubScene을 개별적으로 로드
- 각
World
가 동일한SubScene
을 독립적으로 로드하여 각각의 ECS 데이터를 가질 수 있음 - 서로 다른
World
에 있지만, 동일한 형태의 ECS 데이터가 생성
2. Persistent Scene을 활용해 한 World에서 로드한 데이터를 다른 World가 참조
- 한
World
에서SubScene
을 로드하고, 다른World
는 해당 데이터를 복사하거나 참조하도록 설계 - 예를 들어, 멀티플레이어 게임에서 서버
World
와 클라이언트World
가 동일한 맵 데이터(SubScene
)를 참조하는 방식 - 하지만, Unity ECS가 기본적으로 제공하는 기능은 아니므로 별도의 데이터 동기화 로직이 필요함.
3. Bake 시점에 SubScene의 ECS 데이터를 여러 World로 복사
SubScene
이 변환될 때(Bake
), ECS 데이터를 특정 World가 아니라 여러World
에 분산해서 배치하는 방식도 가능EntityManager.CopyEntitiesFrom()
등을 사용하여SubScene
의 데이터를 한
에서 다른World
로 복사 가능.World
- Unity ECS가 기본적으로 지원하는 기능이 아니므로, 수동으로 구현해야 할 수도 있음.
❓SubScene 내의 Entity들이 서로 다른 World에 포함될 수 있을까?
Yes, 하지만 Unity ECS에서 기본적으로 지원하는 방식은 아님
특정한 방법을 사용하면 일부 Entity
를 다른 World
로 이동시키는 것은 가능함
➡️ 일반적인 SubScene의 World 포함 방식
기본적으로 SubScene
이 로드되면, 그 안의 모든 GameObject가 동일한 World
로 변환(Bake
)됨
즉, SubScene 내의 모든 Entity
들은 한 개의 World
에 속하는 것이 원칙
📌 예제: 일반적인 SubScene
로드 과정
SceneSystem.LoadSceneAsync(world, subSceneEntity, loadParameters);
특정 World
에서 SubScene
을 로드함 → SubScene
내부의 Entity
들이 해당 World
에 포함됨
결과적으로 SubScene
의 모든 Entity
들은 동일한 World
에 속하게 됨
➡️ 특정 Entity만 다른 World에 포함시키는 방법
하지만, 특정한 방식으로 SubScene
내부의 Entity
를 다른 World로 이동시키는 것도 가능
🔹방법 1: 특정 Entity를 다른 World로 복사하기
SubScene
이 로드된 후, 특정 Entity
를 다른 World
로 복사하는 방식
// 현재 World에서 Entity 가져오기 EntityManager entityManager = sourceWorld.EntityManager; EntityQuery query = entityManager.CreateEntityQuery(ComponentType.ReadOnly<MyComponent>()); // 대상 World로 Entity 복사 EntityManager destinationManager = targetWorld.EntityManager; NativeArray<Entity> entities = query.ToEntityArray(Allocator.Temp); foreach (var entity in entities) { Entity newEntity = destinationManager.Instantiate(entity); } entities.Dispose();
- 원본
Entity
를 삭제하지 않으면 동일한Entity
가 두 개의World
에 존재할 수도 있음! → 원본을 삭제할지 여부를 잘 결정해야 함
🔹방법 2: SubScene을 특정 World에서 로드한 후 일부 Entity만 다른 World에서 활용
SubScene
을World A
에서 로드- 특정
Entity
를World
B
에서 참조할 수 있도록 설계 EntityCommandBuffer
또는CopyEntitiesFrom
을 활용하여 데이터 이동 가능
targetWorld.EntityManager.CopyEntitiesFrom(sourceWorld.EntityManager, query);
- 하지만, 시스템(System)과 동작 방식이 달라질 수 있으므로 주의해야 함