Hybrid ECS

그렇다면 ECS를 프로젝트에 적용하려면 어떻게 해야할까?

원래 사용하던 전통적인 방식인 GameObject & MonoBehavior 을 완전히 없애지 않고 ECS 처럼 사용하는 방법이 있다. New way of CODING in Unity! ECS Tutorial 참고

원래는 speed 변수와 rotation을 갱신해주는 동작이 함께 있다.

using UnityEngine;

public class Rotator : MonoBehaviour
{
  public float speed;
  
  // Update is called once per frame
  void Update()
  {
    transform.Rotate(0f, speed * Time.deltaTime, 0f);
  }
}

Data는 남기고 ComponentSystem을 상속받은 클래스를 생성해 Behavior를 분리한다.

// 데이터와 ComponentSystem 분리
using UnityEngine;
using Unity.Entities;

public class Rotator : MonoBehaviour
{
  public float speed;
}

class RotatorSystem : ComponentSystem
{
  struct Components
  {
    public Rotator rotator;
    public Transform transform;
  }

  // Will run every frame
  protected override void OnUpdate()
  {
    // 영상 설명엔 이렇게 되어있었는데 실제로 해보니 컴파일 에러가 ..
    // 일단 이런식으로 behavior를 분리하여 data에 접근
    foreach ( var e in GetEntities<Components>())
    {
       e.transform.Rotate(0f, speed * Time.deltaTime, 0f);
    }
  }
}

이런 식으로 하면 Pure 한 ECS 방식은 아니지만 기존의 Monobehavior를 완전히 없애지 않고 가지고 갈 수 있음.


Entity Debugger

Entity는 Hierarchy에서 확인할 수 없다. 대신 Entity Debugger 라는 것이 있음.

아래와 같이 3개의 큐브 Entity들이 회전하는 프로젝트를 실행시키면

다음과 같은 Entitiy Debugger 화면을 확인할 수 있다.

  • 제일 왼쪽은 System들을 보여준다.

  • 해당 System의 영향을 받는 Entity들이다. 3개 Entity가 있는 것을 확인할 수 있다.

  • 사용되고있는 Chunk개수를 확인할 수 있다.

관련 내용으로 여기서 더 자세히 잘 설명해주심…

 

참고 페이지들

 

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기