Search

Prefab Variants

Updated: Aug 22, 2019

The first thing I did when starting my next game project was update to Unity 2019. It was due, but the biggest factor in that decision was all the trouble I had with nested prefabs in Alien Cow Farm. After struggling for so long, the new prefab workflows introduced by this video seemed too good to be true. But so far, I'm very happy. This post will show you how I've integrated a few of those features so far.


I think I was the most excited about prefab variants, gameObjects that stem from a common prefab ancestor, override a few specific properties, but are still prefabs themselves (meaning that you can instantiate new instances of them). This would have been phenomenal when dealing with my barns, for instance, if I were able to efficiently update a single common base and have it propagate to all the different player and map variations. This new game will also have many similar variations of objects, often differentiated only by the attached sprite or color scheme for the different-themed levels.


Let me start with the simplest tangible example I have currently: jellyfish appear in two of my levels, but they need to be colored differently in each. I could do some tricky things with Unity's sprite color property (basically starting from an all-white image and tinting it until it's the color I want), but I think in this case I can afford to just change the entire sprite. (Note that this means I could also go from a jellyfish to an octopus or something else completely different, but I'm still sticking with the simplest case for now.)


So what doesn't change? Everything else. That means I can create my first prefab, the "base", with my behavior scripts, rigidbody, collider, and whatever else, including a placeholder image sprite. When I've finished, I drag it down into my file list to create my first prefab, just like in previous versions of Unity. Then comes the new bit: right-click on it and select Create -> Prefab Variant. I'll rename the new one Jellyfish_Arctic to go with my normal Jellyfish. At this point, if you're like me, you'll want to test it out. If you make a change to Jellyfish, that change is automatically reflected in Jellyfish_Arctic. If you make a change to Jellyfish_Arctic, it gets saved as an "override" specific to it, and will not appear in Jellyfish. Of course, this is only the default behavior, and with a few more button clicks, you can "apply the overrides" and manually send the changes to Jellyfish_Arctic back up the chain to Jellyfish. Similarly, a change to Jellyfish can be ignored in Jellyfish_Arctic by setting that specific property as an override in the latter...and that's exactly what we're going to do. By setting the sprite component of Jellyfish_Arctic to be an override, I can change the sprite on Jellyfish as often as I want and it won't affect Jellyfish_Arctic. But remember, everything else will, so I've accomplished my goal: two jellyfish that are exactly the same (and importantly, can be updated in the future the same, by making the change in exactly one place), except for having different sprites.


So let's jump over to Photoshop and create those sprites.


Wow, that was easy, wasn't it? At this point, I want to mention that my art workflow is designed to be as modular and reusable as possible, as well. All the black outlines you see are "shapes", so they can be modified as "objects" rather than flat "images"... in plain English, this just means that you can resize, recolor, and more at any point in the future without worrying about pixelation or distortion. For a cel-shaded style like this, being forced to use the paint can tool can really mess things up around the edges and destroy the look. Since fill color is something you can set for your shapes, doing so would solve that problem and be absolutely fine for my purposes...but I thought of an even better way. The pink you see above is not a shape; it started out as one, but has been rasterized into a normal image. That let me take the eraser tool to it and contour it to the black outlines. Not the inner ones (like where the head meets the ruffly part), mind, just the absolute edges of the jellyfish. If I hid the black and purple layers, you'd see one large pink area. What does this accomplish? I can go ahead and use the paint can on it to change all the pink to something else at once, as opposed to having to do so for every sub-shape individually. And the problem with the paint can I mentioned before doesn't exist here, because I'm working with one large field of flat color, where I don't have to consider grayscale transitions to edge colors. I hope all of that made sense...if not, the takeaway is that I've got both an outline I can change at any time without worrying about distortion and a way to change every piece that's the same color with a single click and still no problems. So let's do so.



Okay, now I just need to import both images into Unity, and apply them to Jellyfish and Jellyfish_Arctic. And there you have it; changing either prefab's sprite leaves the other alone, but changing anything else (that isn't an override) changes both. Pretty cool!



Have you figured out what my game is yet? Stay tuned for the grand reveal next time, just as soon as I figure out what I'm going to call it...