I have a prefab which represents a plant - it has a sprite, possibly a 2d collider, and some custom components. This plant can be grown and will have several variations - primarily a different sprite, but also possibly a different collider.
I’m looking for advice on how best to structure/handle this. I don’t want to create a prefab for every variant. Not only is that excessive but it means I’d have to replace the prefab which will be trouble.
I thought about assigning different sprites through the editor and then switching the sprite when needed, but I don’t see how I could do that with colliders and other components. I can’t predefine them and store this way and I’d rather not construct the programmatically.
I have actually attempted to solve a similar issue by developing a solution called “Presets”. I did not fully implement this, but this could give you a rough idea for a solution or something you can base off your own solution.
In our game, we used a single prefab, but we would “configure” it at runtime, from code. These configurations would mean we would change sprites, positions, scales, and other properties.
Doing this from code was cumbersome and not ideal.
The solution i had in mind was to define presets - a preset is a single configuration of the game object and its properties. You would get a nice dropdown with named presets (in your lingo - variants). you could then select the different presets from the dropdown to change the configuration of your game object.
This is better than doing all of this from code, and is more clear to the user (a preset has a name so it’s clear what it does).
As for the implementation - this is the tricky part… my current WIP solution would save a preset as a sub-game object under the root game object, so effectively you would get a duplicate object in the hierarchy… this is not ideal, and i have yet to continue my work on this (as we’ve already moved on to a different project).
There are probably other, more elegant solutions, i may revisit this one day when i get some time.
Hopefully this could give you a few ideas to get started
We’re doing pretty much what @liortal 's suggesting for NPCs. Each NPC is the same prefab, but there’s a bunch of different game objects under it that’s a variant. A custom editor script on the root of the NPC object lets you select which one you want.
We use the PostProcessScene attribute to find and destroy all of the variants that are not used.
@Baste the only problem with that is that it’s not really efficient… if you want a single prefab to be able to “switch” between all available variants at runtime, you’re essentially storing all possible combinations which is a waste.
I tried coming up with more elegant designs but they are a lot more tricky to implement…
Oh, if the switching is happening at runtime, then you can’t use my solution. Then you’ll want pretty much exactly what you’re talking about.
I think the way to do it would be to store the different possible settings in a ScriptableObject asset? That would allow you to not copy the same data around.