Megabite #8 - Importing Your Wares Into Unity3d

I have yet to have any real problems with the Unity asset importing features, but the reason I wanted to write this article was to explain some of the quirks that might make things easier for new Unity developers.  In this article, the two major things I want to cover are:

  • Importing textured 3d models into your game
  • Importing sound files and using them correctly
To begin, we're going to start with 3d models.  Personally, I've been using Cheetah3d which is a very inexpensive and basic modeling program.  You can also use Blender as a no-cost alternative, and you really shouldn't have major issues regardless of the file type you prefer.  While Cheetah3d defaults to a .jas type of file, I have now taken to saving as .fbx because it seems to be more common and universal for other modeling programs.  However, it's important to note that Unity itself has no issue reading or importing any of the file types I've come across.
Obviously, the first thing we want to do here is design and texture a 3d model.  For this article, I chose to use a terra-cotta pouring vase, seen below within Cheetah3d.
 
When rendered within the 3d modeling program, we get something like this:
 
To understand the path ahead, what we want to end up with here are basically 2 files - the object itself which is a series of polygons, and the texture that wraps around those polygons.  If we were just to save it and import it now, we would end up with just a shape without any texture.  To get the textures into a usable file, we simply bake them into a .png picture file.
We don't need to know how everything works exactly except that our program has a UV editor and an option to bake the texture for us.  I select the object, select "bake texture" from the menu, and open the UV editor to see this:
 
 
On the left, you can see the flat mapping of every polygon that can be affected by applied textures.  You'll notice that on the right, I've chosen UV1 and set a decent texture resolution of 1024x1024.  When this picture is rendered, we get a result that looks like this:
 
Save your main file wherever you like, and be sure to save this image file along with it.  Within Unity, we can right-click on our project window to get the option to "Import new Asset" or "Open in Explorer (or Finder)".  Whichever you choose, add your files into your game assets and you'll see Unity work for a moment to calculate everything it needs.
 
 
 
Once it's within your project window, you can manipulate settings on the prefab that Unity creates.  Before anything else, 
be sure to drop your texture image file onto the little window within the material object.  With this file added, you can see in the preview window of the 3d model prefab exactly how everything is rendered.
In some cases, the object may look a bit funky at this point.  This is something that was slightly frustrating to me early on, but I found that Unity compensates through the inspector window and can correct the display.  Take a look at the pictures below and you'll see the rendering difference when "Normals" are imported versus calculated for this model:
 

 

 

Once you have your object set up correctly, drag it from your project window into the scene and take a look - hopefully everything looks as you expect.  There are many more advanced ways to work with materials and texturizing, so if things are not exact, you can change your resolution or settings within your modeling program to your liking.

Another important thing to note is that if you work with the saved model file directly from your assets folder, you can change your model on the fly.  Simply edit and save to instantly update everything within unity.  (keep in mind you may need to bake your texture again if you make big changes).

While I've yet to work with the major professional modeling applications such as Maya or 3d Studio Max, I can't imagine they would be much tougher than what we've done here with Cheetah.  I look forward to working with those programs when I have the chance and perhaps writing a more specific tutorial if the process is different.

On the next page, we'll work with importing and utilizing sound files within our project.


Like models, Unity is open to just about any kind of sound file.  The import procedure is very much the same, but understanding how to attach the file and work with it might take some basic tips.

In the image on the left, you can see how I have set up a "game music" type of sound attachment.  In this instance, I have dragged the sound file directly onto the camera (where the audio listener is attached).  Your audio listener component is where the sounds will be "centered".  By default, you should have one attached to your main camera, and you'll need to remove that if you want to place it somewhere else in the game.  You can only have one audio listener in-game at a time.

Working down from the top, we have a few fairly self-explanatory options such as "Mute" or "Loop".  If we're doing music or something of the like, we would want to loop it, but obviously for a sound effect we wouldn't really prefer to hear it more than just once.  "Play on awake" will cause your sound to start as the level begins - this setting is again something you should select depending on your own situation.

The "Priority" of the sound is the virtual layer that determines which sounds are the most important.  For example, your game music should likely be less of a priority than your sound effects, and the sound of glass smashing should play "on top" of your music.  Once you have a mass of sounds imported, tweaking this slider on each file will further customize your game and possibly make it more immersive.

The Volume, pitch, and first few 3d sound settings are methods you can use to further customize your player experience.  All of these settings can be changed here or via script to your liking.

Distance is perhaps one of the coolest features, as changing these settings is what affects the volume of your sound in-game as it relates to the listener.  By changing these settings, you can change how far away you can begin to hear something, and how loud the sound is as you get closer.  Attached to a projectile, for example, you could hear the sound of a bullet fade away as it disappears into the horizon.  Attached to a static object, you could hear the crackling sound of fire as you approach a burning building.

It's difficult to write a step-by-step for something like this because each situation is so very different.  The best advice that can be given is to keep tabs on your audio listener, especially if you change it.  At one point, I made the mistake of attaching it to my player, who was destroyed via script in-game.  Once that happens, there won't be any sound unless you establish a new listener immediately.

Learning by experimentation will be key in getting your game exactly how you like it.  If you have any questions about anything within this article, please let me know in the comments below.