- Home /
Entities outside SystemBase
Is it possible to list the entities I need with components via ForEachLambdaJobDescription
Entities without the SystemBase
class?
Let's say there is some event that enumerates entities only once in a lifetime, and calling onUpdate
SystemBase
every time sounds somehow too cumbersome.
Answer by andrew-lukasik · Apr 27 at 08:19 AM
Yes. You can query and grab whatever data you need, even from a common MonoBehaviour
.
But be aware that everything Unity.Collections
( NativeArray<T>
etc) will be slow to read/write outside a job. So still prefer jobs where possible (you can start them from Monobehaviour
s too)
void Start ()
{
var world = World.DefaultGameObjectInjectionWorld;
var entityManager = world.EntityManager;
var query = entityManager.CreateEntityQuery(
typeof(RenderBounds)
);
NativeArray<Entity> entities = query.ToEntityArray( Allocator.Temp );
Entity[] entitiesArray = entities.ToArray();
NativeArray<Entity> renderBounds = query.ToComponentDataArray<RenderBounds>( Allocator.Temp );
RenderBounds[] renderBoundsArray = renderBounds.ToArray();
}
Thanks. Is it possible to achieve the same syntax as in Entities through a lambda instruction? And is it also a good practice from a performance point of view to create multiple arrays at once through ToComponentDataArray in order to work with several components at once through enumeration, and not with one?
Seems like a busywork, but syntax-wise - I can't see why not.Is it possible to achieve the same syntax as in Entities through a lambda instruction?
Allocation time is rarely an issue, but when it becomes one - you can always useis it also a good practice from a performance point of view to create multiple arrays at once through
ToComponentDataArray
in order to work with several components at once through enumeration, and not with one?
ToComponentDataArrayAsync
instead of
ToComponentDataArray
.
More important thing: if you want anything here to be fast, your best choice is to create Burst-compiled jobs and schedule that. Everything else is low-performance, in comparison.
Sidenote: you don't want to enumerate any NativeArray<T>
in Start
(it's super slow).
Your answer
![](https://koobas.hobune.stream/wayback/20220613055858im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Accessing local system ( File Browser ) 2 Answers
[ECS] Create Entities from Job ( IJobForEachWithEntity) using an EntityCommandBuffer 1 Answer
How can I get my Lives scripts working 1 Answer
Is there possible to save scene gameobject data on external file? (not in the registry) 2 Answers
Attack Script 1 Answer