Can ForEach access POCOs?

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:

using Unity.Entities;
using UnityEngine;
using UnityEngine.UI;

[DisallowMultipleComponent]
[RequiresEntityConversion]
public class ToggleSetup : MonoBehaviour, IConvertGameObjectToEntity
{
    public ToggleView ToggleView;

    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        dstManager.AddComponentData(entity, ToggleView.ToggleData);
        dstManager.AddComponentObject(entity, ToggleView);
    }
}

[System.Serializable]
public struct ToggleData : IComponentData
{
    public bool IsOn;
}

[System.Serializable]
public class ToggleView
{
    public ToggleData ToggleData;
    public Image OnImage;
    public Image OffImage;
}

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!

I’ll add that I’m aware I can do something like:

using Unity.Entities;
using UnityEngine;
using UnityEngine.UI;

[DisallowMultipleComponent]
[RequiresEntityConversion]
public class ToggleView : MonoBehaviour, IConvertGameObjectToEntity
{
    public ToggleData ToggleData;

    public Image OnImage;
    public Image OffImage;

    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        dstManager.AddComponentData(entity, ToggleData);
        dstManager.AddComponentObject(entity, this);
    }
}

[System.Serializable]
public struct ToggleData : IComponentData
{
    public bool IsOn;
}

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).

You can access MB components without problems in ForEach.
https://discussions.unity.com/t/733916 page-2#post-4478902

@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.

You mean just non MB class as Component?

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.

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.

1 Like