Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. 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.

  1. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    292
    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:
    337
    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:
    292
    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:
    337
    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:
    292
    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:
    4,088
    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:
    243
    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:
    292
    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") :)