Get Entity from Previously converted GO within a MonoBehaviour

Hello Eveyone

im looking for a way to store some converted GameObjects as entities and use them later for instantiation, i know i can convert them directly by using “GameObjectConversionUtility.ConvertGameObjectHierarchy” api but it’s not working for GO with Physics stuff in Android devices.

Results in different devices but same apk.
SamsungS8 : physics entity goes under the ground
SamsungA2 : game Crashes
OnePlus2: works Correclty

so im looking for a way to store the entity in Edit time for a later use, or maybe a way to Convert the Entity using ConverToEntity instead of the “GameObjectConversionUtility.ConvertGameObjectHierarchy” and instantiate it from a MonoBehaviour.

Thanks!

In this talk (sample 2) they show how to convert a prefab into an entity and instantiate it later at runtime.

The conversion is executed in 3 steps:

  • Declare Prefabs: All GameObjects which are converted are gathered in IDeclareReferencedPrefabs (but not converted yet)
  • Create Entities: For each delcared Prefab an entity is created (but the entity is still Entity.Null) but can be referenced
  • Conversion: Each GameObject is converted and all IConvertToGameObject methods are called

Some facts which helped me ALOT:

  • Entities created from Prefabs have a Prefab Component attached and are NOT queried in Systems.
    So if you Instantiate a Prefab there are usually 2 Entities around which only differ by one having the Prefab Component. (would be REALLY helpful if the Prefab Component would be displayed in Entity Debugger Overview with an icon before the name)
  • GetPrimaryEntity returns an Entity for a GameObject even if the conversion did not happen. Therefore the conversion order does not matter if entities are referenced by other entities.
  • GetPrimaryEntity only converts the GameObject ones → all other calls copy the already created entity with the prefab tag

There is also this article by 5argon. Really hard to understand at the beginning but turns out to mention everything you need (Read it about 5 times)
https://gametorrahod.com/gameobject-conversion-ecosystem-code-tour/

2 Likes

hi @Flipps ,
Thanks for the answer :slight_smile: , but i steal looking for a way to access to the GetPrimaryEntity api from a regular MonoBehaviour like a GameManager Class,
im already converting my GO this way but cant access them outside classes not using the IConvertGameObjectToEntity. and using the GameObjectConversionUtility.ConvertGameObjectHierarchy crashes the game.

Ah ok. But cant you just create an Entity Spawner in a Monobehaviour script with IConvertGameObjectToEntity (reference SpawnedEntity with PrimaryEntity)

    public class SpawnerAuthoring : MonoBehaviour, IConvertGameObjectToEntity
    {
        public GameObject SpawnedGameObject;

        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            dstManager.AddComponentData(entity, new SpawnerData()
            {
                Entity = conversionSystem.GetPrimaryEntity(SpawnedGameObject)
            });
        }
    }

    public struct SpawnerData : IComponentData
    {
        public Entity Entity;
    }

and then in your GameManager spawn it by quering the SpawnerData

    public class GameManager : MonoBehaviour
    {
        public void Spawn()
        {
            var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
            var spawner = entityManager.CreateEntityQuery(typeof(SpawnerData)).ToComponentDataArray<SpawnerData>(Unity.Collections.Allocator.Temp);
            entityManager.Instantiate(spawner.First().Entity);
        }
    }

Code not tested :slight_smile:

1 Like

that’s awesome!
Thank you!