#Beginner: Spawn prefab

1 messages · Page 1 of 1 (latest)

raven gull
#

So yeah, I wanna spawn a prefab here's what I got (Note: my prefab is just a cube with a mesh renderer and mesh filter):

using Unity.Entities;
using UnityEngine;

public class SpawnerAuthoring : MonoBehaviour {
    public GameObject prefab;
}

public class SpawnerBaker : Baker<SpawnerAuthoring> {
    public override void Bake(SpawnerAuthoring authoring) {
        Entity entity = GetEntity(TransformUsageFlags.Dynamic);
        AddComponent(entity, new SpawnerComponent {
            prefabEntity = GetEntity(authoring.prefab, TransformUsageFlags.Dynamic)
        });
    }
}

public struct SpawnerComponent : IComponentData {
    public Entity prefabEntity;
}

and then I attempt to spawn it in:

using Unity.Entities;
using Unity.Transforms;
using Unity.Burst;
using Unity.Mathematics;
using UnityEngine;


[BurstCompile]
public partial struct _TestSpawner : ISystem{
    private EntityManager entityManager;
    
    public void OnCreate(ref SystemState state){
        entityManager = state.EntityManager;

    public void OnUpdate(ref SystemState state){

        foreach (RefRO<SpawnerComponent> spawner in SystemAPI.Query<RefRO<SpawnerComponent>>()){
            Entity prefab = spawner.ValueRO.prefabEntity;
            Entity instance = entityManager.Instantiate(prefab);
            entityManager.SetComponentData(instance, LocalTransform.FromPosition(new float3(i * 5, 0, 0)));
            }
        }

        state.Enabled = false;
    }

    public void OnDestory(ref SystemState state){
        
    }
    
}

it does successfully spawn into the entity hierarchy, but nothing actually renders (no cube, no mesh, just empty space) what's going on here?

nimble thorn
#

Have you installed the entities graphics package?

#

Do things render if you just put them in a subscene

raven gull
#

oh, that's probably it notlikethis

nimble thorn
#

Side note, your burst compile attribute goes on onupdate not on the system struct

raven gull
#

oh right, thanks for pointing that out

raven gull
nimble thorn
#

So the default rule of burst is you need to do that

#

For like burst function pointers etc

#

A while ago though for convenience they removed the need to put it on the struct of an isystem

#

Its just a special case

raven gull
#

ohhh, yeah that makes sense, the docs examples did seem to be outdated in some cases

#

it's prob still fine just leaving it there