Search Unity

  1. Going to #UniteBerlin? Bring a buddy & get 25% off the 2nd ticket! More info here.
    Dismiss Notice
  2. Famed game designer and creator of The Sims, SimCIty, and Spore is looking for a 3D artist to join their team. Join the challenge.
    Dismiss Notice
  3. Unity 2017.4 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. We've opened a new World Building sub-forum to faciltate all your world building discussions! Drop in and ask any ProBuilder, ProGrids, or PolyBrush questions.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

ECS. Bug or feature.

Discussion in 'Entity Component System and C# Job system' started by eizenhorn, Apr 16, 2018 at 11:55 AM.

  1. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    34
    Hello! I discovered some strangeness. If we instantiate the object in the old-fashioned way (UnityEngine.Object) and it has the necessary components, then ComponentSystem correctly finds the corresponding groups of objects (example code below):

    Code (CSharp):
    1. public class SomeManager : MonoBehaviour
    2. {
    3.     public GameObject prefab;
    4.  
    5.     void DoStuff()
    6.     {
    7.         Instantiate(prefab);
    8.     }
    9. }
    10.  
    11. public class SomeSystem : ComponentSystem
    12. {
    13.     struct SomeGroup
    14.     {
    15.         public ComponentArray<SomeOldComponent> SomeOldComponent;
    16.         public EntityArray              Entity;
    17.         public int                      Length;
    18.     }
    19.  
    20.     [Inject] SomeGroup someGroup;
    21.  
    22.     protected override void OnUpdate()
    23.     {
    24.         Debug.Log(someGroup.Length); // <- if "prefab" (higher) has Some Component then Length more than 0
    25.     }
    26. }
    If we first instantiate an object and then add a component to it, then ComponentSystem does not see this object:

    Code (CSharp):
    1. public class SomeManager : MonoBehaviour
    2. {
    3.     public GameObject prefab;
    4.  
    5.     void DoStuff()
    6.     {
    7.         GameObject tmp = Instantiate(prefab);
    8.         tmp.AddComponent<SomeOldComponent>();
    9.     }
    10. }
    11.  
    12. public class SomeSystem : ComponentSystem
    13. {
    14.     struct SomeGroup
    15.     {
    16.         public ComponentArray<SomeOldComponent> SomeOldComponent;
    17.         public EntityArray              Entity;
    18.         public int                      Length;
    19.     }
    20.  
    21.     [Inject] SomeGroup someGroup;
    22.  
    23.     protected override void OnUpdate()
    24.     {
    25.         Debug.Log(someGroup.Length); // <- if "prefab" (higher) hasn't Some Component, and we add them after Instantiate, then Length equal 0
    26.     }
    27. }
     
  2. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    193
    There is no auto sync. You have to add the components to the entity as well through the EntityManager
     
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    34
    Then, with Instantiate, the objects would not be added to the group, which means that with Instantiate (old-school style) there is a synchronization point and can also be in the old-school addition of the component. And I advise you to pay attention to the code - it does not use ECS instantiate.
     
  4. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    193
    The GameObjectEntity Component adds all ECS Components of that GameObject at Start/Awake Method (don't know exactly) to the ECS.

    You can take a look into this component by clicking the GameObjectEntity in the inspector. It will show up the GameObjectEntity.cs script in the Project view.
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    34
    You did not understand a bit (or I incorrectly explained), I'm aware of how GameObjectEntity works (adds components to EntityManager in OnEnable), I'm just talking about why Unity did not add an event to add a component that would catch the change in the composition of the object's components for hybrid approach.
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    3,876
    We are planning to add support for it. But it requires new events to be exposed in the core engine.
     
    optimise likes this.
  7. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    134
    Hopefully I can see the components of Game Object at Inspector and the components of Entity at Entity Debugger sync seamlessly soon.
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    34
    That's all I wanted to hear! Look forward to. Thank you! And we are waiting for the release of 2018.1 :) :) As I suspect, b14 can not wait until the release (you said that in the new version of the ECS will be "out of the box" and will not need a "specific version") :)