#MonoBehaviour references in Components

1 messages · Page 1 of 1 (latest)

final plover
#

What happened to the whole Hybrid workflow? I'm not sure I fully understand the new one. Say I have a GameObject with some MB on it. How can I make it "stay" as a GO while keeping a ref to the MB in a managed component? Is it even possible?

So far I've pretty much exclusively not needed the original behaviour and could just replicate it in ECS instead.

Using an example: Currently my solution to keep a reference to a camera in a subscene is to have a baker for UnityEngine.Camera that creates an entity with a NewCamera component. Then in runtime I spawn one in for each NewCamera component (and subsequently remove the component), and set up proper references. Obviously this isn't really flexible as I'd have to copy all data over manually. Is there a better way? How do you manage these sort of references?

hexed nest
#

bump

#

is it even possible anymore?

fallen shuttle
hexed nest
#

singletons are easy

#

but for non-singletons, I found a hack where you can convert your gameobject into entity yourself on OnEnable, and then not destroying the monobehaviour

#

I don't know the performance cost of accessing EntityManager from outside of ECS though. but it's pretty scalable. you could use managed ComponentData and even store events in them

#
public struct PlayerComponentData : IComponentData {
    public float someValue;
}
public class Player : MonoBehaviour {
    EntityManager _entityManager;
    Entity _entity;

    void OnEnable() {
        _entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
        _entity = _entityManager.CreateEntity();
        _entityManager.AddComponentData( _entity, new PlayerComponentData() );
    }

    [ContextMenu("Use Data")]
    public void UseData() {
        var data = _entityManager.GetComponentData<PlayerComponentData>( _entity );
        Debug.Log( data.someValue );
    }
}
#

one thing to keep in mind is that the new entity will not live inside any subscene, so it won't destroy along with it either

pearl socket
#

not only it's extremely slow, but it's also results in tons of wasted chunks

#

there are 2 viable ways I'm aware of for using MonoB hybrids:

  1. Custom implementation, where you store prefab in managed component
  2. CompanionLink, where you use reflection and other hacks to force Unity to use it's old hybrid implementation
hexed nest
pearl socket
#

and assuming you have a lot of such components - you'll get unique archetype per each combination = tons of wasted chunks

hexed nest
#

or is it the adding of entity

pearl socket
hexed nest
#

ah. I dunno, it can't be that bad, ? we already add component using Entity Manager in Systems. same api, why should it be so much worse

#

the state.EntityManager

#

source gen?

pearl socket
#

In worst case scenario each structural change will create new unique archetype that will be empty until World disposal, but will be queried all the time

#

so let's say 10 unique prefabs with 5 such components = minimum 50 wasted chunks

#

and once again minimum, assuming all of those adds are sorted (and they won't be)

hexed nest
#

so you mean generally shouldn't add component to entities outside of bakers?

pearl socket
#

if for some reason you must do that - you can mitigate that trouble by storing all component data beforehand

#

writing all unique components to add in a typeset

#

add it in one operation

#

and then set all data

#

(won't work with shared comps)

hexed nest
#

ah right 👍

pearl socket
#

in other words - recreate bakers

hexed nest
#

thanks for the info mate

flint hamlet
pearl socket
#

and no, command buffers are same thing, but slower

flint hamlet
#

so createentity(archetype) is a best way?

pearl socket
#

YOu shouldn't create entities manually at all

#

if you do that - it's likely that you are doing something wrong

#

singletons and primitive archetypes are exceptions

flint hamlet
#

my main scene is almost empty and I prefer to create most of the entities by code
ghosts is only exception because they need some magic and I have to use them as prefabs

pearl socket
#

sounds awful

hexed nest
#

on this topic, not adding components during runtime sounds like too much restriction. are they worse than adding components to gameobjects? has there been benchmarks on this? I wanted to benchmark it yesterday but then work's been busy recently and havent found the time yet :(

#

I think it'd be good to know some comparison here

pearl socket
#

adding components in runtime is intended behaviour: ICleanup for example

hexed nest
#

seems if we use entity query, we'll way better performance

#

I don't think it can be used much except for tag components though

pearl socket
hexed nest