Unity Lecture 1 PDF
Document Details
Uploaded by Deleted User
Dr. Rasha Mahmoud Kamel
Tags
Summary
This document provides an overview of Unity, a game engine, and its various features. It includes a description of Unity's basic concepts, user interface elements, and components, as well as examples of using Unity's features. The presentation likely covers Unity's basic functions and concepts, like creating objects, and scripting procedures. It contains illustrative diagrams and code examples, making it suitable for beginners learning Unity.
Full Transcript
Unity Lecture 1 By Dr. Rasha Mahmoud Kamel What is Unity? Unity is a powerful cross-platform game engine and a user-friendly development environment that provides us with a complete set of tools we can use in our code quickly and easily. Unity provides a complete set of to...
Unity Lecture 1 By Dr. Rasha Mahmoud Kamel What is Unity? Unity is a powerful cross-platform game engine and a user-friendly development environment that provides us with a complete set of tools we can use in our code quickly and easily. Unity provides a complete set of tools to create characters and objects that your code can interact with. These assets all live in the project directory as many separate files or entities that you can control through code. Unity has a built-in physics system (for collisions, gravity, and various other forces), particle systems (for explosions, fire, and other special effects), animation systems, audio system, lighting tools, UI system, rendering system, etc. Unity supports art assets and file formats from 3ds Max, Maya, Blender, Adobe Photoshop and other graphics programs. These assets can be added to the game project and managed through Unity's graphical user interface. 2 What is Unity? Unity provides a set of scripting tools that the developer can use to create custom components for GameObjects. To control animations / scene changes in Unity, we use scripting through various programming languages such as Boo, C#, and JavaScript. A game in Unity is made up of many different C# files. Each file can be referred to as a “class” and end with the.cs file extension (which stands for C Sharp) and named according to its purpose. Unity supports deployment to multiple platforms such as desktop platforms (Windows, Mac, and Linux), mobile devices (Android, Apple iOS), consoles (Playstation, Xbox), web browsers, etc. 3 Unity Basic Concepts Project: The project contains all the elements that makes up the game, including models, assets, scripts, scenes, and so on. Scenes: A scene contains a collection of game objects that constitute the world that the player sees at any time. A game generally will contain many scenes. For example, different levels of a game would be stored as different scenes. Assets: An asset is any resource that will be used as part of an object’s component such as meshes (for defining the shapes of objects), materials (for defining shapes), physics materials (for defining physical properties like friction), and scripts (for defining behaviors). 4 Creating a Project To create a new project: 1) Click the Projects tab on the left side in the Unity Hub. 2) Click the blue “New project” button in the top-right corner. A dialog box will appear allowing you to select a template to base the 5 project on. Creating a Project 3) Select the 3D template. 4) To the right side of the template options, change the project name and the directory (file path) on your computer to save the project files to. 5) Once you’ve selected the path you want, click the blue “Create project” button in the bottom- right corner. 6 Creating a Project If you navigate to the place where you saved your project you will see a directory with the name of your project and at least three main directories inside (Assets, Library, and ProjectSettings). Assets: It is the root directory for the Unity editor ’Project Panel’ in which all of your C# files, all of the things you import, and any other game objects such as 3D models, 2D textures, and sounds will be saved in this directory. If a file isn’t located in this directory, your game will not be able to access it. Library: This directory will contain all of the temporary build files Unity creates when you play a game within the editor. ProjectSettings: This directory contains all of the settings you have customized for your project. So, for example, if you set the global gravity setting to be -20 instead of -9.81, this change will be saved in a file called Physics2DSettings.asset. 7 Main Interface 8 Interface – Hierarchy Panel This window depends on the current scene you have open, so its contents will look different for different scenes. This is important because the Hierarchy panel is a list of all of the GameObjects (things) currently in this level. In a new scene, Unity creates a Main Camera and a Directional Light by default. When we add something to the game, it will appear in the Hierarchy Panel. Even when you are running the game, spawned objects will appear in the Hierarchy and deleted objects will disappear. 9 Interface – Hierarchy Panel The create button at the top of the window allows you to create new GameObjects, add effects, add UI and etc. The search bar filters through the list of GameObjects. If you drag and drop one GameObject onto another GameObject, the moved GameObject becomes a child of the other GameObject, inheriting its Transform (Position, Rotation, Scale). This is why it’s called the Hierarchy, it is a list of GameObjects with a structure of Parent/Child relationships. 10 Interface – Scene View It shows the 3D view of the game scene. The Scene View is a window that lets you explore and edit the game world. All the objects that we create in the Hierarchy window will appear here in their corresponding X, Y, Z positions. We can move objects around to place them, scale them, rotate them, or even pan your own view through the world. 11 Interface – Scene View The sample scene doesn’t really have anything inside it yet – just a light, which is an invisible object that casts light over everything in the scene, and a camera, which is the object through which the player will see the scene when the game is being played. 12 Interface – Scene View Within the Scene window, moving your mouse while holding right-click will turn the camera. While holding right-click, you can use the WASD keys to move the camera around (W to move forward, S to move backward, A to move left, D to move right, Q to move directly down, and E to move directly up). 13 Interface – Scene View The scene is more like a construction view of the game that cannot be seen by the player, only you as the creator of the game can access the scene view. There are six buttons to toggle between different modes (pan, 1 2 3 4 5 6 move, rotate, scale, etc.). You can use the hotkeys Q, W, E, R, T, 1 Pan Button and Y to toggle between these tools. 2 Translate Button Hand (Pan) Button [Q]: this allows navigation of the Scene 3 Rotate Button window. It allows you to drag around in the Scene window to pan your view. 4 Scale Button Translate Button [W]: this allows to reposition the selected 5 Rect Button object using the object’s axis handles. 6 Transform Button 14 Interface – Scene View Rotate Button [E]: this allows to rotate the selected object around each axis Scale Button [R]: this adjusts the size or scale of an object using visual handles. 1 2 3 4 5 6 Rect Button [T]: this also adjusts the size or scale of an object 1 Pan Button but in a different way. It mainly used when working with UI. 2 Translate Button Transform Button [Y]: this combines all tools. It can be used to 3 Rotate Button perform translation, rotation, and scaling for the selected object. 4 Scale Button Given that you are not able to see an object selected in the 5 Rect Button Hierarchy in the Scene window, press the F key to focus the Scene view on that object. 6 Transform Button 15 Interface – Game View The Game panel is the view of the game through the Main Camera. Unlike the Scene view, which allows you to explore the game world, the Game view is a window that previews what the game would look like right now, if you were to build the game. 16 Interface – Game View The Game view is controlled by the Play/Pause/Step buttons along the top of the Editor. It is important to warn that the changes you make to any parts of your game scene while in PLAY MODE will NOT be saved once you leave play mode. 1 2 3 So, if you play the game and move things around, when you 1 Play Button press PLAY again to stop the game, things will revert to their original positions. 2 Pause Button 3 Step Button This will result in lost work if you aren’t careful. 17 Interface – Inspector Panel This panel will display all of the information of the thing you currently have highlighted. When you click on a GameObject, you will be shown all of the values and information about this GameObject. The Inspector panel is used to perform a change in the information of a specific GameObject. Add Component button in this panel is one of the many ways in which we can assign a C# file to an object in the scene. 18 Interface – Project Panel It shows us the list of all assets (3D models, scripts, audio files, images, etc) we create in the entire project. As we create new C# files, they will be shown to us in the Project view. Specifically, the Project panel reflects the Assets/ directory. Anything we add to the Assets/ directory automatically appears in the Project panel, and any changes you make to the Project panel changes the Assets/ directory directly. 19 Interface – Project Panel To import an asset to the Project panel, RIGHT CLICK in the Project panel Import New Asset. The second way is to go through the menu at the top: Assets Import New Asset. The easiest way to add something from the Project Panel into the current scene is to drag it into the Hierarchy (or the Scene View). 20 Interface – Console Panel This view displays all of the information that is being created by the C# code we write. Any problems, warnings, and errors will be presented in this panel. Messages Warnings Errors 21 Game Object Game objects are all the “things” that constitute the scene. Game objects not only include concrete objects (e.g., a chair in a room), but also other elements that reside in space such as light sources, audio sources, and cameras. Every game object (even empty ones) has a position and orientation space. Thus, it can be moved, rotated and scaled. Game objects can be enabled or disabled where disabled objects behave as if they do not exist. It is possible to associate various elements with game objects, called components. To create a blank GameObject, RIGHT CLICK in the Hierarchy panel and select 22 ’Create Empty’. The object is created in the Hierarchy named ’GameObject’. Game Object There are several toggles, text fields, and dropdown menus that are appeared at the very top of the Inspector window when the object in the Hierarchy panel is selected. The leftmost toggle enables/disables the GameObject, effectively turning it on or off in the Game. The text field is the name of the GameObject, currently called ’GameObject’. When we change the name to ’Test’ and press ENTER, the name in the Hierarchy should be changed. 23 Components Each game object is defined by a collection of associated elements that are called components. A component is an individual piece of code that does a single thing. Components that are associated with a GameObject depend on the nature of object and define its behavior. Unity has several pre-defined components that handle rendering, physics, even basic character controls. We can also define custom components, called scripts, which allow us to create unique components that do a specific thing and control how the object behaves and how it reacts to its environment. Components that are associated with a GameObject can be viewed and edited in the Inspector window. 24 Transform Component Every GameObject has a component called Transform. This component determines the GameObject’s position, rotation, and scale in the game world. Changing these values are the same as using the movement tools in the Scene view. 25 Adding/Removing Component At the very bottom of the Inspector is a button that says “Add Component”. This button is used to add components that are pre-defined in the Unity Engine as well as any custom scripts written in the project. If the name of the component is known (which matches the class name of the script), we can use the search bar to find it faster. 26 Adding/Removing Component To remove a component, right click on the title of the component and select Remove Component or click on the Cog icon to the right of the component and choose Remove Component from the drop-down menu. 27 Script A script is a chunk of code that attached to game objects to define their behaviors. There are various types of scripts classes, depending on the type of behavior being controlled. Because interactive game programming is eventdriven, a typical script is composed as a collection of functions, each of which is invoked in response to a particular event. (e.g., A function may be invoked when this object collides with another object.) Typically, each of these functions performs some simple action (e.g., moving the game object, creating/destroying game objects, triggering events for other game objects), and then returns control to the system. 28 Creating a New Script For writing some code, right click in the Project panel and select Create C# Script. This will create a new script in the Project panel and highlight the filename. Name the Script and press ENTER to save the script. Double click on the script to open it for editing. Unity opens Microsoft Visual Studio, an IDE for C#. If a different IDE is opened, go to Edit Preferences External Tools External Script Editor to select an IDE. 29 Creating a New Script Once the script opens, you should have a basic skeleton script. This will be the default script every time you create a new script. One important thing to check right away is to make sure the class name of your script matches the filename in Unity. The class HelloWorld inherits from the MonoBehaviour which is the bass class for all new Unity scripts. It contains a list of all the functions and events that are available to standard scripts attached to GameObjects. 30 Creating a New Script Inheriting from MonoBehaviour class provides a couple of methods that are automatically included: Start() and Update(). Start() is called once when the object becomes active (which is generally as soon as the scene with that object has loaded). Update() is called every frame. Note that although the code is written in Visual Studio, it isn’t run there. The code is run when you click Play within Unity. 31 Creating a New Script Include namespaces for Unity and Mono classes The syntax for inheritance Put code here that runs once Put code here that runs every frame 32 Creating a New Script The Debug.Log() command prints a message to the Console view in Unity. Meanwhile, that line goes in the Start() method because, as was explained earlier, that method is called as soon as the object becomes active. Once the log command is added, save the script and go to Unity. The method isn’t named Debug.Log. It’s named Log, but the method is a field inside the Debug class. 33 Adding Script As A Component Now, we need to create a GameObject, like a simple Cube or an empty GameObject, in the scene to attach the script to. Choose GameObject Create Empty, and a blank GameObject will appear in the Hierarchy list. To assign the C# script to an object in the scene, we can use one of the following methods: 1) Drag the script from the Project view and drop it on the object in the Hierarchy view. 34 Adding Script As A Component 2) Select the object in the Hierarchy view so that the Inspector panel will display its properties. Drag the script from the Project view and drop it anywhere under the properties in the Inspector panel. 35 Adding Script As A Component 3) Select the object in the Hierarchy view so that the Inspector panel will display its properties. Press the Add Component button and pick Scripts to select the specified script in your project. 36 Adding Script As A Component To verify that the script is attached to the object, select the GameObject and look at the Inspector view. You should see two components listed: the Transform component, which is the basic position/rotation/scale component all objects have and which can’t be removed, and below that, your script. Note that we can add the same script to hundreds of GameObjects in the scene, and each script will have its Start method. 37 Printing Hello World! – Console Panel Click Play in Unity and switch to the Console view to see the message Hello World!. 38 Printing Hello World! – Console Panel Note that, if you forget to type ; at the end of the line, an error message will appear in the Console tab with a red error icon. Script containing the error Description of this error Location within this script (line, character) 39