When do Prefabs go through conversion?

If a GameObject in a Prefab has a ConvertToEntity Component, will it go through the conversion process when you save changes to it (on a background editor thread, like a Subscene)?

Or does it need to be referenced by something in a Subscene to be converted at editor time?

Thanks for any insight. Having trouble finding documentation on this one.

ConvertToEntity just converts in Awake so it’s all runtime conversion (therefore slow and should be avoided)

2 Likes

Then what’s the correct way to preconvert Prefabs which aren’t used in any subscenes?

EX: bullet Prefabs which are intended to be spawned at runtime only.

I convert all prefabs in a subscene and give them the Prefab component (it’s built into entities)

Or just reference them from the subscene using IDeclareReferencedPrefabs which seems to make more sense for bullets.

So you just create a Subscene specifically to provide Subscene references to all your Prefabs? Clever solution.

But this is a weak point in Entities that I did not expect: Having a library of Prefabs that might only be used at runtime is a very common game dev case.

I actually assumed Unity must be creating Subscene-like structures behind the scenes for any PrefBs with ConvertToEntity components. Because I can’t think of a common case would you would want them to be converted only at runtime.

Maybe i misunderstood you but can this be helpful : Conversion Workflow | Entities | 0.17.0-preview.41 (unity3d.com) ?

go to prefabs subsection

“By default, the conversion workflow only processes the actual contents of an authoring scene, so a specific mechanism is required to also include prefabs from the asset folder. This is the purpose of the system group GameObjectDeclareReferencedObjectsGroup, it runs before the primary entities are created in the destination world, and provides a way of registering prefabs for conversion.”

You have also code example there - hope this helps

Yes if I’ve understood you’ll want them in subscenes with the Prefab component, which by default excludes them from any entity queries, i.e. they are not rendered etc. Then in your bullet spawning system you define a query that has the entity query option set to include prefabs. Then when you use ECB to instantiate, the Prefab component is automatically removed from that instance.

Anyone else leave these forum threads with 10x more questions than they had when they opened it?

1 Like

Thank you all for your advice, truly.

I don’t mean to criticize the design here too much, but needing to make a dedicated Subscene just to house references to Prefabs you only spawn at runtime seems like an unnecessary and clunky thing to have to do.

Since Prefabs and Scenes are (if I understand correctly) the exact same data just with different file extensions, it seems like Unity could take care of this common case automatically: create a Subscene for every Prefab that has a specific component in its hierarchy, such as ConvertToEntity.

If there’s a good reason not to this, please respond with it. It would be great to learn more.

2 Likes

I think I was I wrong -this may not be the problem is was thinking.

If all content in your game lives in Subscenes, and if your game uses Prefabs, then all Prefabs in your game will be referenced by a Subscene, somewhere.

It might make Prefab organization require a bit more thought, however. Prefabs that you intend to instantiate at runtime (like bullets) might need an ‘authoritative’ Subscene that represents ‘the’ Prefab entity you plan on instantiating.

I bet all that’s pretty obvious to some of you. Thanks for walking me through it.