I'm loading data from an external source [call it an array of vector3 locations for the sake of this question]. I want to use it to spawn multiple instances of an entity with a renderer and some other components attached.
I have the components and systems already and I can create a GameObject in the editor (with appropriate authoring components), attach it to the subscene and get it picked up by ECS.
The problem is... I don't actually want the one in the editor, I want several thousand at locations that vary over time.
My understanding is that in an ideal, clean world, I'd create an archetype that encapsulates all the components auto-converted from the GameObject, instantiate lots of entities and set their component values as needed.
archetype = entityManager.CreateArchetype(
typeof(RenderMesh),
typeof(LocalToWorld),
typeof(RenderBounds),
typeof(ResourceProducer), // Mine
typeof(ResourceStockpile), // Mine
typeof(PerInstanceCullingTag), // Added after seeing them in the inspector
typeof(WorldToLocal_Tag), // on the "real" entity created from the
typeof(BlendProbeTag), // GameObject.
typeof(Simulate) // May not be required.
);
I can instantiate that archetype and set the location/mesh/material but I see nothing rendered.
NativeArray<Entity> output = new NativeArray<Entity>(count, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
entityManager.CreateEntity(archetype, output);
for (int i = 0; i < count; i++) {
Entity entity = output[i];
localToWorld[i] = new LocalToWorld {
Value = float4x4.TRS(
positions[i],
quaternion.LookRotationSafe(math.forward(), math.up()),
new float3(1f, 1f, 1f))
};
entityManager.SetSharedComponentManaged(entity, new RenderMesh {
mesh = mesh,
material = material,
});
entityManager.SetComponentData<ResourceProducer>(entity, new ResourceProducer {
Metal = Mathf.Max(0, UnityEngine.Random.Range(-50, 10)),
Polymer = Mathf.Max(0, UnityEngine.Random.Range(-50, 10)),
Ceramic = Mathf.Max(0, UnityEngine.Random.Range(-50, 10)),
Food = (i == 0 ? 150 : 0),
Water = Mathf.Max(0, UnityEngine.Random.Range(-50, 10)),
});
}
[Which shows me an archetype with matching components and the right entity count in the archetypes inspector]
Conversely, I'm willing to have a prefab if it's unavoidable, but how do I mark a specific GameObject as an archetype to reference elsewhere? Does it have to be attached to the subscene? If so, how do I suppress the template object/entity?
Bakers appear to get access to a GetEntity method which I haven't seen implemented/exposed elsewhere.
I'd expose a field of type Entity and try dragging a GameObject there, but the Entity type isn't supported in the editor.
[Tangentially, is there any way I can see the raw table of entity Ids and data for a particular component? Especially entities not converted from GOs. Then I could at least validate component values are being set correctly]