Search Unity

Hybrid ECS Transform Synchronization

Discussion in 'Entity Component System' started by OFFICIAL_bryanw, Jan 25, 2019.

  1. OFFICIAL_bryanw

    OFFICIAL_bryanw

    Joined:
    Jul 6, 2018
    Posts:
    36
    Hey All,

    Is there a simple way to make it so that a GameObject's transform gets synchronized with the associated entity's Position/Rotation component datas?

    Thanks,
    Bryan
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Yes. But may be little tricky. Look for keyword in this ECS forum, tween, tweening. You will get some idea, to start of.
     
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    It's super easy, just add either of these components depending on the direction you want to copy

    CopyTransformFromGameObject for copying the GameObject to Position/Rotation components
    CopyTransformToGameObject for copying the Position/Rotation components to GameObject

    Found in Unity.Transforms.Hybrid
     
  4. OFFICIAL_bryanw

    OFFICIAL_bryanw

    Joined:
    Jul 6, 2018
    Posts:
    36
    Thanks man!
     
  5. LittleGeek

    LittleGeek

    Joined:
    Oct 18, 2013
    Posts:
    7
    How does this work with the GameObjectEntity? Isn't that the preferred way to work Hybrid? GameObjectEntity makes a Transform component in the ECS entity that you can manipulate.
     
  6. dudleyhk

    dudleyhk

    Joined:
    Sep 27, 2017
    Posts:
    14
    Hey, @tertle have you found there is GC.Collect spike in the Profiler, when using the CopyTransformToGameObject?
     
  7. kro11

    kro11

    Joined:
    Sep 23, 2019
    Posts:
    105
    I have the same issue. There are about 1000 objects with CopyTransformToGameObject and CopyTransformFromGameObject and it takes more then 1ms. Spikes appears randomly (possibly with a large number of operations per minute, adding a parent/child to this object). And a spike time is always the same and depends on the total number of these CTTGO/CTFGO entities. For example, if you have 10 thousand entities with CTTGO/CTFGO and you will add a parent to one of them, will cause a lag of 10ms.

    Is this the way it should be or am I doing something wrong, maybe someone knows?
     
  8. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    CopyTransform behaviours have been more or less replaced by the concept of hybrid components. Use those instead.

    That being said, using hybrid components is always going to be slow. If you want real scalability get rid of your monobehaviours/gameobjects.
     
    Nyanpas likes this.
  9. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Internally CopyTransformToGameObjectSystem does this:
    Code (CSharp):
    1. protected override JobHandle OnUpdate(JobHandle inputDeps)
    2.         {
    3.             var transforms = m_TransformGroup.GetTransformAccessArray();
    4.             var copyTransformsJob = new CopyTransforms
    5.             {
    6.                 LocalToWorlds = m_TransformGroup.ToComponentDataArrayAsync<LocalToWorld>(Allocator.TempJob, out inputDeps),
    7.             };
    8.  
    9.             return copyTransformsJob.Schedule(transforms, inputDeps);
    10.         }
    GC comes from .GetTransformAccessArray(), which generates new transform access array if old one is invalid (when number of transforms != entities with transforms in query changes).

    Best way to avoid it - allocate single transform access array, and do not alter it.

    Here's a thread about it, maybe you'll find something useful.
    But in any case this requires custom system to sync TRS around.
    https://forum.unity.com/threads/imp...form-system-performance.1048907/#post-6784835
     
    Last edited: Feb 11, 2021
    dudleyhk and kro11 like this.