This is part 2 of a series. If you’re entirely new to making games with Unity, then I recommend you read the first article first. I don’t plan on writing many of these, but I did want to cover reusing game assets through prefabs and 3D asset workflow. The descriptions of those things I found when learning Unity myself left me confused and misinformed; hopefully I’ll do a bit better here.

Prefabs

Reusing assets is an integral part of game development. Be it projectiles, creatures, terrain, or equipment, there are lots of things you won’t want to create repeatedly. Unity’s prefabs let you save off any GameObject into your ProjectFolder/Assets directory, and you can then drag them into your scene, or into another GameObject in your scene, at any time. Given that an object in Unity generally has multiple scripts and other GameObjects attached to it, this also helps things stay relatively organized and object oriented.

Let’s revisit my last article’s BoxShooter5000 example:

See how the “Enemy" GameObjects are colored blue, and how there’s also an "Enemy” object in the right Project pane?

That blue box means it’s a prefab, and the blue text on the Enemy GameObjects in the scene means that they’re linked to that prefab. If you change the prefab, it’ll also change every object linked to it in the scene. It’s very handy. Nested prefabs only work in Unity 4.x and above.



Adding a new component to a linked GameObject will warn you that you’re about to “lose the prefab parent”. You can always drag the GameObject onto the prefab to both update the prefab and re-link it, though, so this is a fine way to add things to prefabs while still getting to preview things in the editor.

You can also change the properties of a linked GameObject without breaking the prefab connection. Honestly, it’s kind of confusing and can lead to things getting overwritten by prefab changes in the future; I try to avoid editing an object without updating the prefab to match it immediately afterward.

While you have an object that’s linked to a prefab selected, you’ll be able to select the prefab the object is based on, revert to the prefab’s settings, or apply the selected object’s changes to the prefab (and, in turn, to any other objects linked to that prefab!) via some handy buttons in the object inspector.

To create a new prefab go to Assets > Create > Prefab or just right click on the exact spot in the hierarchy you want to create the prefab and choose Create > Prefab. Your new prefab’s icon will be gray to indicate its utter sadness at being empty and useless until you drag something onto it.

3D Models/Assets

Importing 3D models into Unity is generally very easy. For Blender, you can simply drag a model’s .blend file and any necessary textures into your assets folder (caveat). They’ll be added as prefabs and you can drag the model into your scene from there. Animations, materials, UV mapping, etc. will all come with it, and you can actually update the materials inside Unity.

Unity has some nice examples for how to use their character controllers. They cover moving a model around with a script and playing the appropriate animation at the right time while blending appropriately with much aplomb, so I won’t be covering the specifics of handling animations in Unity here. Their examples do have one glaring fault, though.

Unity’s examples make their 3D models the root GameObject. If you do this, you’ll break the prefab connection to that original imported model. This means that whenever you update your model file you’ll need to make a new GameObject using that model and re-add your scripts, components, etc. That’s a pretty miserable workflow. Things are much more cleanly updated if you simply make the model a member of the main GameObject instead:

Don’t drag things into your models. Just drag the transforms you’d like to attach your sword/helmet/particle effects to out and attach said items via a script (see my script’s mixing transform variables in the above screenshot, for example). That way your model’s prefab remains untouched in the bowels of your GameObject and will update by itself whenever that model gets reimported.

This does mean that your base object will no longer be a model. Unity’s examples take advantage of that a lot, both by calling “animation” to get at the model’s animations from within a script and by just dragging animations from the model into a script’s member data.

You can get at that same animation component by adding your model as a member variable to your script of choice and getting the Animation component out of it like this:

creatureAnim = creatureModel.GetComponent( Animation );

From there, you can easily reference animations by name rather than by drag and dropped reference, i.e.:

creatureAnim[“legs-run”].speed = runSpeed;

So now you can update your model and that prefab portion of your GameObject will just update itself the next time the model’s imported into Unity! Much better.