I'm trying to implement a way to bake a bunch of prefabs and assigned them to slots so I can spawn them by id.
But I'm seeing some strange behaviour with the prefabs. The one I get from the baker changes somewhere magically. Like if I do something like this where I store the entity in the entity in a list and in a component, the one in the component changes at some point to a new entity. And the one that I orginally assigned to the component doesn't have any of the components I baked (even though it did). What would be a correct way to implement this kind of pattern.
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
public struct TestSpawner : IComponentData {
public int index;
public Entity Prefab;
}
public class TestSpawnerAuthoring : MonoBehaviour {
public GameObject prefab;
private class Baker : Baker<TestSpawnerAuthoring> {
public override void Bake(TestSpawnerAuthoring authoring) {
TestSpawnSystem.prefabs.Add(GetEntity(authoring.prefab, TransformUsageFlags.Dynamic));
var index = TestSpawnSystem.prefabs.Count - 1;
AddComponent(GetEntity(TransformUsageFlags.None), new Spawner {
bid = index,
Prefab = TestSpawnSystem.prefabs[index]
});
}
}
}
public partial class TestSpawnSystem : SystemBase {
public static List<Entity> prefabs = new();
protected override void OnUpdate() {
var spawner = SystemAPI.GetSingleton<Spawner>();
var prefab = prefabs[spawner.bid];
if (prefab != spawner.Prefab) {
Debug.Log($"{prefab} != {spawner.Prefab}");
}
EntityManager.Instantiate(spawner.Prefab);
}
}```