BACK

Blender to Unity, Part Three: We Emerge From The Winter

Alex Lowe - 02/08/2018 9:19 PM

Blender to Unity, Part Three: We Emerge From The Winter

Alex Lowe - 02/08/2018 9:19 PM
FILED TO:
http://glowingbluecore.com/wordpress/wp-content/uploads/Blender-to-Unity-post-3-excerpt-large.png

This is the second tutorial in a four-part series of getting started in Blender and Unity. You’ll need Blender (free), Unity (free) and Photoshop (not free) as you machete through these steps with me. Having some basic coding knowledge is helpful too, but not necessary.

 

In this series:

Blender to Unity, Part One: Woe and Wisdom

Blender to Unity, Part Two: Unwrapping Seams and Your Sanity

Blender to Unity, Part Three: We Emerge From The Winter

Blender to Unity, Part Four: A Rain of Barrels

 

The tutorial that we’re rebuilding can be found here:

 

https://www.youtube.com/watch?v=71rRJBqu2KE&t=300s

 

In the last two posts we’ve gone from vague aspirations with Blender to actually modeling a simple barrel, giving it a texture and calculating lighting angles. Now with our FBX file, we’re going to head on over to Unity and put that sucker in there. This tutorial will be kind of a victory-lap after all the travails of the previous two.

 

So download Unity. You might need to sign up for an account. Not a day goes by that I don’t have to sign up some damn account somewhere, so I understand your frustration. Punch a pillow, and sign up for one. When you’ve downloaded Unity, open it up and create a new project.

 

Now, you’ll see a window that says “Untitled”. This window is your scene hierarchy. It contains every object in your current scene. Right now your scene is rather spare, and it contains only a camera and a light source. We’re going to change that soon. Right click in that window and select “Create Empty”.

 

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-create-empty-game-object-768×415.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-create-empty-game-object-1024×553.png

 

We’re going to import three resources to add the barrel in here. Your .fbx file that we exported last time, as well as the ambient-occlusion texture that we also exported last time, and finally our nice colored texture.

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-import-assets-300×289.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-import-assets.png

 

Right click on that little “Assets” folder icon and select “Import New Asset”. Select each of those resources. You’ll have to do this for each one. After you’ve imported them, your assets folder window should look something like this:

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-assets-window-768×416.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-assets-window-1024×554.png

 

All right, now you’re over a this other inspector window for this game object that you created. Change the name to Barrel:

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-change-the-name-to-barrel-300×214.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-change-the-name-to-barrel.png

 

Now drag the barrel mesh in the Assets window to the stage.

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-drag-mesh-to-the-game-object-stage-768×831.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-drag-mesh-to-the-game-object-stage-947×1024.png

 

Now the stage of your game object should look like this:

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-barrel-game-object-768×581.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-28-barrel-game-object.png

 

Now we’re going to use our texture files. In the Assets window, right click and create a new material.

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-29-create-a-new-material-768×500.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-29-create-a-new-material-1024×667.png

 

A nice little sphere icon should appear in your Assets window. Name it something like “Barrel Mat”.

 

Now in the inspector panel for the new material, drag your texture files to the correct spots.

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-29-assign-textures-to-the-material-768×540.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-29-assign-textures-to-the-material-1024×720.png

 

Note that we don’t have a normal map. I didn’t make one remember? Because of my old Photoshop? So we just won’t have one. Anyway, that sphere in your Assets window: when you assign your textures, it’s going to get all funky looking. Drag that sphere to the stage after you’ve assigned your textures. Now your barrel should be textured and interacting with the light field!

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-29-our-barrel-300×276.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-29-our-barrel.png

 

And that, friends, is the end of the video! Now we could call it a day, but we’re feeling pretty good about this whole enterprise, so we’re going to move past the video. What we’re going to do is add some behaviors to this barrel in Unity. First of all, what we want is to be able to instantiate this object dynamically. Then we’re going to program in some behaviors and watch that thing go!

 

An aside: Reading through Unity forums there seems to be a lot of “Flash refugees” who came to Unity after seeing the writing on the wall about Flash Player’s eventual demise. Flash was a universe unto its own, containing all kinds of strange things like MovieClips and Timelines. Unity is kind of the same way. Dynamic objects that you can call up in your code are called “Prefabs”. I guess for “Prefabrication” presumably? Well, we want to be able to instantiate our barrel with some code, so we have to make one of these “Prefab” things.

 

In your Assets window, right click, and select “Create” -> “Prefab”. Now a cube icon should appear in your Assets window, and you can name it “Barrel”. The imported mesh object from Blender that we made last tutorial should be in your hierarchy window. We’re going to drag that from our Hierarchy window to our prefab icon in the Assets window.

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-move-textured-mesh-to-prefab-768×960.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-move-textured-mesh-to-prefab-820×1024.png

 

And your Barrel prefab icon should become a little image of that texture barrel mesh.

 

Now you should see two barrels on the screen:

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-prefab-has-the-mesh-768×651.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-prefab-has-the-mesh.png

 

The first is the mesh from the last tutorial, and the second is the Barrel prefab which contains a copy of that mesh.

 

Now here’s what we’re going to do. We only want one of these barrels on the screen. We just want our prefab for the time being. But we can’t just delete our textured mesh from the Hierarchy. We have to first drag it from our Hierarchy window to our Assets window. The Hierarchy window contains every object in the scene, and the Assets window contains everything that Unity knows about, whether it’s on the scene or not. We created our textured barrel mesh in the last tutorial, and now we want to save it, but we don’t want that original barrel on the scene any more, so if we move it to the Assets window, we can safely delete it from the Hierarchy. Did you just delete the mesh from your hierarchy and now your prefab went blank? You have to drag the mesh to the Assets window first or Unity will delete it from everywhere in the whole project! I don’t say these things for fun! They’re actually required!

 

All right. Now we’re going to add some code. I’m going to create an empty game object and use that as the main hook for all the code modules that I come up with. I’m sure all of you Flash refugees out there have done something similar in the past, so hopefully I’ve made you nostalgic for those early 2000s. Anyway, over in your Hierarchy window, right click and in the menu that pops up click “Create Empty”. I’m going to name mine “Bootstrap” because it’s going to be the main hook that starts up all of my code.

 

In your Assets window, right-click, and select “Create” -> “C# Script”. I called mine “Main”. Note: Don’t include “.cs” at the end.

 

Now I’m going to do something extra, which is that I really love Sublime Text 2 for my text editor. So I’m going to tell Unity to use that editor instead of its own internal one. That’s entirely optional. If you’re a connoisseur of finer text-editing, you might want to do this too. Go to “Unity” -> “Preferences” and in “External Tools” you can browse for the text editor that you want to use.

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-change-the-text-editor-to-sublime-768×637.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-change-the-text-editor-to-sublime.png

 

Now, back in your Assets window, double-click the icon of your Main script. Paste this in:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Main : MonoBehaviour {

	// Use this for initialization
	void Start () {
	Debug.Log("It's right up your alley.");
	}

	// Update is called once per frame
	void Update () {

	}
}

 

MonoBehavior? What’s that? I told you Unity is a universe unto its own. I don’t really know why the typical base-class is called MonoBehavior. Don’t worry about it. Anyway, this code won’t do much, but it will display a time-honored Jurassic Park quote to the debug window. Now we have to do one more thing before this script will run. Right now our script is just an asset that Unity knows about. But it doesn’t DO anything on its own. You have a potato masher somewhere in your kitchen drawer, but it’s not going to use itself. You have to actually take it out and put it on the cutting board first. So here, the script has to be attached to something sitting in the scene for it to actually come to life. We’re going to add this script to our Bootstrap empty object that we created earlier. So click “Bootstrap” in the Hierarchy window. In the inspector, you should see a button that says “Add Component”. Click it.

 

In the options that appear, click “Scripts” and then select “Main”.

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-add-in-the-main-script-655×1024.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-add-in-the-main-script.png

 

Now, up at the top, click ‘Window” -> “Console”. That’s your debugging console where your Debug.Log messages will be shown. Now- see that little “Play” button up at the top? (Ah, Flash!) Click that bastard!

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-first-unity-run-768×572.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-first-unity-run.png

 

Yee haw! And you console window should say “It’s right up your alley”. Ah, John Hammond. I gotta watch that movie again soon. Anyway click “Play” again to stop this thing.

 

All right so now back to our code. We’re going to add a public variable to our Main class

 

public GameObject Barrel;

 

So now it should look like this:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Main : MonoBehaviour {

public GameObject Barrel;

	// Use this for initialization
	void Start () {
	Debug.Log("It's right up your alley.");
	}

	// Update is called once per frame
	void Update () {

	}
}

 

Now if you go back to Unity, you’ll notice that the inspector has that same variable present.

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-public-variable-should-be-visible-300×274.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-public-variable-should-be-visible.png

 

Now what are we going to do? We’re going to drag our Barrel prefab from the Assets window directly over to that little “Barrel” variable that just opened up on the inspector. Now it should say “Barrel” in there instead of “None (Game Object)”

 

Now go back to your code and alter the Start function to look like this:


	// Use this for initialization
	void Start () {

	Debug.Log("Hold on to your butts.");

	Instantiate(Barrel);

	}

Note the new command “Instantiate”, which is going to create an instance of our Barrel prefab. Now, I can hear the code-purists starting to groan and howl. Dragging and dropping? Why oh why do we have to interact with the damn Unity Editor to assign a variable like this? I know, it’s a pain, but unfortunately it’s necessary. The alternative is way worse, which is to load the object in from the resources folder.

 

(See, code-purists are kind of like magicians. They just want the magic to be entirely self-contained in their code, like a forbidden book. The more they’re told that they have to rig their code to a specific development environment like Unity, the more irritable they get.)

 

Any any rate, go to the scene window in Unity. You still have your Barrel prefab in your scene. Make sure that it’s in your Assets window, and then delete it from the Hierarchy. Trust me. Now your Hierarchy should be very minimal and it should contain only the camera, the light-source, and your empty Bootstrap object. Press “Play”.

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-barrel-created-dynamically-768×603.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-30-barrel-created-dynamically.png

 

 

Whoa! The barrel is back! What’s happening is that we’re creating a barrel on the fly from our Main script. And the Main script is attached to our Bootstrap object, and the Bootstrap object is the only thing sitting on the scene. So the deal is that we’re going to be concentrating as much of the logic and intelligence as possible inside our code. It’s better that way, and I’m saying this as someone who did lots and lots of wrangling with Flash years ago.

 

All right we’re not done yet, though! We’re going to add just a little bit of animation to this barrel! Copy over your Main script with this:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Main : MonoBehaviour {

public GameObject Barrel;

private GameObject _barrel;

	// Use this for initialization
	void Start () {
	Debug.Log("Raptors- they should all be destroyed.");
	_barrel = Instantiate(Barrel);
	}

	// Update is called once per frame
	void Update () {
	_barrel.transform.Rotate(1.0f, 1.0f, 1.0f);
	}

}

 

What’s going on here? We’re storing our barrel instance as a private variable “_barrel” and then in the Update function we’re using that variable so that we can rotate the instance just a little bit with every frame! So now click “play” and you’ll see that your script not only creates a barrel on the fly, it also sets it up to rotate!

 

There’s just one last little point of housekeeping here. We have to save our scene. Click that little drop-down icon where it says “Untitled”

http://glowingbluecore.com/wordpress/wp-content/uploads/Step-31-save-your-scene-300×259.png http://glowingbluecore.com/wordpress/wp-content/uploads/Step-31-save-your-scene.png

 

And click “Save Scene”. Name it something nice like Barrel Scene. It should appear in your Assets window. That’s the end of this tutorial. We’ve done a lot of work in these so far. We’ve crashed through Blender to model and texture a barrel in 3D, we’ve imported it into Unity, and we’ve also made it available as a dynamic instance that our code can summon like a Patronus charm whenever we want and make it dance around. Just crazy. And there’s more to come!