using Unity.Entities; using Unity.NetCode; using Unity.Transforms; using UnityEngine; [UpdateInGroup(typeof(ClientSimulationSystemGroup))] public class PlayerModelSystem : ComponentSystem { protected override void OnUpdate() { Entities.WithNone<Transform>().ForEach((Entity playerEntity, ref PlayerComponent playerComponent) => { if (Prefabs.PlayerModel != null) { var playerModel = Object.Instantiate(Prefabs.PlayerModel); EntityManager.AddComponentObject(playerEntity, playerModel.GetComponent<Transform>()); EntityManager.AddComponentData(playerEntity, new CopyTransformToGameObject()); } }); } }
using Unity.Entities; using Unity.Jobs; using Unity.Mathematics; using Unity.Transforms; using UnityEngine; // Attach to your Game Object to be converted, the GameObjectPrefab is a pure Game Object with no conversion that may contain MonoBehaviour components such as the particle system. public class GameObjectPrefabAuthoring : MonoBehaviour, IConvertGameObjectToEntity { public GameObject GameObjectPrefab; public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { dstManager.AddComponentObject(entity, new GameObjectPrefab { Value = GameObjectPrefab }); } } public class GameObjectPrefab : IComponentData { public GameObject Value; } // Instantiate and destroy the referenced prefab when the entity is created or destroyed. You can even pool the Game Object. public class GameObjectPrefabSystem : JobComponentSystem { public class GameObjectPrefabInstance : ISystemStateComponentData { public GameObject Value; } protected override JobHandle OnUpdate(JobHandle inputDeps) { Entities .WithNone<GameObjectPrefabInstance>() .ForEach((Entity entity, GameObjectPrefab gameObjectPrefab) => { var gameObjectPrefabInstance = Object.Instantiate(gameObjectPrefab.Value); EntityManager.AddComponentData(entity, new GameObjectPrefabInstance { Value = gameObjectPrefabInstance }); // Just an example to make the GameObject Prefab instance follow the entity. EntityManager.AddComponentObject(entity, gameObjectPrefabInstance.transform); EntityManager.AddComponent<CopyTransformToGameObject>(entity); }) .WithStructuralChanges() .Run(); Entities .WithNone<GameObjectPrefab>() .ForEach((Entity entity, GameObjectPrefabInstance gameObjectPrefabInstance) => { Object.Destroy(gameObjectPrefabInstance.Value); EntityManager.RemoveComponent<GameObjectPrefabInstance>(entity); }) .WithStructuralChanges() .Run(); return default; } } // A dummy system for testing purposes. public class DummyEntityWithGameObjectControllerSystem : JobComponentSystem { EntityQuery m_Query; protected override JobHandle OnUpdate(JobHandle inputDeps) { var deltaTime = Time.DeltaTime; var speed = 3; var direction = new float2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); var destroy = Input.GetKeyDown(KeyCode.Delete); if (destroy) { EntityManager.DestroyEntity(m_Query); } return Entities .WithStoreEntityQueryInField(ref m_Query) .WithAll<GameObjectPrefab>() .ForEach((ref Translation translation) => { translation.Value += math.normalizesafe(new float3(direction, 0).xzy) * speed * deltaTime; }) .Schedule(inputDeps); } }