Introduction to Unity3D

Introduction to Unity3D

Tutorial Details
  • Technology: iOS SDK
  • Difficulty: Beginner
  • Completion Time: 30 Minutes

Unity3D is a powerful cross-platform 3D engine and a user-friendly development environment. Learn how Unity3D can help you create games in this article!


What is Unity3D?

Unity3D is a powerful cross-platform 3D engine and a user friendly development environment. Easy enough for the beginner and powerful enough for the expert; Unity should interest anybody who wants to easily create 3D games and applications for mobile, desktop, the web, and consoles.


Cost

Indie developers rejoice, Unity is free! Well, almost. There is a Pro edition that comes with more features and tools, but will set you back $1,500. Considering the feature set and how permissive the Unity publishing license is, this price is actually very reasonable. However, the free version will let you get your feet wet, build complete games, and even publish them to the desktop and the web without paying a cent! The only caveat is that games published under the free edition will have a small Unity watermark.

Anything built in Unity will work exactly the same way in Unity Pro. This means you can choose to upgrade at any point if you need the additional features, or want to publish to more platforms such as iOS and Android. There’s also a 30 day Pro trial you can sign up for in order to test drive all the extra features!

A complete feature comparison between Unity and Unity Pro can be found here:

http://unity3d.com/unity/licenses


Installation

Installation is a painless two step process. First, download and run the Unity installer.

Unity Installer

Second, when you start Unity for the first time, it will open a web browser and prompt you to register using your email address. It will let you choose which version you want to run. You can select either the free version or a Pro trial that will fallback to the free version after 30 days.


The Application

The Unity application is a complete 3D environment, suitable for laying out levels, creating menus, doing animation, writing scripts, and organizing projects. The user interface is well organized and the panels can be fully customized by dragging and dropping.

Unity Application

The Project panel is where all the assets within a project are stored. When assets are imported, they will first appear here.

The hierarchy panel is where assets are organized in a scene. Assets from the Project panel can be dragged into the Hierarchy panel to add them to the current scene.

The Inspector panel lets you inspect and adjust all the attributes of a selected asset. Everything from its position and rotation, to whether it’s affected by gravity or able to cast a shadow.

The Scene panel is a 3D viewport where you can physically arrange assets by moving them around in 3D space. You can navigate the viewport by panning, rotating, and zooming the view. If you’ve used Maya at all, you should find these hotkeys familiar:

Mouse Button Shortcuts

When it comes to running your game, it couldn’t be simpler. Just press the play button. To stop it, press the play button again. You can even pause your game during play to inspect your scene.

Play Button

Unity Projects

A Unity project is an ordinary folder containing every resource that belongs to your game. Creating a new project is a straightforward affair.

  1. Click File > New Project
  2. Click the Create New Project tab
  3. Browse to a suitable folder
  4. Click Create
Create New Project Window

The result is a project folder containing subfolders named Assets, Library, and ProjectSettings.

Project Folder

Assets

Assets are any resource your game uses. These include 3D models, materials, textures, audio, scripts, and fonts, to name a few. Other than a few simple objects such as cubes and spheres, Unity can’t actually create most of these assets. Instead, they must be created externally using 3D modeling applications and painting tools and then imported into Unity.

Thankfully, Unity’s asset importing is robust and intelligent. Traditionally, 3D game engines have usually been finicky things and are very particular about what files you give them, forcing developers to carefully convert all their files. Not Unity. It will accept all popular 3D file formats including Maya, 3D Studio Max, Blender and FilmBox with all the rigging, materials and textures intact. Unity also supports all common image file formats, including PNG, JPEG, TIFF and even layered PSD files directly from Photoshop. When it comes to audio, Unity supports WAV and AIF, ideal for sound effects, and MP3 and OGG for music.

A complete list of all the formats Unity can import can be found here:

http://unity3d.com/unity/editor/importing

Let’s import an asset so we have something to work with:

  1. Download boxboy.zip
  2. Unzip it to your desktop
  3. Drag the boxboy folder (containing boxboy.fbx and texture.png) from your desktop into the Project panel
  4. Drag the boxboy asset from the Project panel into the Hierarchy panel
  5. Select boxboy in the Hierarchy panel
  6. Press F to focus the Scene panel on the boxboy

Note: Unity has an Asset Store where you can purchase 3D models, characters, textures, sound effects, music, tools, and even scripts. The Unity Asset Store has quickly become an invaluable resource for game developers and a money making venture for artists and tool developers.


Scenes

Scenes are where you can drag in project assets and arrange them to make levels and game screens. The Hierarchy panel represents the contents of the current scene in a tree-like format. While the Scene panel is ideal for arranging your scene’s assets in 3D space, the Hierarchy is where you’ll spend most of your time actually organizing your scenes and keeping them tidy.

When you start a new project, Unity automatically creates a new scene for you. Scenes start out with nothing but a camera. If you were to run the game now, you won’t see anything but the background color. To give us something to look at:

  1. Drag the boxboy asset we imported from the Project panel into the Hierarchy panel

    Dragging Asset to Hierarchy
  2. Select the boxboy asset in the Hierarchy panel
  3. In the Inspector, find the Transform component and adjust the position so that X, Y, and Z are all set to 0. This will ensure your asset is at the exact center of the 3D world.

    Transform Properties
  4. The default camera position isn’t very good, so let’s give it a better angle. Select the camera, then reposition it using the move and rotate tools.

    Camera Being Moved

Scenes are assets and should be saved in your project just like other assets. To save your scene:

  1. Click File > Save Scene
  2. Navigate to your project’s Assets folder
  3. Name your scene Main
  4. Click Save
Project With Main Scene

Scripting

Scripts, known in Unity as behaviours, let you take assets in your scene and make them interactive. Multiple scripts can be attached to a single object, allowing for easy code reuse. Unity supports three different programming languages; UnityScript, C#, and Boo. UnityScript is similar to JavaScript and ActionScript, C# is similar to Java, and Boo is similar to Python. Depending on your background you may feel more comfortable with one or the other.

Let’s create a C# script:

  1. Click Assets > Create > New C# Script
  2. Rename the new script in the Project panel to PlayerScript
  3. Double click the script to open it in MonoDevelop

The script should look just like this:

using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
        // Use this for initialization
        void Start () {
        }
        // Update is called once per frame
        void Update () {
        }
}

Note: C# class names must be the same as their file name and are case sensitive. Make sure your class name matches the file name exactly, excluding the file extension.

All scripts have a start() method and an update() method. The start() method is run once when the object is first created, while the update() method run once per frame. Our script needs to be constantly checking for arrow keys being pressed, so we’ll add the following code to the update() method.

void Update () {
    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");
    transform.Translate(horizontal, vertical, 0);
}

Now that our script is done, we need to assign it to our asset. Naturally, Unity makes this a simple affair:

  1. Drag the script onto the boxboy asset in your scene

With the script assigned to our boxboy asset, we can run the game and move BoxBoy around by pressing the arrow keys.


Publishing

Unity is able to publish to Windows, OS X, and the web via the Unity Web Player. The Web Player is a browser plugin that works in all major browsers and offers the same performance available on the desktop.
You can download the Unity Web Player here:

http://unity3d.com/webplayer/

Not surprisingly, Unity Pro can publish to even more platforms, including iOS, Android, Wii, Xbox 360, Playstation 3 and even a Flash version of the Web Player.

To publish our game for the Web Player:

  1. Click File > Build & Run
  2. Select Web Player from the list
  3. Click Build And Run
Unity Web Player

Conclusion

Click here to download the complete Unity project.

This article barely scratches the surface of what is possible with Unity. If this introduction has whet your appetite for more 3D game development, be sure to check out the following resources:

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Serhio

    Finaly GREAT!!!!

  • http://thecodeblockinc.com Anthony

    Good start but you have to pay $400 more for iOS builds or $1500 more for Pro iOS builds and tools. A great tool to start learning to animate and build but will cost you $$$ if you want to publish.

    • NDogg

      That is only if you want to publish to mobile devices and if you need the pro tools. You can publish apps for pc/mac/web for free. However, generally, you have to spend money to make money.

    • Ryan

      Publishing to PC/Mac/Web with Unity is completely free. You are only required to go Pro if your company makes over $100,000/year with it. It’s also worth mentioning, is that Unity goes on sale fairly often. They recently offered iOS and Android licences for free over several weeks.

  • Ian
    Author

    It doesn’t cost anything to get started and you can build complete games without the pro version. It only costs if you want to publish to mobile devices; It’s free for desktop and web. You’re free to create an entire game, evaluate the framework, and only have to pay if you feel the result is worth of other platforms.

    Furthermore, the cost for publishing to mobile devices is minimal compared to other 3D platforms such as the Unreal Engine when you consider the licensing restrictions.

    • http://rcdmk.com RCDMK

      Envato should implement a voting system on the comments, so we can +1 this.

  • http://www.mobiadage.com/ Bill [Mobiadage]

    Awesome.. looking forward to more tutorials!

    Good thing I got Unity when the iOS and Android exporters were free for a while the past couple of months! :D

    Cheers!

  • me2o

    it just me or anyone feel that this kind of tutorial really useless? feel like briefing intro to unity3d rather than introduction, manly consist of information side than theory/technical.

  • http://www.zamtools.com Ian
    Author

    @me2o,

    I intended this as a primer; Something anybody could read and get a functional result out of by the end of it. It is meant to be a foundation for more advanced articles in the future.

    The purpose was to familiarize the reader with the core concepts in Unity such as projects, assets and scripts, so that they have enough of an understanding to continue experimenting on their own.

  • Cobus

    Please do more tutorials on Unity3d. I have written some very easy platformer games in Unity but would like to learn how to do some advanced stuff. Keep up the great work.

    • http://www.zamtools.com Ian
      Author

      What sorts of advanced things are you looking to do? Specific topics help me guide my future tutorials.

  • Tarifex

    Really Good, we hope more tutorials on Unity3d!

    • http://www.zamtools.com Ian
      Author

      I just submitted my next article about creating different types of 3rd person cameras in Unity3D. Hopefully, it will be be published this week.

  • Mark

    Can unity allows you to display a shape consisting of a polycurve defined by bezier curves. and if so can you then bend that polycurve plane? In other words can you define a 3d geometry with bezier curves or something similiar, and then parametrically change those curves?

    • http://www.zamtools.com Ian
      Author

      Unity doesn’t have the facilities for manipulating splines out of the box, but there is an excellent plugin you can purchase on the asset store called RageSpline.

      http://ragespline.com/

      RageSpline has excellent vector/spline editing tools, specifically designed for building 2D vector games.

  • http://www.cgvector.com cgvector

    great article.. looking more advance tutorial on unity 3d…

  • http://twitter.com/Scois0n Scois0n

    So excited! Got license keys for iOS and Android when they were on sale a while back! Please keep the tutorials coming!!!