Chapter 1. Working with Unity

To make things in Unity, it’s important to understand how Unity works and how to work with it as a piece of software. In this chapter, we’ll take a tour through the interface and features of Unity, so that you’ll be ready to start building things in the editor.

Tip

If you’re new to Unity, we recommend reading all the recipes in this chapter, as well as in Chapter 2, before trying other recipes in this book.

This chapter also introduces you to many of the terms used in the Unity ecosystem, which will help you make sense of the rest of this book.

1.1 Getting Around in Unity

Solution

Unity’s user interface is organized along windows of content. Each different view can do different things. In this recipe, we’ll present some of the most important ones and talk about how to arrange the editor to suit your needs.

When you launch Unity and create a new project, you’ll be taken to the editor’s main window (Figure 1-1).

ugdc 0101
Figure 1-1. The main window of the Unity editor

There are several features that are of particular importance to anyone working in Unity. Let’s take a look at each of them!

Toolbar

The toolbar contains controls that affect Unity as a whole (Figure 1-2). It’s always at the top of the editor window, and can’t be moved.

ugdc 0102
Figure 1-2. The toolbar

The toolbar, from left to right, contains the following controls:

Tools palette

This palette controls the behavior of the transform controls that appear when an object is selected. Only one mode can be selected at a time; they are:

Hand tool

Clicking and dragging in the Scene view will move your view of the scene.

Move tool

Objects that are selected can be moved.

Rotate tool

Objects that are selected can be rotated around their pivot point or center.

Scale tool

Objects that are selected can be scaled around their pivot point or center.

Rectangle tool

Objects that are selected have a rectangle drawn around them, and can be scaled and repositioned. This tool is largely used for 2D objects like sprites and user interface elements.

Transform tool

This tool combines the Move, Rotate, and Scale tools. Selected objects can be moved, rotated, and scaled.

Custom tool

Any custom tools that are defined by the code in your game will appear in here.

Pivot/Center toggle

This toggle sets whether the transform controls are placed at the local pivot point of the object or in the center of the object’s volume. (This can be different for certain 3D models; for example, models of human characters typically place the pivot point at the character’s feet.)

Local/Global toggle

This toggle sets whether the Transform tool operates in global space or local space. For example, in local space, dragging the blue “forward” arrow of the Move tool moves an object forward based on its own orientation, while in global mode, dragging the blue “forward” arrow ignores the object’s orientation.

Play button

This button starts Play mode, which enters your game. You can click the button again to end Play mode and return to editing.

Warning

You can edit the scene while in Play mode, but any changes you make to the scene will be lost when you end the game. Don’t forget to check if you’re playing the game or not before doing a large amount of work!

Pause button

This button pauses the game. If you’re in Play mode, the game will pause immediately. If you’re not in Play mode, you can still click this button; if you then click the Play button, the game will pause immediately after the first frame.

Step button

This button advances one frame, while keeping the game paused.

Collab menu

This menu provides controls for working with Unity Collaborate, Unity’s version control service.

Services button

This button opens the Services view, which allows you to work with Unity’s web-based services like Cloud Build, Unity Analytics, and more. For more information, see Unity’s Services page.

Account button

This button allows you to configure your Unity account.

Layers button

With this button you can choose which layers are currently visible or selectable.

Layout button

This button allows you to save and restore a predefined layout of windows. This is useful when you have a layout that works well for tasks like animation, or level layout, and you want to switch between different tasks without having to fiddle with precise window placement.

Scene

The Scene view allows you to view, select, and modify the objects in a scene (Figure 1-3). In the Scene view, you can left-click on any object to select it; when an object is selected, you can move, rotate, or scale it by using the transform controls, depending on which tool you have selected in the toolbar (see Figure 1-4).

ugdc 0103
Figure 1-3. The Scene view

To use the transform controls on a selected object, click and drag the arrows (when using the Move tool), circles (the Rotate tool), or boxes (Scale tool) attached to it. Hold down the Shift key to snap the movement, rotate, and scale to a predefined increment. To move around the Scene view, select the Hand tool from the palette at the top left of the view, and click and drag. You can also hold the Alt key (Option on a Mac) to make clicking and dragging rotate the view; Alt-Control (Option-Command) and drag will pan the view around. You can use the scroll wheel on your mouse, or a two-finger gesture on a trackpad, to zoom in and out.

Tip

You can also quickly move the Scene view to focus on the currently selected object. To do this, press the F key.

ugdc 0104
Figure 1-4. The Transform tool, shown with object selected. In this image a Camera has been selected, and the Transform tool is in Move mode; clicking and dragging the arrows will move the selected object in that direction.

Inspector

The Inspector shows information about the objects currently selected (Figure 1-6). From here, you can control every component attached to the game object.

ugdc 0106
Figure 1-6. The Inspector

At the top of the Inspector, you can set the name of the currently selected object. You can also set an icon for the object by clicking the icon to the left of the object’s name and choosing a symbol to use. This is useful for game objects that are invisible.

By default, the Inspector will change its contents when the current selection changes. If you don’t want it to do this, you can click the Lock icon at the top-right corner of the Inspector, and it will stay on the object that it’s currently displaying.

Hierarchy

The Hierarchy shows the list of objects in the current scene (Figure 1-7). From here, you can browse through the list of objects in your scene. If you select an item in the Hierarchy, it becomes selected in the Scene view, and vice versa. You can also drag and drop one object on another to make it a child of that second object.

ugdc 0107
Figure 1-7. The Hierarchy view

1.2 Working with Game Objects

Discussion

You can also create new game objects by dragging and dropping assets into the Scene view. For example, if you drag and drop a 3D model asset into the Scene view, Unity will create a new game object that contains the necessary components for rendering that model.

As a shortcut, you can quickly create an empty game object as a child of the currently selected object by opening the GameObject menu and choosing Create Empty Child. You can also press Option-Shift-N (Alt-Shift-N on a PC).

1.3 Working with Components

Solution

On its own, a game object is just an empty container. It’s the components that make a game object actually do anything useful.

To get started in thinking about components, we’ll create a new game object that comes with some useful components built in: we’ll add a cube to the scene!

To do this, follow these steps:

  1. Open the GameObject menu, and choose 3D Object → Cube. A new cube will be added to the scene (Figure 1-9).

  2. Select the new cube in the Hierarchy or the Scene view. The Inspector will update to show the list of components attached to it (Figure 1-10).

ugdc 0109
Figure 1-9. A new cube, freshly added to the scene
ugdc 0110
Figure 1-10. The Inspector for the new cube

Every game object has at least one component: a Transform. The Transform component stores the position, rotation, and scale of an object, and is also responsible for keeping track of the parent of an object. You can’t remove the Transform component.

On the cube, you’ll find several additional components. Each of them do something different:

Components let you configure how they work by exposing properties that are listed in the Inspector. For example, the MeshFilter component has a single property: the Mesh that it should be using. This is an example of an object field—it’s a reference to another object that’s part of your project. In the case of the MeshFilter, the field can take any Mesh object; these are found in the assets you add to your project. The type of object that an object field can use is determined by the component; you can’t drop any other kind of object besides a Mesh into this field, for example.

Note

Object fields don’t have to refer to assets—they can also refer to other objects in the scene, too.

To add a component yourself, you can either use the Component menu or click the Add Component button at the bottom of the Inspector. Both of these options will let you specify what kind of component you want to add.

1.4 Working with Prefabs

1.7 Building Unity Projects

Solution

To build your game, open the Build Settings view by opening the File menu and choosing Build Settings (Figure 1-13).

ugdc 0113
Figure 1-13. The Build Settings view

When you build your game, you specify which scenes should be included. If you haven’t already saved your current scene, you should do so now by opening the File menu, and choosing Save, or by pressing Command-S (Control-S on a PC). You can drag and drop the scenes you want to include into the Scenes In Build list, or you can click the Add Open Scenes button to add all scenes that you have open right now.

Next, you need to select which platform you want to build for. Unity supports a wide range of platforms, ranging from desktop PCs to mobile phones to consoles and more. Only one platform can be selected at a time; the current platform will be marked with a Unity logo next to it. To build for a different platform, select it and click the Switch Platform button.

When you’re ready, click the Build button. Unity will ask you where you want to save the build; once that’s done, Unity will start building.