Skip to content

Instantly share code, notes, and snippets.

@malj
Created March 19, 2023 09:17
Show Gist options
  • Save malj/4b61ee251eb37f4b5c8ddf3ef82475c5 to your computer and use it in GitHub Desktop.
Save malj/4b61ee251eb37f4b5c8ddf3ef82475c5 to your computer and use it in GitHub Desktop.
Unity MonoBehaviour to ECS component converter
using Unity.Entities;
using UnityEngine;
[DisallowMultipleComponent]
public class GameObjectEntity : MonoBehaviour
{
Entity entity;
EntityManager manager;
void Awake()
{
manager = Unity.Entities.World.DefaultGameObjectInjectionWorld.EntityManager;
entity = manager.CreateEntity();
foreach (var componentData in GetComponents<IComponentData>())
{
manager.AddComponentObject(entity, componentData);
}
}
void OnDestroy()
{
try
{
manager.DestroyEntity(entity);
}
catch
{
// Entity manager has already been deallocated.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment