#Instantiating entities with a normal gameobject monobehaviour?

1 messages · Page 1 of 1 (latest)

wooden turret
#

Hello, I'm currently in the process of converting my project to ecs for the performance benefits. The only part of my project I do want to convert is the instantiate chunks of my procedural generation system which is instantiated through a game manager.

The only way I know of instantiating an entity is through a baker, and afaik to do that, my game manager would need to be an entity. I'm only recently been getting into ecs so I would like to know if there are any other options. Thanks!

misty sage
#

I have similar challenges. My advice might be sketchy, but here goes. (in the spirit of "teaching others to learn yourself")
(would greatly appreciate some of the pros to chime in, either way)

  1. Make Unity Prefab Assets out of your Baker GameObjects. Assume the baker adds something like YourPrefabTag and some other IComponentDatas.
    We will then turn them into Entity prefabs in the next step.

  2. Create a Subscene that contains a single authoring object that registers all the prefabs.

namespace Jovian.Authoring
{
    public class ShipRegistryAuthoring : MonoBehaviour
    {
        public GameObject[] ships; //put your baker gameobject prefabs here
    }

    public class ShipRegistryBaker : Baker<ShipRegistryAuthoring>
    {
        public override void Bake(ShipRegistryAuthoring authoring)
        {
            //not sure if 100% correct but this works for me
            foreach (var ship in authoring.ships)
            {           
                GetEntity(ship, TransformUsageFlags.Dynamic | TransformUsageFlags.WorldSpace);
                RegisterPrefabForBaking(ship); //I don't understand why this line isn't the only one I need
            }
        }
    }
}
  1. Add the Subscene in the game scene (or ensure it's otherwise loaded, but I suggest the former)

  2. In a MonoBehaviour, you can now:

_manager = World.DefaultGameObjectInjectionWorld.EntityManager;
_prefabs = _manager.CreateEntityQuery(typeof(Prefab) /*important, by default queries filter out prefabs*/, typeof(YourPrefabTag), typeof(SomeOtherComponentYouCareAbout));
  1. Then you can have the MonoBehaviour work with _prefabs.ToEntityArray, _prefabs.ToCompnentDataArray, etc. And use _manager.Instantiate(prefabEntity) to spawn these entities. You can also fill up some entity command buffers with the instantiation commands, bulk instantiate, etc. Explore the methods available on entitymanager and entitycommandbuffer.
misty sage
#

The biggest mystery is usually finding out whether the prefabs have actually baked.
You can search your entity hierarchy window with a query like this: any=YourPrefabTag +IncludePrefab (⚠️ the cursed any keyword is case sensitive, and changing your query often resets the +IncluePrefab attribute)
You may also be able to see your entities in the normal hierarchy under the subscene (just noticed they have a blue prefab style look now - thanks unity!)

#

(numbers different here because I did a few iterations - they're the same as the blue ones above)

crude granite
#

I'd highly recommend browsing through the tutorials and examples in the "DOTS samples" pinned in the general discussion thread.

misty sage
#

So when do you use RegisterPrefabForBaking? I literally can't find it anywhere in the samples.

#

There's also a grand total of 12 lines of code regarding prefab entities, it seems.

It's also not clear where EntityPrefabReference is of use, and there's no example how to use it across multiple bakers / multiple subscenes.

RequestEntityPrefabLoaded is practically undocumented, as well. Documentation seems to say something else than the Samples. https://docs.unity3d.com/Packages/com.unity.entities@1.0/api/Unity.Scenes.RequestEntityPrefabLoaded.html
(where is it relevant? is this during baking? is this during scene streaming?)

crude granite
#

I believe RegisterPrefabForBaking just adds a prefab tag component to the entity so you can query by that. But yeah, definitely not required, you can just put the baked entity on a Singleton component and reference it that way like in the examples

#

Not sure about the other things you mentioned

wooden turret
#

Wait a minute, can't I just instantiate a gameobject in a subscene where it is converted into an entity?