Created
March 19, 2023 09:17
-
-
Save malj/4b61ee251eb37f4b5c8ddf3ef82475c5 to your computer and use it in GitHub Desktop.
Unity MonoBehaviour to ECS component converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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