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)
-
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.
-
Create a Subscene that contains a single authoring object that registers all the prefabs.
namespace Jovian.Authoring
{
public class ShipRegistryAuthoring : MonoBehaviour
{
public GameObject[] ships;
}
public class ShipRegistryBaker : Baker<ShipRegistryAuthoring>
{
public override void Bake(ShipRegistryAuthoring authoring)
{
foreach (var ship in authoring.ships)
{
GetEntity(ship, TransformUsageFlags.Dynamic | TransformUsageFlags.WorldSpace);
RegisterPrefabForBaking(ship);
}
}
}
}
-
Add the Subscene in the game scene (or ensure it's otherwise loaded, but I suggest the former)
-
In a MonoBehaviour, you can now:
_manager = World.DefaultGameObjectInjectionWorld.EntityManager;
_prefabs = _manager.CreateEntityQuery(typeof(Prefab) , typeof(YourPrefabTag), typeof(SomeOtherComponentYouCareAbout));
- 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.