<<

Unity Introduction Kingsley Stephens, Monash University

Introduction 2 What is ? 2 Downloading and Installing Unity 3

Getting Familiar With the Editor 4 About the Editor 4 The Editor Interface 4 Controlling the Scene View Camera 5

Hello Unity! A First Program 6 Create a New Project 6 Add a Cube 7 Make the Cube Interesting 8 Some Further Things to Try 10

Unity Concepts 11 Game Objects and Components 11 Scene Hierarchy 11 Assets 11 Manipulating Game Objects 11 Prefabs 12 Scripting in C# 12 Building an Executable 12

Learning Resources 13

Introduction

What is Unity?

Unity1 is a that has a generous free pricing tier. A game engine is a software library that provides the lower layers for games to be built on top of. A game engine typically provides components for rendering 3D graphics, playing audio, access to the file system, handling user input, physics simulation and many more services that go into making the complex software that is a game. Another popular game engine is the Unreal2 engine, while an up and coming one is the Amazon Lumberyard3 engine.

Unity (as do the other engines) also provides an editor. The editor allows games creators to import art assets, build scenes, write scripts, coordinate game play, and deploy to the platform of their choice; be it console, PC/Mac, web, mobile, or VR.

Figure 1. The Unity editor with a scene being edited

The first version of Unity was released in 2005 by Unity Technologies. It is now on its sixth major version iteration. It has grown in popularity due to it targeting ‘indie’ developers - single developers or small teams with low budgets. It’s proponents claim that Unity “democratises game development”.

1 https://unity3d.com/ 2 https://www.unrealengine.com/en-US/what-is-unreal-engine-4 3 https://aws.amazon.com/lumberyard/

Unity can be used to make 2D or 3D games. It is supported by the Asset Store which allows creators to download free or paid for art assets, scripts, sounds and other assets used to speed up the creation of games. It can be deployed to the latest VR headsets such as HTC Vive and Oculus Rift, as well as the latest AR/MR equipment such as HoloLens. Unity not only offers the game engine but a range of services to support the game making effort such as source control, cloud building, and analytics.

Unity is suitable for data visualisation as it is a general purpose game engine. This means that it will efficiently display as many points, lines, and polygons as your target platform can handle. It has a flexible scripting system built on .NET using the C# programming language that allows the creator to add new functionality. It can deploy to many different platforms including VR. This flexibility and the high performance 3D graphics allows for many different types of data visualisations to be made possible.

Downloading and Installing Unity

To download a version of Unity, go to the following website: https://unity3d.com/get-unity/download/archive. ​

Choose your version (current is Unity 2017.3.1), select the dropdown for either Windows or Mac based on your type of computer and choose the “Unity Installer”. A download will commence.

Once the installer is downloaded, run it and when at the “Unity component selection” screen choose the following basic options: ● Unity (e.g. Unity 2017.3.1f1) - The Unity Editor. ● MonoDevelop - C# IDE. ● Standard Assets - A package of useful assets.

Everything else is optional and are mainly components to allow you to deploy to different platforms.

Once you have installed Unity and you run it for the first time, you will be presented with a login screen. Sign up for the free Unity ID as this allows you to access the Asset Store and other Unity services.

When asked for a serial key, select the Free Personal tier.

That’s it. You are ready to go.

Getting Familiar With the Editor

About the Editor

The Unity Editor and a C# IDE is where you will be doing all your work. Unity versions up to Unity 2017 shipped with MonoDevelop as the C# IDE of choice, with the option to use Microsoft Visual Studio Community Edition (or other selected third party IDEs). From Unity 2018 onwards MonoDevelop is being deprecated and Visual Studio will be the free editor of choice.

The Editor is used to import art assets, create 3D scenes full of game objects, assign scripts written in the C# IDE to objects, and to build executables among other things.

The Editor Interface

The following figure shows the Editor interface.

Figure 2. The parts of the Unity Editor interface

In Figure 2 there are five main areas to the interface highlighted: 1. The Hierarchy window shows all the game objects that have been added to your scene. The game objects form a parent-child hierarchy (explained in the next section)

2. The Scene/Game views. The Scene view is where you place your 3D objects and manipulate them to create your scene. The Game view is where you preview your scene running. 3. The Project view shows all of the assets in your project. Assets are made up of 3D models, textures, materials, shaders, C# scripts, audio files, text files etc. Unity automatically imports and processes your assets into scene ready formats for you. 4. The Inspector view shows you the properties for the currently selected game object in the Hierarchy view. You can see in the Inspector all the components that have been added to a game object and the public properties that can be modified on them. 5. Play/pause/step buttons. Hit the play button to launch your scene in the game view. The pause button will freeze the time of the game view, while the step button will advance a single frame after pausing. It should be noted that while in game view your are able to switch to scene view at any time and configure/view the game from the editor’s perspective.

There are many other views that can be added into the Editor UI based on your needs. E.g. you can display a console view to see the output of your logging, an animator view to configure animation state machines, an Asset Store view to browse the Asset Store for assets to import into you project etc.

Controlling the Scene View Camera

Read this help page to understand how to control the camera in the Scene view: https://docs.unity3d.com/Manual/SceneViewNavigation.html

In particular you will want to be able to move, orbit, and zoom the camera. Also pay attention to the “Frame Selected” keyboard shortcut “F”. This will zoom your camera onto the currently selected game object.

Hello Unity! A First Program

Create a New Project

The first thing you see when you launch Unity is the Project chooser. This shows you all of the Unity projects you have previously opened. Handily it will tell you which version of Unity is required to open the project. You can always upgrade a project to a newer version of Unity just by opening it, but you can’t downgrade a project to an older version.

Figure 3. The Unity project chooser

To create a new project, click on the “New” button in to the top right. This will present you with the following screen.

Figure 4. The new project screen

For this example you will need to: 1. Turn off analytics by pressing the toggle to “off” - we don’t need this feature on. 2. Leave the 3D radio button as it is - we want a 3D scene as opposed to a 2D scene. 3. Choose a location for the project - this can be a root folder for all your projects as Unity will create a new folder in it with the name of your project. 4. Provide a name for your project. 5. Click the “Create project” button.

Add a Cube

Once Unity has loaded the new project you will be presented with a blank scene that just contains a camera and a directional light. Do these steps to add a cube: 1. Go to the GameObject menu and select 3D Object and then Cube (menu GameObject > 3D Object > Cube) - this will create a unit white cube in your scene. 2. Select the game object called Main Camera in the Hierarchy view. This will select the camera and provide you with a little preview window in the bottom right of the screen that shows you what the camera sees. 3. If you can’t see the cube, select the Cube object in the Hierarchy, go to the Inspector view, and where it says Transform make the values in Position (0, 0, 0) 4. Select the Game tab (menu Window > Game if it isn’t already open) - you will also see a view from the camera. 5. Press the play button - the scene is now live in the game view, though it is not doing anything interesting at this point. Press play again to exit play mode.

Figure 5. An uninteresting cube not doing anything

Make the Cube Interesting

Let's make the cube come to life a bit more. 1. Select Cube in the Hierarchy. 2. Hit the ‘R’ key to change to scale mode. 3. Click on the little cube in the middle of the scale gizmo and drag your mouse. This will scale the object. Scale it up to about four times its normal size. You can see it’s scale changing in the Scale property in the Inspector view (alternatively you can just put (4, 4, 4) directly into this Scale property to achieve the same result). 4. Hit the ‘W’ key to change to translate mode. 5. Grab the green vertical arrow on the gizmo and drag it upwards two units (1 unit = 1 meter). You will see the Y value of Position in the inspector changing (alternatively you can just enter 2 into this Y property directly). 6. Preview the camera view to make sure the cube is now nice and large in the view (hit play or select the Main Camera object)

Now let's make it spin. 1. In the Project view there is a “Create” dropdown, select this and choose “C# Script”. Name the new script “Spin” 2. Double-click the new “Spin” file in the Project view and it will launch the C# IDE (perhaps MonoDevelop or Visual Studio depending on how you have set things up) 3. Add the lines of code to make it look like the following:

1. Switch back to Unity and you should see a little spinner in the bottom right hand corner - this means it is building your code. The spinner goes away if there are no errors in the console or status bar which means the code compiled correctly. If there are errors then these need to be addressed before proceeding. 2. Make sure the Cube object is selected in the Hierarchy. 3. In the Inspector view, click the “Add Component” button down the bottom, type in “Spin” (the name of your script file) into the filter box to find your script and then select it. This will add your script as a component to the Cube game object. 4. Hit the Play button and you should see an amazing spinning cube!

To tweak the cube while the scene is playing: 1. In the Game view ensure the “Maximise on Play” button is unchecked (you can check this button to make the Game view go fullscreen, but we want that off for now) 2. Press Play 3. Select the Cube in the Hierarchy view so that you see it’s properties in the Inspector view. 4. Change the value of the “Speed” property you created. You should see the cube responding to you changes by slowing down or speeding up.

NOTE: In general, any changes you make to values in the Inspector while the scene is in Play mode will be lost when you exit Play mode! This is a Unity feature. The idea is to tweak values while the scene is live so you can get instant feedback, but then you must record these new values yourself and update them when Play mode has exited.

Finally, let’s change the cube’s colour. 1. In the Project view, select Create > Material. Name the new material “Cube”. 2. Select the new material in the Project view to view it’s properties in the Inspector view 3. Next to “Albedo” (this is the primary colour channel for the material) is a colour picker that is currently white. Click this and change the cube to whatever colour you like

(e.g. a lovely shade of red) and close the colour picker. You will see a preview of the material colour change in the Inspector. 4. Now add the material to your Cube. In the Project view, click and drag the material all the way over to your Cube in the Hierarchy view. Drop it on, and the material is now applied to your cube. This is what you should see in Play mode:

Figure 6. Completed Hello Unity tutorial

Some Further Things to Try

1. While the scene is playing and the cube is spinning, switch back to Scene view and notice that you can use your Scene editor camera controls to move around the scene while it is playing. 2. Try modifying the Spin script to make it rotate around all three axis. 3. For the authentic hello world experience, try adding the following line to the Update method of the Spin script: Debug.Log("HELLO WORLD!"); ​ ​ ​ ​ This will spam the message to the console every frame.

Unity Concepts

This section highlights some of the key Unity concepts.

Game Objects and Components

Game Objects and components are core to the way you build scenes in Unity. A Game Object is basically an empty container that gets its behaviour and attributes from Components that are added to it. Every Game Object has at least a Transform component that cannot be removed. For example, a cube is a Game Object with a Transform (position, scale, and rotation), Mesh Filter (to hold the mesh), Mesh Renderer (to draw the mesh), and possibly a Box Collider (for the physics system). A light is a Game Object with a Light component on it, and a Camera is a Game Object with a Camera component on it. I think you should get the idea by now.

Unity has a rich set of pre-defined components such as Light, Camera, Mesh Renderer, Box Collider etc, but you can define your own components in C# script that can add whatever behaviour you wish to a Game Object.

Game Objects are added to the scene and are visible in the Hierarchy view. When selecting a Game Object in the Hierarchy view it’s components and their properties are viewable and modifiable in the Inspector view.

Scene Hierarchy

Game Objects exist in the scene in a hierarchy of parent-child relationships. To form such a relationship, simple drag a Game Object in the Hierarchy view and drop it on the Game Object you wish to become it’s parent. The hierarchy can be arbitrarily deep. The key to this is that when a parent object moves, all of its children move as if they were attached to it. Through this mechanism, quite complex objects can be built out of multiple Game Objects in a hierarchy.

Assets

All of the project’s assets are viewable in the Project view. An asset may be a mesh, audio file, text file, script etc. Even the scene when it is saved is an asset. Unity converts a number of assets into internal formats for use with the engine. Assets have import settings available for view in the Inspector view. You can drag and drop an asset from your file system (Finder or Explorer) into the Project view to place it directly in your project.

Manipulating Game Objects

Game Objects can be rotated, scaled, and translated in the Scene view. Simply select the object you wish to manipulate and use a hot key such as ‘W’ for Translate, ‘E’ for rotate, or

‘R’ for scale. Alternatively you can select the relevant button in the toolbar in the top left of the Unity interface to choose the respective behaviour.

Prefabs

Prefabs are Game Objects that have been saved as an asset for future reuse. When Game Objects are created from a prefab, any changes made to the prefab will be reflected in the Game Objects in the scene. To create a prefab, simply drag a Game Object from the Hierarchy view into the Project view. The Game Object in the Hierarchy view will turn blue to signify that it is linked to a prefab.

Scripting in C#

Most of the time you will be writing components in script that can be attached to Game Objects. These components will derived from a Unity class called MonoBehaviour. This behaviour defines a Game Object life cycle and has many predefined events that you can hook into such as Awake, Start, Update, OnDestroy etc.

The UnityEngine namespace provides a rich API for scripting in. There are utility classes for pretty much anything you want to build, and if it doesn’t exist there are many free or paid packages available on the Asset Store or Github.

Building an Executable

To build an executable simply go to File > Build Settings. Choose your target platform (you may need to install the relevant component with the installer to get access to it), configure Player Settings if required and hit build.

Learning Resources

When you start Unity, on the project chooser window there is a Learn tab with links to many Unity tutorials.

Alternatively you may want to search video tutorial websites such as Udemy or Udacity for courses on 3D graphics and Unity. Be aware that most of these courses will be focused on using Unity to produce games.

Unity themselves have a series of train material called Courseware that enables people to learn enough of Unity to pass Unity certification exams.