Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

ComponentDataArray -> NativeArray?

Discussion in 'Entity Component System' started by Tom01098, Apr 28, 2018.

  1. Tom01098

    Tom01098

    Joined:
    Jun 8, 2015
    Posts:
    42
    How would I pass values in a ComponentDataArray into a NativeArray for use in a job? My current approach does not change the Vector3 in the component that it should - I think this problem happens around lines 32-37.
    I believe this is because it is not a reference, but I am not sure how to go about fixing this.

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using UnityEngine;
    5.  
    6. public class CubeMoveSystem : ComponentSystem
    7. {
    8.     //Getting and injecting cubes
    9.     public struct Cubes
    10.     {
    11.         public int Length;
    12.         public ComponentArray<Transform> transforms;
    13.         public ComponentDataArray<Velocity> velocities;
    14.     }
    15.     [Inject] Cubes cubes;
    16.  
    17.     //The actual job which increases the velocity by mod
    18.     struct UpdateVelocitiesJob : IJobParallelFor
    19.     {
    20.         public NativeArray<Vector3> velocities;
    21.         public Vector3 mod;
    22.  
    23.         public void Execute(int i)
    24.         {
    25.             velocities[i] = velocities[i] + mod;
    26.         }
    27.     }
    28.  
    29.     protected override void OnUpdate()
    30.     {
    31.         //Filling the NativeArray with all the velocities from the ComponentDataArray
    32.         NativeArray<Vector3> velocities = new NativeArray<Vector3>(cubes.Length, Allocator.TempJob);
    33.  
    34.         for (int i = 0; i < cubes.Length; i++)
    35.         {
    36.             velocities[i] = cubes.velocities[i].value;
    37.         }
    38.  
    39.         //Creating the job
    40.         UpdateVelocitiesJob updateVelocitiesJob = new UpdateVelocitiesJob()
    41.         {
    42.             velocities = velocities,
    43.             mod = Velocity.mod
    44.         };
    45.  
    46.         //Doing the job
    47.         JobHandle updateVelocitiesJobHandle = updateVelocitiesJob.Schedule(cubes.Length, 1);
    48.         updateVelocitiesJobHandle.Complete();
    49.  
    50.         //Disposing the array
    51.         velocities.Dispose();
    52.     }
    53. }
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3.  
    4. [System.Serializable]
    5. public struct Velocity : IComponentData
    6. {
    7.     public Vector3 value;
    8.  
    9.     public static Vector3 mod = new Vector3(.1f, .1f, .1f);
    10. }
    11.  
    12. public class VelocityComponent : ComponentDataWrapper<Velocity> { }
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,654
    Why you simply not use ComponentDataArray<Velocity> in job?
    Code (CSharp):
    1. struct UpdateVelocitiesJob : IJobParallelFor
    2.     {
    3.         public ComponentDataArray<Velocity> velocities;
    4.         [ReadOnly] public Vector3 mod;
    5.  
    6.         public void Execute(int i)
    7.         {
    8.             var _tmp = velocities[i];
    9.             _tmp .Value = velocities[i].Value + mod;
    10.             velocities[i] = _tmp;
    11.         }
    12.     }
     
    Last edited: Apr 28, 2018
    Tom01098 likes this.
  3. Tom01098

    Tom01098

    Joined:
    Jun 8, 2015
    Posts:
    42
    ...oh, you can do that? Thought that only NativeArrays could be passed between jobs, whoops.

    Thank you!
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,654
    You can pass all blittable values in to job
     
    RaL and Tom01098 like this.