I’m about to decide how to manage my prefabs in my game consistently and (hopefully) “future proof”. Thus I would like to discuss some “best practices” and advantages / disadvantages for some workflows regarding prefabs. As preparation I watched Code Monkey’s introduction to prefabs. And I must say that I’m an ECS noob and thus can’t really see advantages and disadvantages of different methods. And since this video is 9 months old maybe in the meantime better options are available (Subscenes)?
So I hope this discussion will help some ECS “noobs” to find a proper workflow for them in the many different possibilities.
So the aim of this thread is to discuss how to get prefabs properly (the intended way) into ECS worlds and be able to spawn them from there. When working with MonoBehaviors I had a custom poolmanager which knew every prefab in the game and I could spawn stuff from there (I work mostly with procedural generation and distribution). So I would like to have some kind of “manager” which knows every prefab and which the systems could order to spawn stuff. For example when a projectile is fired or a planet shall be placed.
When I say “prefab” I mean mostly the art side of things (models, textures, particle systems). I like to place them “under” an entity which has the game logic components.
Methods to convert prefabs:
A) Store it in an IComponentData with a [GenerateAuthoringComponent] on a GameObject with a ConvertToEntity script.
Pros:
easy and least verbose, “direct” way
Cons:
Runtime conversion, probably slow
B) Store it on a MonoBehavior which implements IConvertGameObjectToEntity.
Pros:
more controll with manual conversion, but usefull for what?
Cons:
much boilerplate code.
Runtime conversion, probably slow
C) On a MonoBehavior which implements IDeclareReferencedPrefabs and IConvertGameObjectToEntity.
Pros:
more controll with manual conversion, but usefull for what?
Cons:
much boilerplate code.
Runtime conversion, probably slow
D) Implement a GameObjectConversionSystem to “hook” into the conversion.
only mentioned in the video but not properly explained.
E) Subscenes (are not mentioned in the video).
Pro:
Editor Conversion, fast (playmode), recommended way AFAIK
Cons:
Harder to work with in Editor? Access via subscenes. Must be present in scene hierarchy instead of the project view.
Methods to “access” those prefabs:
S) A field in an IComponentData with [GenerateAuthoringComponent].
Cons:
requires a foreach over a single entity.
T) A static field in a (converted) component.
Pros:
No Entities.Foreach to access it.
Cons:
static messing up Chunk utilization?
U) Singleton
Cons:
Chunk utilization? Is better support for them to be expected?
Only one prefab component allowed?
V) Blob asset
Only mentioned in the video. Not explained properly.
For my special usecase, a “global” spawn manager which knows all possible prefabs, I tend towards conversion method E) (subscenes, not sure how to organize that) and I would mix access types T) and U) (a singleton with a static field for each prefab type). Is this viable?
questions:
So which of those methods do you use and why?
Do you see more advantages or disadvantages for specific ones?
Are there other possibilities not mentioned here?
Which method do you think will “survive” DOTS evolution? Or are they all equally valid and here to stay? (I know this is speculative).
What about extensibility through mods. So when modders wan’t to bring additional content in the game how can it be referenced?
How do these methods play together with the archetype system?
How does this allow “setting” values during spawn (positions for projectiles, color for laser, scale for planets)? I would like to have a method to spawn each type with proper (default) parameters. Can these be static methods in a system which are called from the generation system? Or is this not “data driven” enough?
How does this play together with texture atlases? When some of the models share parts of the same texture. Is this handled gracefully by Unity under the hood?
Which method is preferable for “hybrid” entities which also have a parallel GameObject and are synched?
How could an “automatic” way be achieved? I mean simply dropping a prefab into an array and it gets available in ECS without changing code.
Does someone create prefabs directly in ECS (as Archetype for example)? How can assets like models and textures be handled this way?
Is this way desirable at all or is this a dead end? How do you handle prefab workflow?
I know this is a large post and it’s asking many questions. But as a “noob” in ECS I fell pretty overwhelmed by those different methods to do the same thing. And since documentation still su…, eh lacks, I thought I ask here. And if there are good suggestions this could be useful for many people trying their hands on ECS. And maybe some of the “authorities” are willing to give some insight into future plans for prefab workflow.
So I hope some of you are willing to share your experiences. Thanks in advance.