My other idea was this. OnUpdate is called continuously until either there is a AuthoringComponent and it isn't disabled. The only thing that's missing here is that I would've liked a way to communicate when that whole process is done..
Here the onReadyEvent callback is no longer valid...
namespace Cirrus.UnityExtras.ECS
{
using static Unity.Entities.SystemAPI;
using Color = UnityEngine.Color;
using Entity = Unity.Entities.Entity;
using Guid = Core.Guid;
public class AuthoringLibraryManagedComponent : IComponentData
{
public Action onReadyEvent;
}
public struct AuthoringLibraryComponent : IComponentData
{
public NativeHashMap<Guid, Entity> entities;
}
public partial struct AuthoringLibraryBakingSystem : ISystem
{
#if CIRRUS_UNITYEXTRAS_ECS_BURST_COMPILE
[BurstCompile]
#endif
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<AuthoringComponent>();
var em = state.EntityManager;
var entity = em.CreateEntity();
var library = new AuthoringLibraryComponent();
library.entities = new NativeHashMap<Guid, Entity>(10, Allocator.Persistent);
em.AddComponentData(entity, library);
em.AddComponentData(entity, new AuthoringLibraryManagedComponent());
}
public void OnUpdate(ref SystemState state)
{
// state.Enabled = false;
var em = state.EntityManager;
var managed = SystemAPI.ManagedAPI.GetSingleton<AuthoringLibraryManagedComponent>();
ref var library = ref SystemAPI.GetSingletonRW<AuthoringLibraryComponent>().ValueRW;
foreach (var(entry, entity) in SystemAPI.Query<RefRW<AuthoringComponent>>()
.WithOptions(EntityQueryOptions.IncludeDisabledEntities | EntityQueryOptions.IncludePrefab)
.WithEntityAccess())
{
ref readonly var value = ref entry.ValueRO;
library.entities.Add(value.guid, entity);
em.SetComponentEnabled<AuthoringComponent>(entity, false);
}
managed.onReadyEvent?.Invoke();
}
}
}