Game Design From Concepts To Implementation Giacomo Cappellini - g.cappellini@mixelweb.it
Why Unity - Scheme Unity Editor + Scripting API (C#)! Unity API (C/C++)! Unity Core! Drivers / O.S. API! O.S.! Hardware!
Unity Licenses Pro Unity Editor + Scripting API (C#)! Unity API (C/C++)! Unity Core! Drivers / O.S. API! O.S.! Hardware!
Unity Licenses Free Main limitations: No custom splash screen No video playback No native plugins (call platform functions from Unity scripts) No realtime shadows No fullscreen post-processing effects No navigation meshes (path finding) Complete list at: http://unity3d.com/unity/licenses
Unity Licenses Free Unity Editor + Scripting API (C#)! Unity API (C/C++)! Unity Core! Drivers / O.S. API! O.S.! Hardware!
Why Unity Cross Platform When working on top of Unity Unity Editor + Scripting API (C#)! Unity Core will take care of every cross-platform issue Unity Core! Here is where black magic happens - We don t have any access to it - You can just try to guess what s going on under the hood
Unity physical dissection Unity Editor PhysX engine Collection of asset importers Customized Mono environment Per-Platform Mono compiler MonoDevelop IDE Documentation + Scripting reference
Project script = Mono libraries Mono is an open source implementation of Microsoft's.NET Framework based on the standards for C#. In other words, it s a system that you can use to compile C# code and run it on different platforms. You may compile your code as an executable (.exe) or a library (.dll) Unity compiles your code into a.dll and calls the functions inside the Unity core
Components and Scripting
Unity logical dissection Scene = A holds zero or more of B GameObject Script (Component) Assets Material Shader Mesh Sound Texture Binary
Component A component is a Unity Script that has been attached to a GameObject There s no limit to the number of components you can assign to a GameObject The code that you write in your Unity Script should describe a single Behaviour The sum of the behaviours describes the real nature of the GameObject itself
Component Components are the most important part of your logic Unity comes with a limited number of built-in components You can use b.i.c. to send instructions to: Render system Physics system Audio system and others You can think of built-in Components as special scripts that are able to use private API functions that you cannot use inside custom scripts.
Component By using custom components you are able to call functions on different components on the same GameObject, or on a different GameObject, or use external resources and even modify the behaviour of the whole Engine. Assets Material Shader Mesh Sound Texture Binary We will expose later some of the power features of the Unity Scripting Engine
Component FlyBehaviour.cs Eagle It wanders around It flies WandererBehaviour.cs Wolf It wanders around It howls HowlingBehaviour.cs You can save a lot of coding by using behaviour combination
Component -- AVOID THIS WHENEVER POSSIBLE -- EagleBehaviour.cs Eagle Wolf WolfBehaviour.cs
Unity Editor - Working with Components -
Creating a new project You are given a form to select the project destination path and a checkbox list of packages. Packages are compressed bundles of Scripts and Assets that you can import on project initialization or at a later time.
GameObject Create Other Empty An Empty GameObject has no graphics and no behaviour, it has noting but a Transform. It is a point in space ready to be configured. It s like the steam cell of any game element.
GameObject Naming and tagging You can rename any GameObject by slowly double-click on it inside Hierarchy window or by using the Inspector s name field You can also assign a tag to the GameObject Tags can be used by scripts to search throughout the hierarchy tree.
Editor Components If you take a look inside the Hierarchy Create menu or GameObject Create Other menu you find a set of predefined GameObjects The differences between a predefined GameObject and an Empty GameObject are the Components that it comes with when created Now create end select a Cube
Editor Components You can think of a Cube like an Empty with: 1. A Mesh Filter Component 2. A Mesh Renderer Component 3. A Box Collider Component WARNING: Transform is not a Component. You cannot remove the Transform from the GameObject that owns it
Editor Components 1. Component Mesh Mesh Filter 2. Component Mesh Mesh Renderer 3. Component Physics Box Collider
Editor Attributes Components have attributes and options Mesh Filter: has a Mesh attribute of type Mesh Mesh Renderer: Has 3 Booleans (Cast/Receive Shadows + Light Probes ) and an Array of Material Box Collider: Has 1 Boolean, a Physics Material, and 2 Vector3 fields ( Center and Size )
Custom Objects 1. Create an Empty GameObject 2. Name it Custom Cube 3. Add these two components on Custom Cube 1. Mesh -> Mesh Filter 2. Mesh -> Mesh Renderer 3. Physics -> Sphere Collider 4. Set the mesh attribute of the Mesh Filter component to Cube 5. Set the Materials -> Element 0 attribute to Default-Diffuse Now compare your Custom Cube and the Cube object we have previously created
Editor Custom Components Custom Component = Unity Script Assets Create C# Script using UnityEngine; using System.Collections; public class MyFirstScript : MonoBehaviour { // Use this for initialization void Start () { Name it MyFirstScript and double click on it: MonoDevelop will start } } // Update is called once per frame void Update () { }
Editor MonoDevelop
Editor MonoDevelop MonoDevelop is an Integrated Development Environment originally made for working with the Mono Framework and later adapted for being Unity s default script editor Note: MonoDevelop is not the COMPILER of your executable code. You can use the Build function to check the correctness of your code, but Unity will re-compile your files internally and without asking after any code modification. You can also edit your files using your preferred text editor.
Editor Custom Components Now drag the Script you ve just created from the Project window on any GameObject in your Hierarchy window. Ta-Dah! You ve just turned a Script into a Component! This is the starting point of any custom logic of your game. Inside the C# Script you re expected to describe the Behaviour of every GameObject with the same script attached.
Editor MonoBehaviour Every Script is a new C# new class that extends MonoBehaviour http://docs.unity3d.com/documentation/scriptreference/monobehaviour.html
Editor Script Attributes using UnityEngine; using System.Collections; public class MyFirstScript : MonoBehaviour { public bool mycustombool; public float mycustomfloat; public int mycustomint; public Vector3 mycustomvector3; public AudioClip mycustomaudioclip; public Mesh mycustommesh; public Material mycustommaterial; // Use this for initialization void Start () { } // Update is called once per frame void Update () { Now some magic: public attributes = Component options! } }
Editor Script Attributes
Editor Script Attributes Every Component is sharing the same C# code but the attributes/options are different on each GameObject
Start() function and Update() function When you create a new Script, you are given this template // Use this for initialization void Start () { } // Update is called once per frame void Update () { } You are expected to write your code into those functions but, how to use them? Do you know what a frame is?
What is a frame? A frame is an unique image, just like in movies The frame rate (aka frames per second or fps) are: how many frames your hardware is able to compute in 1 second Depending on: the capabilities of your hardware the complexity of your scene the available resources the fps of your running project won t be constant, and you shouldn t assume that.
Frame -> Update() Frames are being calculated by your hardware fast as possible To achieve game that doesn t look and feel bad you need, at least, to have 30fps. Otherwise your game will just feel stuttering all the time. The Update() function is getting called, on each script, every time Unity computes a new Frame 30fps =~ 0.0333s per frame 60fps =~ 0.0166s per frame So, you should do (very) small changes on each Update call
Frame -> Update() + transform http://docs.unity3d.com/documentation/scriptreference/transform.html Or just search Unity Transform on the Internet You see a number of variables and functions available on the Transform object. You can type something like: transform.translate( 1, 0, 0 ); Inside your Update() function, just between the braces. Save the.cs modification (ctrl+s) and switch back to Unity What s going to happen?
Frame -> Update() + transform transform.translate( 1, 0, 0 ); Is like adding 1 on the X value of the position of the object s transform, every frame. It means that the object is going to move on the X axis by frames per second units every second So IF your hardware is able to perform 200 fps, the object would move 200 units every second. And it would go slower on different hardware or just when your computer gets busy doing calculating else. You don t want that, you want your game to work in a framerate independent way.
Time.deltaTime Your workstations are probably able to compute more than 200 fps inside our simple scene. Inside your Update() function you can use a float value named Time.deltaTime to make the modifications in your scene frames per second independent. Time.deltaTime contains the time (in seconds) that Unity has spent while calculating the previous frame. How about: transform.translate( 1 * Time.deltaTime, 0, 0 );
Feedback from your script While writing your script you have 2 ways to get feedback from your logic. 1. Run the script and check if it s behaving like expected. 2. Use Debug.Log( Some text ) and ask for a value: Debug.Log( The local X is + transform.localposition.x ); Debug.Log( The global X is + transform.position.x ); Debug output will be printed inside Unity Console. Open it using Window -> Console
Scripting Playground Download the Unity Project here: http://bit.ly/1bbwtyr Goals: 1. Fix Translate script using deltatime 2. Fix Rotate script using deltatime 3. Make Rotate use one or more variables as input instead of constants, just like TranslateWithVariable 4. Fix TranslateWithVariable using deltatime Brave enough? Make a script that moves that moves on the right (+X), but after 5 seconds it starts going the other way (-X). HINT: create a float variable and add Time.deltaTime to it each frame, then use it to calculate how much time has passed.