Unity Basics: An Introduction

Unity 2.5’s release is finally on the horizon, which means Windows editor!  In addition to some great new workflow and editor features, 2.5 will also usher in a wave of Windows users.  There is a lot to learn about Unity, when you first encounter it, so we’re going to do a series of Unity Basics posts, with an introduction to some of the core concepts.  This first post answer the question:

What is Unity?

Unity’s scope makes concise definition difficult.  Unity is a lot of things, and it’s used differently by different disciplines, but here’s one breakdown. Unity is:

An Integrated Editor

Unity provides an editing environment where you organize your project assets, create game objects, add scripts to these objects, and organize objects and assets into levels.  Most importantly, Unity provides a “game” view for your content.  You can hit play and interact with your content while you watch values, change settings, and even recompile scripts.

The IDE is largely stateless, in that there is little distinction between creating your levels and playing them.  For example, the editor remains functionality identical whether your content is stopped or currently playing.  This is hugely useful, because while your content is playing you can hit pause and then move things, create new objects, add scripts, and whatever else you need to do to test gameplay or chase down bugs.

Different team members and disciplines use the editor differently.  Here at Flashbang artists use the editor to smoke test new asset imports, arrange assets into levels, and tweak textures and other visuals.  A programmer may focus more on watching values and tweaking numbers.  A unified interface helps us tremendously; we don’t have people using different tools with different interfaces and workflow conventions.

A Component Architecture Paradigm

Unity utilizes a component-based architecture.  You could ignore this in creating your game logic, but you will suffer without a clear understanding of Unity’s design.  In Unity, every object in your scene is a GameObject.  An arbitrary number of Components are attached to GameObjects to define their behavior.

For example, a physical crate might be:

  • GameObject
    (name, layer, tags)

    • Transform
      (position, rotation, scale, parent)
    • Mesh Renderer
      (actually draws the object)
    • Box Collider
      (define collision volume)
    • Rigidbody
      (movable physics object)

Here’s the key:  When you create a script, you create a component. For example, you could create a Jump.js script to make your cube jump when you press a key, like:

var strength:float = 30.0;

function Update()
      rigidbody.AddForce(Vector3.up * strength)

JavaScript hides some of the details, but what’s happening here is you’ve created a new Jump Component.  Your script implicitly inherits from MonoBehaviour, which inherits from Behaviour, which inherits from Component.  You now have a new component, which you can easily add to your crate!

Note:  We use JavaScript at Flashbang, for a variety of reasons, but the steps are quite similar in C#.  The only real different, aside from syntax, is that you need to explicitly define the inheritance:

using UnityEngine;
using System.Collections;

public class Jump : MonoBehaviour
   public float strength = 30.0f;

   void Update ()
         rigidbody.AddForce(Vector3.up * strength);

A Game Engine

Unity is a fully-featured game engine.  It includes and exposes many systems needed for game creation, such as:

  • Graphics Engine
    Unity’s graphic engine includes a shader language, ShaderLab, which wraps Cg and GLSL shaders with additional engine semantics.
  • Physics Engine
    Unity uses NVIDIA PhysX for their physics engine, with editor and API integration (you set up collision volumes, joints, and things by adding components to your GameObjectsin the editor, and script physics with things like Rigidbody.AddForce() and MonoBehaviour.OnCollisionEnter() callbacks).
  • Audio Engine
    Unity has a positional audio system.  You can play sounds in 3D space, or “2D” stereo sounds.
  • Animation System
    Unity includes an animation system, including support for animation layers, blending, additive animations and mixing, and real-time vertex/bone reassignment.

There are quite a few other out-of-the-box systems there to help you, too, like particle systems, networking, UnityGUI, and so on.

A Scripting Platform

Unity embeds Mono to power its scripting environment.  You can script in C#, JavaScript, or Boo (a Python variant).  Mono itself is an open-source version of the .NET development environment.  Note that this doesn’t mean that Unity requires .NET.  Mono is totally distinct from Microsoft’s .NET, and Unity totally embeds Mono.

It’s also worth noting that Unity’s use of Mono goes above and beyond the compiler and common language runtime.  You also get the full .NET namespace, which means a huge amount of classes are available to you out of the box:  XML parsing, cryptography, sockets, and more.

As I mentioned earlier, Flashbang uses JavaScript.  Unity’s JavaScript isn’t very similar to the JavaScript language found in web browers.  It’s about as similar to web JavaScript as Flash’s ActionScript, which is to say not very similar at all.  There are a number of advantages to using UnityScript, although there are far fewer tools available (we use a custom-created hack of FlashDevelop).  With the advant of the Windows release, I imagine many newcomers will choose C# and Visual Studio.  Lucas Meijer makes a very compelling case for C# scripting over at his Unity blog.

A Scripting API

Your Mono-powered scripts have full access to Unity’s engine through Unity’s scripting API.  They’ve exposed the entirety of the engine, which means that you can do pretty much anything.  High level stuff is quite easy and simple, but you can dig all the way down to doing mesh generation or direct OpenGL calls if you’d like (which work on DirectX, thanks to Aras’ genius).

MonoBehaviour, the parent class for your scripts, provides a number of convenience members.  Things are usually quite straightfoward.  For example:

  • transform.position = Vector3.zero;
  • rigidbody.AddForce(Vector3.up * 10);
  • renderer.enabled = false;
  • particleEmitter.Emit(10);

There are a number of callbacks provided for game logic purposes:

  • Start()
  • Update()
  • OnCollisionEnter()
  • OnMouseDown()

The scripting environment supports coroutines, which are magically useful for all kinds of things.  Want to destroy an object 3 seconds after it was hit with something?

function OnCollisionEnter()
   // do something
   yield WaitForSeconds(3.0);

In addition to scripting your game at runtime, Unity provides a powerful editor API to create custom tools, windows, and shortcuts to expedite your workflow in the editor itself.  With Unity 2.5 the entire editor itself has been rewritten with the Unity API (which means you should be able to do practically anything they have).

More to Learn!

As you can see, Unity is quite broad.  This article should provide a good starting point for its top-level features, but even this overview hasn’t covered the whole of the software.

We’ll dive into more Unity features in depth in future Unity Basics articles.  We didn’t talk much about the Inspector (and how variables are serialized in Unity), or asset importing, or any number of other features.  What would you guys like to focus on?  Use the comments below to let us know!

This entry was posted in Tutorials and tagged . Bookmark the permalink.

7 Responses to Unity Basics: An Introduction

  1. Great initiative! I was starting to think that you had given up on this blog. Looking forward to the follow-ups. With the expansion of the user base, the community really needs to have more beginners guides ready – if nothing else then to relieve some pressure off the forums.

    One thing I was missing from this intro was talk of the editors expandability via editor scripts (do you even mention it at all?). As seen at unite, 2.5 also brings a fleet of new editor classes to the developers, so I think its an important part (and now that you mention him, I’d bet that Lucas agrees).

  2. Matthew Wegner says:

    I briefly mention it in the Scripting API section–but, yeah, it definitely deserves its own article…

  3. Sam Horton says:

    Fantastic article!

    This really sheds a lot of light on many of the questions I’ve had about Unity development. It’s hard to find these things out until you take the plunge and get your hands dirty, but what with buying a mac and all, I’ve been a bit hesitant. It really does seem worth it though.

  4. karel says:

    thank you for article. I be keeping my eye on this blog.

  5. Andrej Vojtas says:

    Thank you for your time, this is an excellent article.

    I would like to learn more about developing effectively: e.g. how to debug, how to organize data: in-game text, game balance constants, but also Unity specific areas (portability to iPhone, what kind of custom editor upgrades are handy). A sort of “best practices” info.

  6. Harley Witt says:

    I would like to hear about things that caused you the most pain and time to learn so we can avoid those things. This is a great forum to provide insight to the rest of us following your blog. As the windows version of Unity comes out, there will be an explosion of users and your company is at the forefront of making very great games using the power of Unity. Keep up the great work. I would be interested in hearing how you integrated things like the achievement on the blurst website similar to the Kongregate system since I will eventually getting around to doing this providing free educational type games using Unity with educational achievements. While we are on that topic, I would love to see if any of your team would be interested in either creating an educational type version of any of your games that I could then promote your games on my website. For example, maybe jetpack brontosaurus has to pick flying through rings in alphabetical order and if A-Z is achieved then they win. Also, it would be cool if the letters as flown through moved and minimized to the order at top or bottom of the screen. You guys could whip that out in a day or two but it will take me a while outside of my normal work hours to get to that point. Best of luck to you and your team. Let me know what you think of my idea. See my blog at http://worldlearningtree.com/blog

  7. Pradeep says:

    I would like to know a little more about the technologies that were used for developing Unity. Does anyone know what the UI was written in? While mono scripting is used the UI seems very nice for any of the Mono GUI kits.

Comments are closed.