Search Unity

Can ForEach access POCOs?

Discussion in 'Entity Component System' started by timmehhhhhhh, May 6, 2019.

  1. timmehhhhhhh

    timmehhhhhhh

    Joined:
    Sep 10, 2013
    Posts:
    157
    I've been stuck in 2018.3 and was finally able to dip back into the latest releases recently, but unable to find a quick answer to this question.

    I'm trying to get a feel for the current state of authoring, something like:


    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. [DisallowMultipleComponent]
    6. [RequiresEntityConversion]
    7. public class ToggleSetup : MonoBehaviour, IConvertGameObjectToEntity
    8. {
    9.     public ToggleView ToggleView;
    10.  
    11.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    12.     {
    13.         dstManager.AddComponentData(entity, ToggleView.ToggleData);
    14.         dstManager.AddComponentObject(entity, ToggleView);
    15.     }
    16. }
    17.  
    18. [System.Serializable]
    19. public struct ToggleData : IComponentData
    20. {
    21.     public bool IsOn;
    22. }
    23.  
    24. [System.Serializable]
    25. public class ToggleView
    26. {
    27.     public ToggleData ToggleData;
    28.     public Image OnImage;
    29.     public Image OffImage;
    30. }
    31.  
    I'd then have a system for reacting to changes in toggle data and playing an animation or just setting the images to on or off or w/e. But it seems we still can't `ForEach` over regular classes like ToggleView. I was under the impression this would be possible (or we'd get an interface) for doing this, it seems silly to have to stick to Unity Components when all you want is a reference to something. Is this still not possible or is there something that I'm missing or misunderstanding here? Cheers!
     
  2. timmehhhhhhh

    timmehhhhhhh

    Joined:
    Sep 10, 2013
    Posts:
    157
    I'll add that I'm aware I can do something like:


    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. [DisallowMultipleComponent]
    6. [RequiresEntityConversion]
    7. public class ToggleView : MonoBehaviour, IConvertGameObjectToEntity
    8. {
    9.     public ToggleData ToggleData;
    10.  
    11.     public Image OnImage;
    12.     public Image OffImage;
    13.  
    14.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    15.     {
    16.         dstManager.AddComponentData(entity, ToggleData);
    17.         dstManager.AddComponentObject(entity, this);
    18.     }
    19. }
    20.  
    21. [System.Serializable]
    22. public struct ToggleData : IComponentData
    23. {
    24.     public bool IsOn;
    25. }
    But the documentation on IConvertGameObjectToEntity states "The purpose of this class is to store data for authoring purposes - it is not for use while the game is running", which is exactly what I'm wanting to use it for (and what Proxies components and GameObjectEntity did well).
     
    Last edited: May 6, 2019
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
  4. timmehhhhhhh

    timmehhhhhhh

    Joined:
    Sep 10, 2013
    Posts:
    157
    @eizenhorn I guess I wasn't clear, that's what I'm trying to avoid. I'd like to use the MonoBehaviours to setup the entity and ideally also act as a binding so we can modify the values in the inspector - essentially, the ComponentDataProxy workflow. But sometimes I want (need) those components to store references to things like Unity Components. Until now I've just used MonoBehaviours + GameObjectEntity for this, I'm wondering if it's possible yet to use regular classes or structs as ComponentObject - it seems adding works but then iterating for ForEach in a ComponentSystem throws the same error that you must use Unity Component.
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,684
    You mean just non MB class as Component?
     
  6. timmehhhhhhh

    timmehhhhhhh

    Joined:
    Sep 10, 2013
    Posts:
    157
    yes exactly - there was mention we may get an interface for this at some point, i'm wondering if it exists or is possible yet.
     
  7. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Funnily enough, the Component requirement is only tested within the TypeManager safety checks but it works in a build where those checks are compiled out.

    No idea if it might fail under some situations but I was able to ForEach over a normal class and modify it in a release build.
    You can comment out the check on lines 1194 and 1195 of TypeManager in Entities .31 to make it work in the Editor.
     
    timmehhhhhhh likes this.