What is the current workflow for spawning prefabs as entities?
For my use case - i need to spawn missiles, each with a simple int Value on them.
What would be the correct workflow to spawn each missile, during runtime, with all the same data, except for Value, which needs to be incremented?
Im still trying to grok Authoring classes, and a lot of the information I searched is out of date and uses old hybrid stuff.
apkdev
February 7, 2025, 10:27am
2
The workflow for instantiating entities is very similar to how you instantiate GameObjects.
Take a look at the samples repository, you’ll find some goodies there. Some scenarios similar to the one you’re describing:
// Remove the NewSpawn tag component from the entities spawned in the prior frame.
var newSpawnQuery = SystemAPI.QueryBuilder().WithAll<NewSpawn>().Build();
state.EntityManager.RemoveComponent<NewSpawn>(newSpawnQuery);
// Spawn the boxes
var prefab = SystemAPI.GetSingleton<Config>().Prefab;
state.EntityManager.Instantiate(prefab, count, Allocator.Temp);
// Every spawned box needs a unique seed, so the
// seedOffset must be incremented by the number of boxes every frame.
seedOffset += count;
new RandomPositionJob
{
SeedOffset = seedOffset
}.ScheduleParallel();
// For every player, spawn a ball, position it at the player's location, and give it a random velocity.
foreach (var transform in
SystemAPI.Query<RefRO<LocalTransform>>()
.WithAll<Player>())
{
var ball = state.EntityManager.Instantiate(config.BallPrefab);
state.EntityManager.SetComponentData(ball, new LocalTransform
{
Position = transform.ValueRO.Position,
Rotation = quaternion.identity,
Scale = 1
});
state.EntityManager.SetComponentData(ball, new Velocity
{
// NextFloat2Direction() returns a random 2d unit vector.
Value = rand.NextFloat2Direction() * config.BallStartVelocity
});
}
1 Like
Thank you!
Im surprised I missed that.