Search Unity

Cannot access transformAccessArray with custom index using NativeDisableUnsafePtrRestriction

Discussion in 'Entity Component System' started by ght1875, Apr 19, 2021.

  1. ght1875

    ght1875

    Joined:
    Jul 29, 2016
    Posts:
    32
    I am using job system without dots. I use object pooling and want to skip inactive gameobjects for transform update in a job. I try to access transformAccessArray with custom index, but unity gives me error even if I use NativeDisableUnsafePtrRestriction.

    Does transformAccessArray supports NativeDisableUnsafePtrRestriction or am I doing it wrong?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    To access TransfromAccessArray in jobs, you need to use IJobParallelForTransform, can't use other job types.
    Example:
    Code (CSharp):
    1.       [BurstCompile]
    2.       private struct SyncPositionToTransformJob : IJobParallelForTransform {
    3.          [ReadOnly]
    4.          [DeallocateOnJobCompletion]
    5.          public NativeArray<Entity> Entities;
    6.  
    7.          [ReadOnly]
    8.          [NativeDisableParallelForRestriction]
    9.          public ComponentDataFromEntity<SyncPositionToTransform> TagData;
    10.  
    11.          [ReadOnly]
    12.          [NativeDisableParallelForRestriction]
    13.          public ComponentDataFromEntity<Position> PositionArray;
    14.  
    15.          public void Execute(int index, [ReadOnly] TransformAccess transform) {
    16.             Entity entity = Entities[index];
    17.             if (!TagData.HasComponent(entity)) return;
    18.             if (!PositionArray.HasComponent(entity)) return;
    19.  
    20.             Position data = PositionArray[entity];
    21.             transform.position = data.Value;
    22.          }
    23.       }
    (Instead of entities / CDFE, it could be any data you want to pass to the job)

    And to schedule:
    Code (CSharp):
    1.          SyncPositionToTransformJob syncPosJob = new SyncPositionToTransformJob
    2.                                                  {
    3.                                                     Entities = AlignedEntities,
    4.                                                     TagData = tagData,
    5.                                                     PositionArray = positions
    6.                                                  };
    7.        
    8.          Dependency = syncPosJob.Schedule(TransformArray, Dependency);
    (if you don't have any other dependencies - leave Dependency as default, or don't specify it as second parameter)
     
    futurlab_peterh likes this.
  3. ght1875

    ght1875

    Joined:
    Jul 29, 2016
    Posts:
    32
    I see. There is no way to use custom index to access transformArray. Skip transform update in Execute with custom logic instead. Thanks.
     
    poritoshdavid likes this.