This is an old revision of the document!
VIDEO here - soon
Hotkeys
Folders inside unity
Unity reserves some project folder names to indicate that the contents have a special purpose. There are a number of folder names that Unity interprets as an instruction that the folder's contents should be treated in a special way. Some of these folders have an effect on the order of script compilation. These folder names are:
Asset Bundles, Resources and StreamingAssets
There are several ways to serve up resources to Unity at runtime, and each method has its place in game development: asset bundles, resource folders, and streaming assets.
# Asset Bundles:
Asset bundles let you deliver content to your application outside of your Unity build. Generally, you'd host these files on a remote web server for users to access dynamically. Asset bundles can contain anything from individual assets to entire scenes, which also makes them ideal for delivering downloadable content (DLC) for your game.
# Resource Folders:
The Resource folders are bundled managed assets. That means they will be compressed by Unity, following the settings you apply in the IDE. Unlike Asset bundles, resource folders are baked into the Unity Player as part of the game. You can do this by adding the required assets to a folder named Resources in your assets directory.
# Streaming Assets:
Like Resource Folders, a Streaming Assets directory can be created by intuitively creating a folder named StreamingAssetsthat, that you can use to put bundled un-managed assets. Unlike Resource folders, this directory remains intact and accessible in the Unity player. This creates a unique access point for users to add their own files to the game.
Prefabs In Unity 3d
Prefab in Unity 3D is referred for pre-fabricated object template (Class combining objects and scripts). At design time, a prefab can be dragged from project window into the scene window and added the scene's hierarchy of game objects. If desired the object then can be edited.
At the runtime, a script can cause a new object instance to be created at a given location or with a given transform set of properties.
In Unity scripting, there are a number of event functions that get executed in a predetermined order as a script executes. This execution order is described below:
Difference between Start and Awake Unity Events
Awake as ” initialize me” and Start as ” initialize my connections to others.” You can't rely on the state of other objects during Awake, since they may not have Awoken themselves yet, so it's not safe to call their methods.
Start function is called only if the script component is enabled. Awake function called when the script instance is being loaded.
If the script is NOT enabled at the beginning of your game, and you don't need the variables to be initialized, start() would be saving performance as awake() would be called regardless.
Difference between Update, Fixed Update and Late Update.
Update: (Most things)
FixedUpdate:(Physics things)
LateUpdate:(After update)
time.deltatime
In unity, Time.deltaTime will provide you time in seconds it took to complete the last frame (Read Only). This can be used to make your game frame independent.
When you multiply any value with Time.deltaTime you essentially express: I want to move this object 10 meters per second instead of 10 meters per frame.
For more details please visit “Edit → Project Settings → Time” OR From scripting, check out the [Time](http://docs.unity3d.com/ScriptReference/Time.html) class.
public class ExampleClass : MonoBehaviour { void Update() { float translation = Time.deltaTime * 10; transform.Translate(0, 0, translation); } }
Coroutines
While Coroutines seem to work like threads at first glance, they actually aren't using any multithreading. They are executed sequentially until they `yield`. Coroutine might seem like it is a thread, but coroutines execute within the main thread.
The difference between a coroutine and a thread is very much like the difference between [cooperative multitasking](https://en.wikipedia.org/wiki/Cooperative_multitasking) and [preemptive multitasking](https://en.wikipedia.org/wiki/Preemption_(computing)). Note that a coroutine runs on the main thread and must voluntarily yield control back to it, if control is not yielded (this is where the coroutine must be _cooperative_) then your coroutine will hang your main thread, thus hanging your game.
The disadvantage of not doing “real” multithreading is that you can not use coroutines to parallelize CPU-intense calculations over multiple CPU cores. With the release of Unity 2017, it is now possible to use a new C# feature called [async-await](https://blogs.msdn.microsoft.com/appconsult/2017/09/01/unity-coroutine-tap-en-us/) for our asynchronous methods instead. This comes with a lot of nice features compared to coroutines.
Let's look at a simple example. Given the following coroutine:
public class AsyncExample : MonoBehaviour { IEnumerator Start() { Debug.Log("Waiting 1 second..."); yield return new WaitForSeconds(1.0f); Debug.Log("Done!"); } }
The equivalent way to do this using async-await would be the following:
public class AsyncExample : MonoBehaviour { async void Start() { Debug.Log("Waiting 1 second..."); await Task.Delay(TimeSpan.FromSeconds(1)); Debug.Log("Done!"); } }
Script compilation order inside Unity
There are four separate phases of script compilation. The phase where a script is compiled is determined by its parent folder. In a script compilation, the basic rule is that anything that is compiled in a phase _after_ the current one cannot be referenced. Anything that is compiled in the current phase or an earlier phase is fully available.
The phases of compilation are as follows:
A common example of the significance of this order occurs when a UnityScript file needs to reference a class defined in a C# file. To achieve this, you need to place the C# file inside a Plugins folder, and the UnityScript file in a non-special folder. Because C# code is compiled before JS code, so in general, while JS code can access C# classes, the opposite is not possible If you don't do this, an error is thrown saying that the C# class cannot be found.
Classes and Structures
One of the basic design decisions every framework designer faces is whether to design a type as a class (a reference type) or as a struct (a value type).
Similarities:
Differences:
The Main difference between reference types (Class) and value types (Struct) we will consider is that reference types are allocated on the heap and garbage-collected, whereas value types are allocated either on the stack or inline in containing types and deallocated when the stack unwinds or when their containing type gets deallocated.
Static Classes and Singleton
Both can be invoked without instantiation, both provide only one “Instance” and neither of them is thread-safe.
A [static](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static) class is basically the same as a non-static class, but there is one difference: a static class contains only static members and a private constructor and cannot be instantiated. In other words, you cannot use the [new](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/new) keyword to create a variable of the class type. The design style embodied in a static class is purely procedural.
Singleton, on the other hand, is a pattern specific to OO design. It is an instance of an object (with all the possibilities inherent in that, such as polymorphism), with a creation procedure that ensures that there is only ever one instance of that particular role over its entire lifetime.
The Virtual keyword
The 'virtual' keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. By default, methods are non-virtual and you cannot override a non-virtual method. You cannot use the `virtual` modifier with the `static`, `abstract`, `private`, or `override`modifiers.
For example, this method can be overridden by any class that inherits it:
public virtual double Area() { return x * y; }
Abstract Classes and interfaces
An abstract class is a special kind of class that cannot be instantiated. An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated.
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.
When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared `abstract`. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:
Feature | Interface | Abstract class |
— | — | — |
Multiple inheritances | A class may inherit several interfaces. | A class may inherit only one abstract class. |
Default implementation | An interface cannot provide any code, just the signature. | An abstract class can provide complete, default code and/or just the details that have to be overridden. |
Access Modfiers | An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public | An abstract class can contain access modifiers for the subs, functions, properties |
Core VS Peripheral | Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. | An abstract class defines the core identity of a class and there it is used for objects of the same type. |
Serialization and De-Serialization
Serialization:
Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
The object is serialized to a stream, which carries not just the data, but information about the object's type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.
Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange.
Deserialization:
As the name suggests, deserialization is the reverse process of serialization. It is the process of getting back the serialized object so that it can be loaded into memory. It resurrects the state of the object by setting properties, fields etc.
Types of serialization:
Methods and functions
A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value). All data that is passed to a function is explicitly passed.
A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:
Generic Functions and Generic Classes
Generic classes encapsulate operations that are not specific to a particular data type. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on. Operations such as adding and removing items from the collection are performed in basically the same way regardless of the type of data being stored.
Generic functions are similarly those which describe the algorithm without specifying a particular data type.
Variables and Parameters
Variables can be controlled from the Unity Editor as parameters, as long as they are defined public or serializable. We can use both simple variable or arrays.
public float speed; public Sprite[] images; private float internalBleed; //cannot be controller from Unity Editor interface [SerializeField] private Text scoreText; [SerializeField] private GameObject[] enemyTypes;
MonoBehaviour Event Execution Order
Ordered by first to last method to execute.
private void Awake() { /* Called when the script is being loaded */ } private void OnEnable() { /* Called every time the object is enabled */ } private void Start() { /* Called on the frame when the script is enabled */ } private void Update() { /* Called once per frame */ } private void LateUpdate() { /* Called every frame after Update */ } private void OnBecameVisible() { /* Called when the renderer is visible by any Camera */ } private void OnBecameInvisible() { /* Called when the renderer is no longer visible by any Camera */ } private void OnDrawGizmos() { /* Allows you to draw Gizmos in the Scene View */ } private void OnGUI() { /* Called multiple times per frame in response to GUI events */ } private void OnApplicationPause() { /* Called at the end of a frame when a pause is detected */ } private void OnDisable() { /* Called every time the object is disabled */ } private void OnDestroy() { /* Only called on previously active GameObjects that have been destroyed */ }
Physics updates on a Fixed Timestep are defined under Edit ▸ Project Settings ▸ Time ▸ Fixed Timestep and may execute more or less than once per actual frame.
private void FixedUpdate() { /* Called every Fixed Timestep */ }
GameObject Manipulation
/* Create a GameObject */ Instantiate(GameObject prefab); Instantiate(GameObject prefab, Transform parent); Instantiate(GameObject prefab, Vector3 position, Quaternion rotation); /* In Practice */ Instantiate(bullet); Instantiate(bullet, bulletSpawn.transform); Instantiate(bullet, Vector3.zero, Quaternion.identity); Instantiate(bullet, new Vector3(0, 0, 10), bullet.transform.rotation); newobj = Instantiate(objTemplate) as ObjType; //from pregab - prefab must be in Resources folder newobj1 = Instantiate(Resources.Load("enemy")); // Instantiate the projectile at the position and rotation of this transform Rigidbody projectile; Rigidbody clone; clone = Instantiate(projectile, transform.position, transform.rotation); enemyOrc = Instantiate(Orc) as Enemy; /* Destroy a GameObject */ Destroy(gameObject); /* Finding GameObjects */ GameObject myObj = GameObject.Find("NAME IN HIERARCHY"); GameObject myObj = GameObject.FindWithTag("TAG"); childObject=parentObject.GetChild("child_name"); parentObject.GetChild("child_name").GetComponent<SpriteRenderer>().sprite = image; /* Accessing Components */ Example myComponent = GetComponent<Example>(); AudioSource audioSource = GetComponent<AudioSource>(); Rigidbody rgbd = GetComponent<Rigidbody>(); GetComponent<SpriteRenderer>().sprite = image; //set image in child component GetComponent<Text>().text = '123' //set text /* Transforms - can be accessed using the `transform` attribute */ Vector3 objectPosition = gameObject.transform.position; gameObject.transform.position = new Vector3(posX, posY, posZ); transform.Translate(Vector3.up * Time.deltaTime, Space.World); transform.Rotate(Vector3.up * Time.deltaTime, Space.World); /* Activate - can hide or how an element from the scene*/ myObject.SetActive(false); // hide myObject.SetActive(true); // show GetComponent<BoxCollider>().SetActive(false); // hide component
Vector Quick Reference
X = Left/Right Y = Up/Down Z = Forward/Back
Vector3.right /* (1, 0, 0) */ Vector2.right /* (1, 0) */ Vector3.left /* (-1, 0, 0) */ Vector2.left /* (-1, 0) */ Vector3.up /* (0, 1, 0) */ Vector2.up /* (0, 1) */ Vector3.down /* (0, -1, 0) */ Vector2.down /* (0, -1) */ Vector3.forward /* (0, 0, 1) */ Vector3.back /* (0, 0, -1) */ Vector3.zero /* (0, 0, 0) */ Vector2.zero /* (0, 0) */ Vector3.one /* (1, 1, 1) */ Vector2.one /* (1, 1) */ float length = myVector.magnitude /* Length of this Vector */ myVector.normalized /* Keeps direction, but reduces length to 1 */
Time Variables
/* The time in seconds since the start of the game */ float timeSinceStartOfGame = Time.time; /* The scale at which the time is passing */ float currentTimeScale = Time.timeScale; /* Pause time */ Time.timeScale = 0; /* The time in seconds it took to complete the last frame */ /* Use with Update() and LateUpdate() */ float timePassedSinceLastFrame = Time.deltaTime; /* The interval in seconds at which physics and fixed frame rate updates are performed */ /* Use with FixedUpdate() */ float physicsInterval = Time.fixedDeltaTime;
Random values
Random.Range(-10.0f, 10.0f) Random.Range(0, 8);
Physics Events
/* Both objects have to have a Collider and one object has to have a Rigidbody for these Events to work */ private void OnCollisionEnter(Collision hit) { Debug.Log(gameObject.name + " just hit " + hit.gameObject.name); } private void OnCollisionStay(Collision hit) { Debug.Log(gameObject.name + " is hitting " + hit.gameObject.name); } private void OnCollisionExit(Collision hit) { Debug.Log(gameObject.name + " stopped hitting " + hit.gameObject.name); } /* Trigger must be checked on one of the Colliders */ private void OnTriggerEnter(Collider hit) { Debug.Log(gameObject.name + " just hit " + hit.name); } private void OnTriggerStay(Collider hit) { Debug.Log(gameObject.name + " is hitting " + hit.name); } private void OnTriggerExit(Collider hit) { Debug.Log(gameObject.name + " stopped hitting " + hit.name); } /* For 2D Colliders just add 2D to the Method name and the Parameter Type */ private void OnCollisionEnter2D(Collision2D hit) { } private void OnCollisionStay2D(Collision2D hit) { } private void OnCollisionExit2D(Collision2D hit) { } private void OnTriggerEnter2D(Collider2D hit) { } private void OnTriggerStay2D(Collider2D hit) { } private void OnTriggerExit2D(Collider2D hit) { }
Coroutines
/* Create a Coroutine */ private IEnumerator CountSeconds(int count = 10) { for (int i = 0; i <= count; i++) { Debug.Log(i + " second(s) have passed"); yield return new WaitForSeconds(1.0f); } } /* Call a Coroutine */ StartCoroutine(CountSeconds()); StartCoroutine(CountSeconds(10)); /* Call a Coroutine that may need to be stopped */ StartCoroutine("CountSeconds"); StartCoroutine("CountSeconds", 10); /* Stop a Coroutine */ StopCoroutine("CountSeconds"); StopAllCoroutines(); /* Store and call a Coroutine from a variable */ private IEnumerator countSecondsCoroutine; countSecondsCoroutine = CountSeconds(); StartCoroutine(countSecondsCoroutine); /* Stop a stored Coroutine */ StopCoroutine(countSecondsCoroutine); /* Coroutine Return Types */ yield return null; // Waits until the next Update() call yield return new WaitForFixedUpdate(); // Waits until the next FixedUpdate() call yield return new WaitForEndOfFrame(); // Waits until everything this frame has executed yield return new WaitForSeconds(float seconds); // Waits for game time in seconds yield return new WaitUntil(() => MY_CONDITION); // Waits until a custom condition is met yield return new WWW("MY/WEB/REQUEST"); // Waits for a web request yield return StartCoroutine("MY_COROUTINE"); // Waits until another Coroutine is completed
Input Quick Reference
if (Input.GetKeyDown(KeyCode.Space)) { Debug.Log("Space key was Pressed"); } if (Input.GetKeyUp(KeyCode.W)) { Debug.Log("W key was Released"); } if (Input.GetKey(KeyCode.UpArrow)) { Debug.Log("Up Arrow key is being held down"); } /* Button Input located under Edit >> Project Settings >> Input */ if (Input.GetButtonDown("ButtonName")) { Debug.Log("Button was pressed"); } if (Input.GetButtonUp("ButtonName")) { Debug.Log("Button was released"); } if (Input.GetButton("ButtonName")) { Debug.Log("Button is being held down"); } /* special input events */ // OnMouseDown is called when the user has pressed the mouse button while over the Collider. // This event is sent to all scripts of the GameObject with Collider or GUI Element. Scripts of the parent or child objects do not receive this event. // This function is not called on objects that belong to Ignore Raycast layer. // This function is called on Colliders marked as Trigger if and only if Physics.queriesHitTriggers is true. void OnMouseDown() { // Destroy the gameObject after clicking on it Destroy(gameObject); }
Debug
Debug.Log(transform.position); Debug.Log("text");
Realizarea unui joc 2D de memorare