#How to write a prefab look-up system. Strange baked entity remapping beahviour.

1 messages · Page 1 of 1 (latest)

opaque drum
#

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);
    }
}```
unborn garnet
#

if i understand the process correctly, the baker serializes to the subscene, and after that the subscene is loaded

#

so what you are saving to the static array is what is baked to the entity scene

#

not what is loaded at runtime

#

adding the prefab entities to component data seems like the right way to go about it; if you need additional mapping then you should probably add that to some component data as well

#

of course component data can not really store things like NativeList or List so you probably need to use a buffer or FixedList to store the list of prefabs