How to convert bunch of prefabs without instantiating as gameobjects

Well, as thread title says: i have a bunch of prefabs which are not in scene on start but loading them before world creation, then i want to convert them as entities prefabs, but see no way except of spawning them as gameobject with ConvertToEntity component or create special gameobject with authoring component, give it all prefabs and let it be converted with all this prefabs at once (my current solution)

public class AddressablesToEntitiesAuthoring : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
    {
        private List<GameObject> _prefabs;
        private string[] _prefabsGUIDs;

        public void Initialize(List<GameObject> prefabs, string[] prefabsGUIDs)
        {
            _prefabs = prefabs;
            _prefabsGUIDs = prefabsGUIDs;
        }
        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
#if UNITY_EDITOR
            dstManager.SetName(entity, "AddressablesAssetMap");
#endif
            var assetMap = dstManager.AddBuffer<AssetMapElement>(entity);
            for(int i = 0; i < _prefabs.Count; i++)
            {
                var guid = new Unity.Collections.FixedString64(_prefabsGUIDs[i]);
                var hash = guid.GetHashCode();
                assetMap.Add(new AssetMapElement { entity = conversionSystem.GetPrimaryEntity(_prefabs[i]), assetGUID = guid, assetGUIDHash = hash });
            }
        }
        public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
        {
            referencedPrefabs.AddRange(_prefabs);
        }
    }
public class PrefabReference : MonoBehaviour, IDeclareReferencedPrefabs
{
    public GameObject prefab;

    public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
    {
        referencedPrefabs.Add(prefab);
    }
}

This still requires a subscene:
This works for single or can be extended to an array for multiple ones. Put it in a subscene, reference your prefabs from resource folder or anywhere and they get automatically converted to entity prefabs because they are a dependency. If the behaviour is correct, can be seen in the DOTS hierarchy. I changed this quite recently to get rid of the ConvertToEntity and ConvertGameObjectHierarchy nonsense.

1 Like

Your current approach seems reasonable. I’m assuming you are letting your special Addressables GO be converted via ConvertToEntity and not GameObjectConversionUtility? If so, you are doing exactly what I would do.

However, keep in mind that Entities now has entity prefab streaming, which might be what you really want. I haven’t played with it, but there is an example for is in the ECS samples repo.

2 Likes

I’m not aware of that sample and can’t find it. Do you have a link?

https://github.com/Unity-Technologies/EntityComponentSystemSamples/tree/master/ECSSamples/Assets/Advanced/EntityPrefab

4 Likes