Search Unity

Help with CopyFromComponentDataArray

Discussion in 'Entity Component System' started by xindexer2, Aug 21, 2021.

  1. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    I am instantiating my prefabs with this:

    Code (CSharp):
    1. entityManager.Instantiate(prefab, entityNativeArray);
    I have a job that returns a LocalToWorld array (NativeArray<float4x4>) that is the same length as the instantiated array.

    How do I copy the LocalToWorld data to my entities without looping through them?

    I have seen this in a SystemBase class, but how do I pass the NativeArray<float4x4> data from my job to the SystemBase class?

    Code (CSharp):
    1. EntityQuery updatePos = GetEntityQuery(
    2.      typeof(LocalToWorld)
    3. );
    4.        
    5. updatePos.CopyFromComponentDataArray(localToWorld);
    Is there a way to do this without using the EntityQuery?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    Code (CSharp):
    1. Job.WithCode(() =>
    2. {
    3.     for (int i = 0; i < entityNativeArray.Length; i++)
    4.         SetComponent(entityNativeArray[i], new LocalToWorld { Value = ltwArray[i] });
    5. }.Schedule();
     
  3. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    My NativeArrays are currently variables in a Monobehaviour class. How do I get my NativeArrays into a systemBase class?

    I can't instantiate a SystemBase. I guess this is more of a how do I pass data around question. Do I just make an IComponentData entity and save it there? (over 100,000 elements in my arrays).

    Edit with a little more detail

    My main class is a MonoBehaviour class.

    1. Load and parse a large JSON file.
    2. Instantiate a new class "Calculate" in order to run my IParallelFor jobs to convert the JSON data into LocalToWorld data. This is passed back to my MonoBehaviour class.
    3. Instantiate a new class to create the prefab and instantiate my entities. this class passes the entityNativeArray back to my MonoBehaviour class
    4. Now I need to add the LocalToWorld data to the entities. My current solution is to just loop through them and combine the two in a job. It works, but it would be nice to do this in one step using CopyFromComponentDataArray.
     
    Last edited: Aug 21, 2021
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,270
    Someone with more experience interopping MonoBehaviour and Entities will have to help you, because I really don't understand your architecture and consequently don't feel comfortable suggesting an API pathway. I only use MonoBehaviours for authoring or as hybrid components, but all logic is driven by systems.
     
  5. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
    You can use:
    Code (CSharp):
    1. var system = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<SystemYouWant>();
    2.             system.SystemNativeArray = YourNativeArray;
    In the example, it's a field in the system, but it surely can be a method with a parameter. It's not a very clean solution but you can definitely try it this way or just use ECB inside a job to iterate and fill values.
     
  6. xindexer2

    xindexer2

    Joined:
    Nov 30, 2013
    Posts:
    78
    @Krajca - thank you

    That's what I was looking for
     
    Last edited: Aug 21, 2021
    Krajca likes this.