Posted 05/12/2014 09:38:29 in Blendering for Unity
Updated 08/20/2017 19:12:52
Before taking our model into Unity, there are a couple of quick things that aren't totally necessary, but will help keep your models a bit more organized. In the Materials tab of the Properties frame on the right, rename your model's material to "characterMaterial". If you get into the habit of doing this, it'll save you from having to create a new material in Unity every time you add a model (if you don't do this, every model you import will use the same default "Material.mat" file in Unity).
Flip back to the Object tab in the Properties frame, and rename the "Cube" object to "characterMesh". This will also help keep things more organized in Unity.
As Deozaan pointed out to me in the comments, you have a choice of either flat-shading or smooth-shading your model (see the screenshot for the difference). By default, your model is flat shaded. If you want to smooth-shade your model, select all your faces (A, and just like when you were UV mapping, it doesn't actually matter whether you're in vertex, edge, or face selection mode). Either open up the Tools panel with T and click the Smooth button under the Shading/UVs tab, or hit Ctrl-F to bring up the Faces context menu and select Shade Smooth.
If you don't do this in Blender, you can still do it in Unity by setting Normals to Calculate in the model's import settings, but I noticed that the result isn't quite as good and results in more jagged shading. If you set the shading to Smooth in Blender, make sure you leave the Normals field on Import.
All right! Let's set up a quick project in Unity to give your character a home. Open up Unity, create a new directory for your project, check "Toon Shading" and "Skyboxes" in the Import Assets box, and create your project! Now hop out of Unity for a moment and open up two file browsers in your OS. Navigate one to your character model directory, and the other to your Unity project's Assets directory. Copy both "character.blend" and "characterColor.png" to your Assets directory (or, create a Models/Character subdirectory in Assets and put your character there). In my projects, I'll usually have a Models subdirectory, and another subdirectory for each particular model. Flip back to Unity, and it'll automatically recognize your model and import it.
Since we already optimized our .blend file, there isn't a whole lot of cleanup to do! However, there are two things regarding the animation clips that you'll want to consider doing. The first is to delete the "Default Take" clip. There is a way to avoid having Unity import it in the first place (exporting as .fbx from Blender and unchecking "Include Default Take"), but that's a topic that I won't be covering here (IMHO, the convenience of having Unity import the .blend instead of .fbx outweigh the negatives). Select your character in the Project browser, then flip to the Animations tab. Select "Default Take" and click the "-" button to remove it (this will stop your project from being polluted by Default Takes from every model you import).
The second thing you'll want to do is to enable "Loop Time" for the clips that you want to loop. Select the Idle clip, scroll down, and check Loop Time. This is more for demonstration in this project, since we'll just have the animations play into each other, but it can be frustrating if you're building a game and the animations play once and stop so I figured it was worth mentioning. Also, make sure you click Apply at the bottom (or confirm in the dialog that pops up if you don't click the button).
Start creating your scene by adding a new Terrain (GameObject->Create Other->Terrain). Relocate it to (-500, 0, -500) to center it on the scene's origin. Give it a texture by selecting it in the Heirarchy, switching to the Paint Texture tab, clicking Edit Textures->Add Texture, and selecting a base texture (I used the rock texture I made for Sail). Here's a .zip file with a few of the textures I made for Sail, along with the .blend I made for this series, to make things a little more convenient for you!
Let's add a simple water plane. Add a new plane (GameObject->Create Other->Plane), relocate it to (0, 2, 0) and scale it to (500, 1, 500) to match the terrain. Create a new material for the water (Assets->Create->Material). Select your plane in the Heirarchy, expand the Materials field in the Mesh Renderer component, and set the material (Element 0) to your water material. Click the Select button to choose your water texture, and set both the x and y tiling to 500 (since the plane is scaled up to 500 x 500).
Next, we'll take care of they lighting and camera. Create a Directional Light (GameObject->Create Other->Directional Light) to light your scene. Select your "Main Camera" in the Heirarchy, raise it up a bit (I set the y position to 5), and add a Skybox component (Add Component Button->Rendering->Skybox). Use the nub next to the "Custom Skybox" field to select one of your imported skyboxes (I chose Sunny2).
Now we'll add the character! Find your camera in the Scene View (double-clicking on "Main Camera" in the heirarchy can help), and make sure your Main Camera GameObject is selected to activate the Camera Preview window. Click and drag your character model into the scene view from the Project browser, and position it where you want on the x-z plane. Then, raise it up to leave room for the island, and rotate it to face the camera (it can help to use the Game view for this).
Let's create an island for the little guy to stand on! Select the Terrain in your Heirarchy, switch to the Raise Terrain tab in the Inspector, adjust the brush size and opacity, and click a few times around your character. Switch back to the Paint Texture tab, add a grass texture, and make the Opacity 100%. Click a few more times around your character to give it something soft and swirly to stand on! Odds are your character is either floating above or sinking into the terrain, so zoom in with the Scene view and position your character more accurately.
Time to add some animation! Create a new Animator Controller (Assets->Create->Animator Controller). Double click on it in the Project browser to open it up in the Animator window. Now you can drag your animation clips directly from your model in the Project view into the Animator! We'll keep it simple for this tutorial and just have the two clips transition back and forth into each other. Right click on each clip, hit "Make Transition", and click on the other. By default, this will transition into the second clip when the first is done. Select your character in the Hierarchy and click the nub by the Controller field in the Animator component to select your controller (make sure you switch to the Assets tab)!
The last thing to do is to add some cel shading to your character. Select your character's material in the project browser (It'll be in the Materials folder in the same directory that you put your model). Change the shader to Toon->Basic Outline. Set the cubemap to "toony lighting", and darken the main color a bit to stop it from blowing out.
You're done! Hit play, and watch the product of all the work you've put into these tutorials happily jump around :D
I hope these tutorials have been helpful! This is the last post in this series, but I think I'll add one or two more posts later on with a list of all the keyboard shortcuts that we used and a list of all the Unity-specific considerations to keep in mind when using Blender.