Unity3D: For Beginners and Professionals
|Jeff Sasmor in Programming Friday, October 11, 2013|
There's always a build-versus buy decision for software tools like Unity. It's simple: do you want to make a game or an engine? Both are full-time jobs. If you want to| TOOLS create a game, do yourself a favor and check out Unity3D. You're getting a great tool, supported by a company with a modern business model: free to play, or "freemium."
It may be odd to think about it that way, but Unity is a freemium product: you get something useful for free, but past a certain point you'll need to buy in. Unity also makes money from their Asset Store, which operates with a revenue model similar to Apple's App Store. They're also in other markets beside just games — they have sales to architectural, medical, and training application developers as well.
A diverse revenue stream makes the company more financially stable. Let's face it, you don't want to invest your time learning the framework and building an app with it, only to see the company disappear.
The Unity Technologies company was founded about ten years ago by David Helgason (the current CEO), Nicholas Francis, and Joachim Ante. One thing that may surprise you is that it's only been available since about 2005, and really has taken off as a major platform around 2009 when the free version of Unity became available. In 2010 they had about 250,000 developers.
A good side effect of having so many developers is that there's a very active forum community. There's also a very useful site called "Unity Answers." As you can imagine, both newbie and seasoned Unity developers use these two resources when they need some help.
There's also plenty of documentation. Lots of it! A manual, a reference, and a scripting reference will keep you wellinformed. Absorbing it all may seem a little overwhelming at first - and there's a lot to learn. But you'll feel really smart as the little light bulbs light up over your head. There are plenty of examples and demos to get you going.
WHAT CAN YOU DO
What can't you do? You still need tools to create your models, animations, textures, sound effects, background music, and other types of assets. But Unity handles all of the other aspects of 2D or 3D development including Animation, Rendering, Lighting, Effects, Materials, Terrains, Physics, and so on. All done? Click the "Build" menu item and voila, an executable emerges.
I really like the cross-platform capability. With one development system you can create apps for Windows, Mac, iOS, Android, Xbox, Wii, Linux, and Windows 8 phone/tablet (currently beta). Only building apps for iOS/Android and don't care about the rest? Well if your app becomes a blockbuster you might start to care. We should all be so lucky, right?
The cross-platform nature is related to Unity's use of the Mono runtime, which is based on interpreted languages. Mono may be an issue if you need to program in a standard compiled language because you need the ultimate in speed. Although this is an advanced topic, you can create platform-specific C++ DLLs for additional speed.
To work faster, you can buy plug-ins from the Asset Store.For example, if you are working on a project to be deployed on touch screen devices, there are plug-ins to handle all the details of gesture recognition. There are several sprite animation plug-ins that would be handy if you're making a 2D game. There are also plenty of actual Assets, too including 3D models, shaders, textures, animations, sound effects, particle systems, and even complete projects that you can use or learn from.
The Asset Store sells all sorts of scripts, including many very useful plug-in libraries including AI, camera control, GUI, I/O, animation, modeling, networking, effects, physics, and system-specific plug-ins, such as in-app purchases, social networking, iCloud, iOS' Game Center, and more. There are literally thousands of items.
For some interesting technology demonstrations, go to http://unity3d .com/gallery/demos/live-demos. Most are game levels or mini games of some kind. Some are even available on mobile App Stores, for play right in your web browser. Some are downloadable in source form so that you can dissect them for learning purposes. There's also a remarkable video showing real-time rendering power, see it at http://video.unity3d.com/video/6912 619/the-butterfly-effect-realtime-rendering
HOWTO GET STARTED
Just go to the Unity3D website and click the Download button to download Unity Free. Games have been released using Unity Free, it's pretty full-featured. Be sure to read the license before actually doing any development work. To stay within the license terms, any sort of incorporated business entity (even a one-person entity), really needs to buy Unity Pro prior to doing active development. There are Pro-version trials available for evaluation if that works better for your organization.
After installing Unity for the first time, play with the "Angry Bots" demo (normally installed with Unity). It's a full-featured game level that demonstrates many Unity features. It's fun too.
Screenshot1 below shows the Unity Editor view of Angry Bots for Unity Version 4. Any panel can be repositioned or even undocked. When satisfied with a layout, it can be saved as a template for use in other projects.
The Unity3D editor is an Integrated Development Environment for Game Development.With the exception of editing scripts and creating assets, you're working within the system all the time.
When you are ready to test a scene, click on the "Play" button at top-center. The view will switch to the Game window, which in Screenshot 1 is obscured by the Scene window. When I'm deeply into the edit-test cycle I undock the Game window and place it on another monitor. That way I can watch the Scene and Game windows without toggling back and forth between them.
Each scene in an app is a separate entity. At runtime, the assets for each scene are loaded into memory. Unity has a setup panel that's used to specify which scenes to include in the app. The first scene in the list is the first one loaded when the app starts. Other scenes are loaded via scripting.
Assets (models, textures, audio files, etc) are drag-and dropped into the IDE, and they appear in the Project panel. Information about how the asset was imported is viewed in an Inspector window. As an example, see screenshot 2, showing the Inspector window for a Texture that we'll see again farther along in this article.
The Project panel displays the contents of a real folder on your storage device. When an asset is imported it's copied to that folder along with some Unity-generated metadata. Why's that important? Unity is cross-platform; it keeps track of the import parameters per-asset for each platform as well as various dependencies. This also means that you never move files around in the Assets folder by using Finder or Windows Explorer because the metadata doesn't get updated (even if you move the metadata too). To build a scene, drag imported 3D models into the Scene window. You can also add built-in primitives like cubes, cylinders, and planes. The Hierarchy window shows the organization of a scene's content. It's a folderish view that a developer uses to keep track of the scene. Everything in this window is a GameObject, which is a container that has no real attributes of its own aside from which layer it's on and a tag.
GameObjects are the heart of the system, and Components (essentially, scripts) are added to these GameObjects to implement functionality. Components use references to GameObjects and their attached Components to implement the functionality of the application. Screenshot 3 shows the Inspector for a simple GameObject that displays a sphere.
The Transform Component is at the top of the Inspector. This Component controls the location, rotation, and scale of the sphere. The Mesh Renderer Component displays the Sphere by using the Mesh Filter Component's information about the 3D model.
You can affect the displayed value of any of these Components at edit time by clicking into the field within the Inspector and then changing one or more values. You could also manipulate the sphere directly within the Scene window. At run time you'd use a script to access and change, say, the Position's X coordinate or the Scale's Z value, or the Main Color of the Shader's material. There are many other Components that are part of the standard Unity installation. Colliders can be used to detect when one object touches another; these are also useful for implementing a user interface. There are Components for Physics, Audio, Cameras, Visual Effects, Navigation, and many more. Other Components can be added via scripting custom-created by a developer. Many are available on the Asset Store.
If you need to replicate the same GameObject you can configure one as you like in the Editor, using whatever components you want and then create what's called a Prefab. At run time you create instances of these via scripting, and they appear in the Hierarchy. It's easy to add a Component: let's add a Collider. I clicked "Add Component" at the bottom of the Inspector, then typed "Collider" and clicked on "Sphere Collider." Screenshot 4 shows the result.
For custom functionality you generally need to create your own custom-scripted Components, although there are also visual programming environments like Playmaker. Your scripts can access public variables, properties, and functions of Script Components on the same or any other GameObject in the Scene. A simple, although less accurate, way to understand the overall system is that at runtime, your scripts (or script plug-ins) link into and become extensions of the OO framework. More specifically, they become Components attached to the GameObjects; the GameObjects in the Hierarchy constitute the scene.
When you create your own script, any variable that has the access modifier "public," as in public GameObject sphere, will appear in the Inspector. Unity understands how to display most types of variables, such as integers, floats, onedimensional arrays, enumerations, etc., and it is possible to create custom inspectors if needed.
There are two main ways that you'll use Inspectors. One is tweaking values without editing code or secondly dragging Scene Hierarchy GameObjects and script references into Inspector fields to link code references to specific GameObjects or scripts in the scene. When you're testing your app in the editor and you want to tweak the value of something, just stop the app in the editor, click into the field and edit the value, then click Play to restart the app.
In screenshot 5 a GameObject called Controller has been added to the scene. That GameObject has an attached script component "Rotate." That script needs a reference to the Sphere in the scene so that it can make it rotate. In the editor, I dragged the Sphere item in the hierarchy to the Sphere variable in the script Inspector and the link is made.
The scene can have GameObjects without renderers,meshes, or materials attached to them. One or more could be used to host control scripts of an app, if that makes sense for that project. Sometimes empty GameObjects are useful as a sort of parent folder to one or more GameObjects if you'd like to organize a complex scene via the folderish hierarchy, or as a parent for prefab GameObjects created at runtime.
Underneath what you see in the Unity Editor resides a rather large Object-Oriented framework. Generally, scripts you write will implement classes that inherit from the MonoBehaviour class. MonoBehaviour provides a rich variety of universal functionality that you'll use over and over again. There's excellent documentation on the entire class library on the Unity3D.com website.
Since .Net programs are based on interpreted languages, it's easy to run apps within the editor without actually building a "real" executable. If you have a fast enough PC or Mac, you just click the Play button and your app is running in a second or two. This is a wonderful type of environment for development, especially for people like me who love to tweak game play until it feels right.
When you need to edit scripts, you use MonoDevelop. Unity includes a modified version of this open-source editor from the Mono project. It's cross-platform, so it's the same editor for Windows and Mac. You can set breakpoints and attach to a running app. It has decent refactoring, search, and cross-reference capabilities. Codecompletion works as expected: type the name of an object and then '.' and a drop-down list of the possible methods and variables appears. If you type a function name, then at the opening parenthesis, a pop-up appears with the name and type of the parameter you need to add. If the source-code author added commentary XML tags, additional information appears in the pop-up.
Don't forget to back up! From my first backup experience using a cassette tape to today's cloud storage: you don't want to lose your work. You can use Unity's Asset Server or other version control systems like Subversion. It's important to use some sort of version control. On the downside, it's a bit clunky that it's necessary to open a Command or Terminal window and navigate to the Asset Server's program directory so that you can access some scripts in order to back up the database. It's a one-line command but this could be made a little easier.
Try some tutorials: click on the Unity3D.com website's "Learn" tab and do some of the tutorials. This is a fairly new feature of the website, I wish it were there three years ago. There's a "Beginner Scripting" set of 24 tutorials that will be a big help even if you're already an experienced coder. It's a great way to get up to speed.
Unity3D is a development environment wrapped around a game engine. If you've used an IDE for development, you should have few problems learning to use Unity. There's always a learning curve for any system, and Unity is no different. There are plenty of resources to get you started: Demo projects, Documentation, Forum postings, and the Asset store will help you in your journey from newbie to pro.
This content is made possible by a guest author, or sponsor; it is not written by and does not necessarily reflect the views of App Developer Magazine's editorial staff.