Any good ways to use IDeclareReferencePrefabs similar to GhostPrefabCollectionComponent?

Read through the conversion workflow documentation but can’t figure out the proper use of IDeclareReferencedPrefabs

I would like to have a list of prefabs I can access from any system to spawn those prefabs.

In NetCode I use the GhostPrefabCollectionComponent to access prefabs and instantiate them in systems.

I created a “PrefabCollection” GameObject in my SubScene and added the following script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;

public class PrefabReference : MonoBehaviour, IDeclareReferencedPrefabs
{
    public GameObject Prefab1;
    public GameObject Prefab2;

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

I cannot for the life of me figure out how to reference this “List” of GameObjects in other systems to create prefabs.

I want to build something like:

public class AsteroidSpawnSystem : SystemBase
{
    private Entity m_Prefab;

    protected override void OnCreate()
    {
        m_Prefab =  DeclareReferencedPrefabs.Prefab1
    }

    protected override void OnUpdate()
    {
        EntityManager.Instantiate(m_Prefab);
    }
}

How do I access the prefabs I have added to the list?

You can use an Entity field in a IComponentData with GenerateAuthoringComponent attribute instead, and add ConvertToEntity script to your object.

If you want to use IDeclareReferencedPrefabs, you need to implement IConvertGameObjectToEntity and use GameObjectConversionSystem.GetPrimaryEntity to get converted entity passing your prefab as parameter.

velenrendlich, I assume that is IF the prefab “starts” in the subscene? Or does “ConvertToEntity” work on that are just in your Assets folder?

I can’t see the point in ever using IDeclareReferencedPrefabs when you want to spawn prefabs programatically.

You can create an Authoring component with a reference to a prefab and call that Singleton component instead to instantiate entities.

Not sure if there is any value in IDeclareReferencedPrefabs…

I don’t know about use case in subscenes