Unity Introduction Kingsley Stephens, Monash University
Total Page:16
File Type:pdf, Size:1020Kb
Unity Introduction Kingsley Stephens, Monash University Introduction 2 What is Unity? 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 game engine 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 Microsoft 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).