So ive been trying to implement a little experiment using SubScenes and LiveConvertGameView where Im able to change my gameobject and generate some entities and update them with some data while the game is running. Problem is I keep moving 1 step forward then 5 steps back. So ive decided to take out all the complex stuff and just write very simple system to get some feedback to make sure the basice are good. Please leave any comment and dont assume I know anything.
I would also like to know give that im changing my gameObject during runtime where my EntityQuery should sit, currently they are in Update() , is this expensive , can they just be in OnCreate(). Same goes for SetFilterChanged().
Thanks in advance.
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using UnityEngine;
namespace Check.Yourself
{
//
//IComponentData
struct EntityCountComponent : IComponentData
{
public int entityCount;
}
struct EntityDataComponent : IComponentData
{
public int entityData;
}
//
// GameObjectConversionSystem
public class ConversionSystem : GameObjectConversionSystem
{
protected override void OnUpdate()
{
this.Entities.ForEach((TestProxy data) =>
{
Debug.Log("<b> <size=13> <color=#9DF155>Info : 1 ConversionSystem : Converting Entities.</color> </size> </b>");
var ePrimary = this.GetPrimaryEntity(data);
var cData = new EntityCountComponent
{
entityCount = data.numEntities
};
this.DstEntityManager.AddComponentData(ePrimary, cData);
});
}
}
//
// ComponentSystem used to create Entities
public class CreateSystem : ComponentSystem
{
private EntityQuery qEntityCount;
private EntityQuery qEntityData;
private EntityArchetype aEntityData;
protected override void OnCreate()
{
aEntityData = EntityManager.CreateArchetype
(
ComponentType.ReadOnly<EntityDataComponent>()
);
}
protected override void OnUpdate()
{
qEntityData = GetEntityQuery
(
typeof(EntityDataComponent)
);
qEntityCount = GetEntityQuery
(
ComponentType.ReadOnly<EntityCountComponent>()
);
qEntityCount.SetFilterChanged(typeof(EntityCountComponent));
Entities.With(qEntityCount).ForEach((ref EntityCountComponent c) =>
{
Debug.Log("<b> <size=13> <color=#9DF155>Info : 2 CreateSystem : Create Entities.</color> </size> </b>");
EntityManager.DestroyEntity(qEntityData); // NOT VERY HAPPY WHERE THIS IS SITTING
NativeArray<Entity> e = new NativeArray<Entity>(c.entityCount, Allocator.TempJob);
EntityManager.CreateEntity(aEntityData, e);
e.Dispose();
});
}
}
//
// JobComponentSystem to update data on Entities
[UpdateAfter(typeof(CreateSystem))]
public class UpdateSystem : JobComponentSystem
{
private EntityQuery qEntityData;
[BurstCompile]
struct SetData : IJobForEachWithEntity<EntityDataComponent>
{
public void Execute(Entity entity, int index, ref EntityDataComponent c)
{
DebugInfo();
c.entityData = 11;
}
[BurstDiscard]
private void DebugInfo()
{
Debug.Log("<b> <size=13> <color=#9DF155>Info : 3 SetDataSystem : Setting Data .</color> </size> </b>");
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
qEntityData = GetEntityQuery
(
typeof(EntityDataComponent)
);
qEntityData.SetFilterChanged(typeof(EntityDataComponent));
inputDeps = new SetData
{
}.Schedule(this, inputDeps);
return inputDeps;
}
}
}