Hi,
I have a simple question about Events in ECS. In my opinion, events are a great method of dividing UI inputs to the real core. But what method is the best to implement them in ECS?
Is it best to simply stick to UnityEvent?
Or is it better to have a “Event” Entity, which holds Components for one frame?
For UI events I’m using a specific system (that runs after simulation group) and catches structural changes then sends an event to MonoBehaviour side. (.AddComponentObject allows to use MonoBehaviours as part of system query)
UnityEvent, UniRx, or even Action or event delegate should work just fine if you query as .WithStructuralChanges().WithoutBurst().Run(). But, make sure you’re not stalling jobs by running this system.
This is where AfterSimulationGroup comes into play, can be defined like so:
using Unity.Entities;
namespace EntitiesExt.SystemGroups {
/// <summary>
/// Update group that runs in the simulation group, but after all ECS system / jobs ran / scheduled.
/// This is done in PreLateUpdate
/// Use this one for the hybrid / MonoBehaviour bridged systems
/// </summary>
[UpdateInGroup(typeof(SimulationSystemGroup), OrderLast = true)]
[UpdateAfter(typeof(EndSimulationEntityCommandBufferSystem))]
public class AfterSimulationGroup : ComponentSystemGroup { }
}
Both approaches valid though. See what fits best for your project.
1 Like