├── cheatsheets ├── unity-cheatsheet.jpg ├── unity-cheatsheet.pdf ├── unity-cheatsheet2.jpg ├── unity-cheatsheet2.pdf ├── unity3d-keyboard-shortcuts.jpg └── unity3d-keyboard-shortcuts.pdf ├── examples ├── SceneManagementExamples.cs ├── UIExamples.cs ├── FindExamples.cs ├── InputExamples.cs ├── EnableSetActiveExamples.cs ├── AudioExamples.cs ├── CollisionExamples.cs ├── InstantiateExamples.cs └── IEnumeratorExamples.cs └── Readme.MD /cheatsheets/unity-cheatsheet.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codemaker2015/unity3d-cheat-sheet/HEAD/cheatsheets/unity-cheatsheet.jpg -------------------------------------------------------------------------------- /cheatsheets/unity-cheatsheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codemaker2015/unity3d-cheat-sheet/HEAD/cheatsheets/unity-cheatsheet.pdf -------------------------------------------------------------------------------- /cheatsheets/unity-cheatsheet2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codemaker2015/unity3d-cheat-sheet/HEAD/cheatsheets/unity-cheatsheet2.jpg -------------------------------------------------------------------------------- /cheatsheets/unity-cheatsheet2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codemaker2015/unity3d-cheat-sheet/HEAD/cheatsheets/unity-cheatsheet2.pdf -------------------------------------------------------------------------------- /cheatsheets/unity3d-keyboard-shortcuts.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codemaker2015/unity3d-cheat-sheet/HEAD/cheatsheets/unity3d-keyboard-shortcuts.jpg -------------------------------------------------------------------------------- /cheatsheets/unity3d-keyboard-shortcuts.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codemaker2015/unity3d-cheat-sheet/HEAD/cheatsheets/unity3d-keyboard-shortcuts.pdf -------------------------------------------------------------------------------- /examples/SceneManagementExamples.cs: -------------------------------------------------------------------------------- 1 | // Various examples of scene management 2 | using UnityEngine; 3 | using System.Collections; 4 | using UnityEngine.SceneManagement; 5 | 6 | public class SceneManagementExamples : MonoBehaviour { 7 | 8 | // Name of new scene. Should be add the scene in build settings. 9 | public string scene; 10 | 11 | // Load the new scene 12 | public void LoadScene(string newScene) { 13 | SceneManager.LoadScene(newScene); 14 | } 15 | 16 | // Reload the current scene 17 | public void ReloadScene() { 18 | SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); 19 | } 20 | } -------------------------------------------------------------------------------- /examples/UIExamples.cs: -------------------------------------------------------------------------------- 1 | // Various UI examples 2 | using UnityEngine; 3 | using System.Collections; 4 | using UnityEngine.UI; 5 | 6 | public class UIExamples : MonoBehaviour { 7 | 8 | // Set the target UI Text in the Inspector 9 | public Text uiText; 10 | // Set the target UI image in Inspector. UI Image must be "filled" type 11 | public Image uiImage; 12 | private int uiNumber = 5; 13 | 14 | void Update() { 15 | 16 | if (Input.GetButtonDown("Jump")) { 17 | // Basic usage 18 | uiText.text = "CODEMAKER"; 19 | // Fill amount is in a range from 0-1. Empty 20 | uiImage.fillAmount = 0; 21 | } else if (Input.GetButtonDown("Fire1")) { 22 | // Numbers must be converted to strings 23 | uiText.text = uiNumber.ToString(); 24 | 25 | // Larger ranges of number can be converted by dividing with the max value 26 | uiImage.fillAmount = 2.5f/uiNumber; 27 | } else if (Input.GetButtonDown("Fire2")) { 28 | // Numbers can be formatted to display a certain number of places 29 | uiText.text = uiNumber.ToString("000"); 30 | // Full 31 | uiImage.fillAmount = 1; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /examples/FindExamples.cs: -------------------------------------------------------------------------------- 1 | // Various ways of finding things in Unity 2 | 3 | using UnityEngine; 4 | using System.Collections; 5 | 6 | public class FindExamples : MonoBehaviour { 7 | 8 | // Example needs a Rigidbody2D component to work 9 | private Rigidbody2D rigidbody2D, otherrigidbody2D, childrigidbody2D; 10 | private GameObject hierarchyObject, childObject, taggedObject; 11 | 12 | void Start() { 13 | 14 | // Find a component attached to this GameObject 15 | rigidbody2D = GetComponent(); 16 | 17 | // Find a GameObject in the Hierarchy, will check all GameObjects in the Hierarchy 18 | hierarchyObject = GameObject.Find("Name Of Object"); 19 | 20 | // Find a GameObject in the hierarchy based on tag 21 | taggedObject = GameObject.FindWithTag("Player"); 22 | 23 | // Can be combined to find a component on a GameObject in the Hierarchy 24 | otherrigidbody2D = GameObject.FindWithTag("Player").GetComponent(); 25 | 26 | // Lowercase transform.Find can be used to search child GameObjects by name 27 | childObject = transform.Find("Name Of Object").gameObject; 28 | 29 | // Can also be combined to find a component on a GameObject in the Hierarchy 30 | childrigidbody2D = transform.Find("Name Of Object").GetComponent(); 31 | } 32 | } -------------------------------------------------------------------------------- /examples/InputExamples.cs: -------------------------------------------------------------------------------- 1 | // Various examples of Input usage in Unity 2 | using UnityEngine; 3 | using System.Collections; 4 | 5 | public class InputExamples : MonoBehaviour { 6 | 7 | // These strings need to be set in the Inspector to match Input Manager entries 8 | public string horiAxis, vertAxis, jump; 9 | public KeyCode key1; 10 | public Vector2 speed = new Vector2(10f, 5f); 11 | 12 | void Update() { 13 | 14 | // Input.GetAxis will return a number between -1 and 1, with smoothing applied 15 | // (adjust Sensitivity in Input Manager) 16 | Debug.Log("Horizontal: " + Input.GetAxis(horiAxis)); 17 | 18 | // Input.GetAxisRaw will return a number between -1 and 1, without Sensitivity smoothing applied 19 | Debug.Log("Vertical: " + Input.GetAxisRaw(vertAxis)); 20 | 21 | // This is often multiplied by a number to create movement 22 | Debug.Log("Horizontal Modified: " + Input.GetAxis(horiAxis) * speed.x); 23 | 24 | // Key pressed down 25 | if (Input.GetKeyDown(KeyCode.V)) { 26 | Debug.Log("Key V pressed"); 27 | } 28 | 29 | // KeyCode can also be set in the Inspector as a variable 30 | if (Input.GetKeyUp(key1)) { 31 | Debug.Log("Key Released"); 32 | } 33 | 34 | // Run only once when button is pressed 35 | if (Input.GetButton(jump)) { 36 | Debug.Log("Jump"); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /examples/EnableSetActiveExamples.cs: -------------------------------------------------------------------------------- 1 | // Various ways of enabling/disabling a gameObject's components and activating/deactivating a gameObject 2 | using UnityEngine; 3 | using System.Collections; 4 | 5 | public class EnableSetActiveExamples : MonoBehaviour { 6 | 7 | public GameObject targetGameObject; 8 | private Collider2D collider2D; 9 | 10 | void Start() { 11 | 12 | // SetActive can switch a gameObject on or off in the Hierarchy. Once deactivated, its components will no longer run until reactivated. 13 | targetGameObject.SetActive(false); 14 | 15 | // Get a collider2D component attached to this gameObject. Note: Collider2D will work with any kind of 2D collider component. 16 | collider2D = GetComponent(); 17 | 18 | // Disable or enable a component using a bool 19 | collider2D.enabled = false; 20 | } 21 | 22 | // Update is called once per frame 23 | void Update() { 24 | 25 | // Jump is space in Input Manager 26 | if (Input.GetButtonDown("Jump")) { 27 | 28 | // Check if a gameObject is active in the scene with activeInHierarchy 29 | if (!targetGameObject.activeInHierarchy) { 30 | targetGameObject.SetActive(true); 31 | } 32 | } 33 | 34 | // Fire is left ctrl in Input Manager 35 | if (Input.GetButtonDown("Fire")) { 36 | 37 | // Check if a component is enabled 38 | if (!collider2D.enabled) { 39 | collider2D.enabled = true; 40 | } 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /examples/AudioExamples.cs: -------------------------------------------------------------------------------- 1 | // Various audio examples for Unity 2 | using UnityEngine; 3 | using System.Collections; 4 | 5 | public class AudioExamples : MonoBehaviour { 6 | 7 | // Attach an AudioSource component to this gameObject 8 | private AudioSource audioSource; 9 | 10 | // Set an audioclip in the Inspector 11 | public AudioClip clip1; 12 | 13 | void Start() { 14 | 15 | // Get the AudioSource component 16 | audioSource = GetComponent(); 17 | // Plays the AudioClip set in the AudioSource component 18 | audioSource.Play(); 19 | } 20 | 21 | void Update() { 22 | 23 | // AudioSource.Play() can also be paused or stopped 24 | // Check if audioSource is playing a clip 25 | if (audioSource.isPlaying) { 26 | if (Input.GetButtonDown("P")) 27 | audioSource.Pause(); 28 | else if (Input.GetButtonDown("S")) 29 | audioSource.Stop(); 30 | 31 | // Set the pitch and volume of the clips played by Audio Source. Volume range is 0~1 32 | audioSource.pitch = Random.Range(0.25f, 2f); 33 | audioSource.volume = Random.Range(0.25f, 1f); 34 | } 35 | 36 | // PlayOneShot can be used to play a short clip 37 | // Can't be used with Pause & Stop 38 | if (Input.GetButtonDown("Play")) { 39 | audioSource.PlayOneShot(clip1); 40 | 41 | // You can give this an optional volume setting as well (0-1 range) 42 | //audioSource.PlayOneShot(clip1, 0.5f); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /examples/CollisionExamples.cs: -------------------------------------------------------------------------------- 1 | // Various collision examples 2 | using UnityEngine; 3 | using System.Collections; 4 | 5 | public class CollisionExamples : MonoBehaviour { 6 | 7 | // Collisions/Triggers require a collider on both gameObjects and a rigidbody on at least one. 8 | void OnCollisionEnter(Collision other) { 9 | 10 | // Do something when another collider touches this gameObject's collider 11 | Debug.Log("Collided with something"); 12 | 13 | // Conditional statements can be used to filter collisions/triggers 14 | // Checking for a known tag is one option 15 | if (other.gameObject.CompareTag("tag1")) { 16 | Debug.Log("tag1 collision"); 17 | } 18 | 19 | // Checking for a known name is one option 20 | else if (other.gameObject.name.Equals("name1")) { 21 | Debug.Log("name1 collision"); 22 | } 23 | } 24 | 25 | // Is Trigger needs to be selected on one of the colliders 26 | void OnTriggerEnter(Collider other) { 27 | // Do something if another collider overlaps this gameObject's collider 28 | Debug.Log("Triggered by something"); 29 | } 30 | 31 | // Collision and Trigger also have stay event 32 | void OnTriggerStay(Collider other) { 33 | // Do something while a collider is still overlapping with this gameObject's collider 34 | Debug.Log("Still triggering"); 35 | } 36 | 37 | // Collision and Trigger also have exit event 38 | void OnCollisionExit(Collision other) { 39 | // Do something after a collider is no longer touching this gameObject's collider 40 | Debug.Log("Collision ended"); 41 | } 42 | } -------------------------------------------------------------------------------- /examples/InstantiateExamples.cs: -------------------------------------------------------------------------------- 1 | // Various Instantiate examples for Unity 2 | using UnityEngine; 3 | using System.Collections; 4 | 5 | public class InstantiateExamples : MonoBehaviour { 6 | 7 | // Set the object to be cloned in the Inspector. 8 | public GameObject prefab; 9 | 10 | // Set a target transform in the Inspector to clone prefab from 11 | public Transform spawnPoint; 12 | 13 | // Update is called once per frame 14 | void Update() { 15 | 16 | // Basic cloning 17 | if (Input.GetButton("X")) { 18 | 19 | // Pass the prefab as an argument and clone it at the spawnPoint. 20 | // spawnPoint can be set to transform for cloning the prefab at the position of this object. 21 | Instantiate(prefab, spawnPoint); 22 | //Instantiate(prefab, transform); 23 | } 24 | 25 | // Advanced cloning 26 | if (Input.GetButtonDown("Fire")) { 27 | 28 | // Overloaded method which can be positioned and rotated. 29 | GameObject prefab1 = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject; 30 | 31 | // Make this prefab a child of the gameObject that spawned it 32 | prefab1.transform.parent = transform; 33 | 34 | // Destroying the prefab after a set amount of time 35 | Destroy(prefab1, 3f); 36 | 37 | // Accessing the cloned prefab's components. Note: The prefab needs a Rigidbody2D component for the next 2 lines to work. 38 | Rigidbody2D prefabRB2D = prefab1.GetComponent(); 39 | prefabRB2D.AddForce(Vector2.up * 100f); 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /examples/IEnumeratorExamples.cs: -------------------------------------------------------------------------------- 1 | // Various IEnumerator timer examples for Unity 2 | using UnityEngine; 3 | using System.Collections; 4 | 5 | public class IEnumeratorExamples : MonoBehaviour { 6 | // Flag for checking if a coroutine is running 7 | private bool alreadyDelayed; 8 | 9 | // Necessary to stop a coroutine 10 | private IEnumerator coroutine; 11 | 12 | void Start() { 13 | // Coroutines run in Start are only called once. No if statement + bool needed. 14 | StartCoroutine(LoopingTimer(7f)); 15 | 16 | // Set to an IEnumerator 17 | coroutine = LoopingTimer(1f); 18 | StartCoroutine(coroutine); 19 | } 20 | 21 | void Update() { 22 | 23 | // DelayTimerOneShot 24 | if (Input.GetButtonDown("PlayOneShot")) 25 | StartCoroutine(DelayTimerOneShot(1f)); 26 | 27 | // Space bar is Jump in Input Manager 28 | if (Input.GetButtonDown("Jump")) 29 | // This if statement ensures that a coroutine can't be run again if it is already running. 30 | if (!alreadyDelayed) 31 | StartCoroutine(DelayTimerLatching(3f)); 32 | 33 | if (Input.GetButtonDown("Fire")) { 34 | // To stop a coroutine 35 | StopCoroutine(coroutine); 36 | Debug.Log("Stopped at " + Time.time); 37 | } 38 | } 39 | 40 | // Wait for an amount of time before doing something 41 | private IEnumerator DelayTimerOneShot(float delayLength) { 42 | yield return new WaitForSeconds(delayLength); 43 | Debug.Log("Delayed One Shot"); 44 | } 45 | 46 | // Wait for an amount of time before doing something 47 | private IEnumerator DelayTimerLatching(float delayLength) { 48 | 49 | // Set the already delayed flag to signal that this coroutine is already running 50 | alreadyDelayed = true; 51 | Debug.Log("Delayed Latch"); 52 | yield return new WaitForSeconds(delayLength); 53 | Debug.Log("Delayed Latch Released"); 54 | // Reset the already delayed flag so that this coroutine can be used once again. 55 | alreadyDelayed = false; 56 | } 57 | } -------------------------------------------------------------------------------- /Readme.MD: -------------------------------------------------------------------------------- 1 | # Unity 3D C# Cheat Sheet 2 | 3 | ## Cheatsheet 4 | 5 | ### Unity 3D Scripting 6 | ![cheatsheet](https://github.com/codemaker2015/unity3d-cheat-sheet/blob/master/cheatsheets/unity-cheatsheet.jpg) 7 | [Download](https://github.com/codemaker2015/unity3d-cheat-sheet/blob/master/cheatsheets/unity-cheatsheet.pdf) 8 | 9 | ![cheatsheet](https://github.com/codemaker2015/unity3d-cheat-sheet/blob/master/cheatsheets/unity-cheatsheet2.jpg) 10 | [Download](https://github.com/codemaker2015/unity3d-cheat-sheet/blob/master/cheatsheets/unity-cheatsheet2.pdf) 11 | 12 | 13 | ### Unity 3D Keyboard Shortcuts 14 | ![cheatsheet](https://github.com/codemaker2015/unity3d-cheat-sheet/blob/master/cheatsheets/unity3d-keyboard-shortcuts.jpg) 15 | [Download](https://github.com/codemaker2015/unity3d-cheat-sheet/blob/master/cheatsheets/unity3d-keyboard-shortcuts.pdf) 16 | 17 | ## Examples 18 | 19 | ### InputExamples.cs 20 | 21 | ```cs 22 | // Various examples of Input usage in Unity 23 | using UnityEngine; 24 | using System.Collections; 25 | 26 | public class InputExamples : MonoBehaviour { 27 | 28 | // These strings need to be set in the Inspector to match Input Manager entries 29 | public string horiAxis, vertAxis, jump; 30 | public KeyCode key1; 31 | public Vector2 speed = new Vector2(10f, 5f); 32 | 33 | void Update() { 34 | 35 | // Input.GetAxis will return a number between -1 and 1, with smoothing applied 36 | // (adjust Sensitivity in Input Manager) 37 | Debug.Log("Horizontal: " + Input.GetAxis(horiAxis)); 38 | 39 | // Input.GetAxisRaw will return a number between -1 and 1, without Sensitivity smoothing applied 40 | Debug.Log("Vertical: " + Input.GetAxisRaw(vertAxis)); 41 | 42 | // This is often multiplied by a number to create movement 43 | Debug.Log("Horizontal Modified: " + Input.GetAxis(horiAxis) * speed.x); 44 | 45 | // Key pressed down 46 | if (Input.GetKeyDown(KeyCode.T)) { 47 | Debug.Log("Key T pressed"); 48 | } 49 | 50 | // KeyCode can also be set in the Inspector as a variable 51 | if (Input.GetKeyUp(key1)) { 52 | Debug.Log("Key Released"); 53 | } 54 | 55 | // Run only once when button is pressed 56 | if (Input.GetButtonDown(jump)) { 57 | Debug.Log("Jump"); 58 | } 59 | } 60 | } 61 | ``` 62 | 63 | ### InstantiateExamples.cs 64 | 65 | ```cs 66 | // Various Instantiate examples for Unity 67 | using UnityEngine; 68 | using System.Collections; 69 | 70 | public class InstantiateExamples : MonoBehaviour { 71 | 72 | // Set the object to be cloned in the Inspector. 73 | public GameObject prefab; 74 | 75 | // Set a target transform in the Inspector to clone prefab from 76 | public Transform spawnPoint; 77 | 78 | // Update is called once per frame 79 | void Update() { 80 | 81 | // Basic cloning 82 | if (Input.GetButton("X")) { 83 | 84 | // Pass the prefab as an argument and clone it at the spawnPoint 85 | // spawnPoint can be set to transform for cloning the prefab at the position of this object 86 | Instantiate(prefab, spawnPoint); 87 | //Instantiate(prefab, transform); 88 | } 89 | 90 | // Advanced cloning 91 | if (Input.GetButtonDown("Fire")) { 92 | 93 | // Overloaded method which can be positioned and rotated 94 | GameObject prefab1 = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject; 95 | 96 | // Make this prefab a child of the gameObject that spawned it 97 | prefab1.transform.parent = transform; 98 | 99 | // Destroying the prefab after a set amount of time 100 | Destroy(prefab1, 3f); 101 | 102 | // Accessing the cloned prefab's components. Note: The prefab needs a Rigidbody component for the next 2 lines to work 103 | Rigidbody prefabrigidbody = prefab1.GetComponent(); 104 | prefabrigidbody.AddForce(Vector2.up * 100f); 105 | } 106 | } 107 | } 108 | ``` 109 | 110 | ### FindExamples.cs 111 | 112 | ```cs 113 | // Various ways of finding things in Unity 114 | 115 | using UnityEngine; 116 | using System.Collections; 117 | 118 | public class FindExamples : MonoBehaviour { 119 | 120 | // Example needs a Rigidbody component to work 121 | private Rigidbody rigidbody, otherrigidbody, childrigidbody; 122 | private GameObject hierarchyObject, childObject, taggedObject; 123 | 124 | void Start() { 125 | 126 | // Find a component attached to this GameObject 127 | rigidbody = GetComponent(); 128 | 129 | // Find a GameObject in the Hierarchy, will check all GameObjects in the Hierarchy 130 | hierarchyObject = GameObject.Find("Name Of Object"); 131 | 132 | // Find a GameObject in the hierarchy based on tag 133 | taggedObject = GameObject.FindWithTag("Player"); 134 | 135 | // Can be combined to find a component on a GameObject in the Hierarchy 136 | otherrigidbody = GameObject.FindWithTag("Player").GetComponent(); 137 | 138 | // Lowercase transform.Find can be used to search child GameObjects by name 139 | childObject = transform.Find("Name Of Object").gameObject; 140 | 141 | // Can also be combined to find a component on a GameObject in the Hierarchy 142 | childrigidbody = transform.Find("Name Of Object").GetComponent(); 143 | } 144 | } 145 | ``` 146 | 147 | ### EnableSetActiveExamples.cs 148 | 149 | ```cs 150 | // Various ways of enabling/disabling a gameObject's components and activating/deactivating a gameObject 151 | using UnityEngine; 152 | using System.Collections; 153 | 154 | public class EnableSetActiveExamples : MonoBehaviour { 155 | 156 | public GameObject targetGameObject; 157 | private Collider collider; 158 | 159 | void Start() { 160 | 161 | // SetActive can switch a gameObject on or off in the Hierarchy. Once deactivated, its components will no longer run until reactivated. 162 | targetGameObject.SetActive(false); 163 | 164 | // Get a collider component attached to this gameObject. Note: Collider will work with any kind of collider component. 165 | collider = GetComponent(); 166 | 167 | // Disable or enable a component using a bool 168 | collider.enabled = false; 169 | } 170 | 171 | // Update is called once per frame 172 | void Update() { 173 | 174 | // Jump is space in Input Manager 175 | if (Input.GetButtonDown("Jump")) { 176 | 177 | // Check if a gameObject is active in the scene with activeInHierarchy 178 | if (!targetGameObject.activeInHierarchy) { 179 | targetGameObject.SetActive(true); 180 | } 181 | } 182 | 183 | // Fire is left ctrl in Input Manager 184 | if (Input.GetButtonDown("Fire")) { 185 | 186 | // Check if a component is enabled 187 | if (!collider.enabled) { 188 | collider.enabled = true; 189 | } 190 | } 191 | } 192 | } 193 | ``` 194 | 195 | ### CollisionExamples.cs 196 | 197 | ```cs 198 | // Various collision examples 199 | using UnityEngine; 200 | using System.Collections; 201 | 202 | public class CollisionExamples : MonoBehaviour { 203 | 204 | // Collisions/Triggers require a collider on both gameObjects and a rigidbody on at least one. 205 | void OnCollisionEnter(Collision other) { 206 | 207 | // Do something when another collider touches this gameObject's collider 208 | Debug.Log("Collided with something"); 209 | 210 | // Conditional statements can be used to filter collisions/triggers 211 | // Checking for a known tag is one option 212 | if (other.gameObject.CompareTag("tag1")) { 213 | Debug.Log("tag1 collision"); 214 | } 215 | 216 | // Checking for a known name is one option 217 | else if (other.gameObject.name.Equals("name1")) { 218 | Debug.Log("name1 collision"); 219 | } 220 | } 221 | 222 | // Is Trigger needs to be selected on one of the colliders 223 | void OnTriggerEnter(Collider other) { 224 | // Do something if another collider overlaps this gameObject's collider 225 | Debug.Log("Triggered by something"); 226 | } 227 | 228 | // Collision and Trigger also have stay event 229 | void OnTriggerStay(Collider other) { 230 | // Do something while a collider is still overlapping with this gameObject's collider 231 | Debug.Log("Still triggering"); 232 | } 233 | 234 | // Collision and Trigger also have exit event 235 | void OnCollisionExit(Collision other) { 236 | // Do something after a collider is no longer touching this gameObject's collider 237 | Debug.Log("Collision ended"); 238 | } 239 | } 240 | ``` 241 | 242 | ### AudioExamples.cs 243 | 244 | ```cs 245 | // Various audio examples for Unity 246 | using UnityEngine; 247 | using System.Collections; 248 | 249 | public class AudioExamples : MonoBehaviour { 250 | 251 | // Attach an AudioSource component to this gameObject 252 | private AudioSource audioSource; 253 | 254 | // Set an audioclip in the Inspector 255 | public AudioClip clip1; 256 | 257 | void Start() { 258 | 259 | // Get the AudioSource component 260 | audioSource = GetComponent(); 261 | // Plays the AudioClip set in the AudioSource component 262 | audioSource.Play(); 263 | } 264 | 265 | void Update() { 266 | 267 | // AudioSource.Play() can also be paused or stopped 268 | // Check if audioSource is playing a clip 269 | if (audioSource.isPlaying) { 270 | if (Input.GetButtonDown("P")) 271 | audioSource.Pause(); 272 | else if (Input.GetButtonDown("S")) 273 | audioSource.Stop(); 274 | 275 | // Set the pitch and volume of the clips played by Audio Source. Volume range is 0~1 276 | audioSource.pitch = Random.Range(0.25f, 2f); 277 | audioSource.volume = Random.Range(0.25f, 1f); 278 | } 279 | 280 | // PlayOneShot can be used to play a short clip 281 | // Can't be used with Pause & Stop 282 | if (Input.GetButtonDown("Play")) { 283 | audioSource.PlayOneShot(clip1); 284 | 285 | // You can give this an optional volume setting as well (0-1 range) 286 | //audioSource.PlayOneShot(clip1, 0.5f); 287 | } 288 | } 289 | } 290 | ``` 291 | 292 | ### IEnumeratorExamples.cs 293 | 294 | ```cs 295 | // Various IEnumerator timer examples for Unity 296 | using UnityEngine; 297 | using System.Collections; 298 | 299 | public class IEnumeratorExamples : MonoBehaviour { 300 | // Flag for checking if a coroutine is running 301 | private bool alreadyDelayed; 302 | 303 | // Necessary to stop a coroutine 304 | private IEnumerator coroutine; 305 | 306 | void Start() { 307 | // Coroutines run in Start are only called once. No if statement + bool needed. 308 | StartCoroutine(LoopingTimer(7f)); 309 | 310 | // Set to an IEnumerator 311 | coroutine = LoopingTimer(1f); 312 | StartCoroutine(coroutine); 313 | } 314 | 315 | void Update() { 316 | 317 | // DelayTimerOneShot 318 | if (Input.GetButtonDown("PlayOneShot")) 319 | StartCoroutine(DelayTimerOneShot(1f)); 320 | 321 | // Space bar is Jump in Input Manager 322 | if (Input.GetButtonDown("Jump")) 323 | // This if statement ensures that a coroutine can't be run again if it is already running. 324 | if (!alreadyDelayed) 325 | StartCoroutine(DelayTimerLatching(3f)); 326 | 327 | if (Input.GetButtonDown("Fire")) { 328 | // To stop a coroutine 329 | StopCoroutine(coroutine); 330 | Debug.Log("Stopped at " + Time.time); 331 | } 332 | } 333 | 334 | // Wait for an amount of time before doing something 335 | private IEnumerator DelayTimerOneShot(float delayLength) { 336 | yield return new WaitForSeconds(delayLength); 337 | Debug.Log("Delayed One Shot"); 338 | } 339 | 340 | // Wait for an amount of time before doing something 341 | private IEnumerator DelayTimerLatching(float delayLength) { 342 | 343 | // Set the already delayed flag to signal that this coroutine is already running 344 | alreadyDelayed = true; 345 | Debug.Log("Delayed Latch"); 346 | yield return new WaitForSeconds(delayLength); 347 | Debug.Log("Delayed Latch Released"); 348 | // Reset the already delayed flag so that this coroutine can be used once again. 349 | alreadyDelayed = false; 350 | } 351 | } 352 | ``` 353 | 354 | ### SceneManagementExamples.cs 355 | 356 | ```cs 357 | // Various examples of scene management 358 | using UnityEngine; 359 | using System.Collections; 360 | using UnityEngine.SceneManagement; 361 | 362 | public class SceneManagementExamples : MonoBehaviour { 363 | 364 | // Name of new scene. Should be add the scene in build settings. 365 | public string scene; 366 | 367 | // Load the new scene 368 | public void LoadScene(string newScene) { 369 | SceneManager.LoadScene(newScene); 370 | } 371 | 372 | // Reload the current scene 373 | public void ReloadScene() { 374 | SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); 375 | } 376 | } 377 | ``` 378 | 379 | ### UIExamples.cs 380 | 381 | ```cs 382 | // Various UI examples 383 | using UnityEngine; 384 | using System.Collections; 385 | using UnityEngine.UI; 386 | 387 | public class UIExamples : MonoBehaviour { 388 | 389 | // Set the target UI Text in the Inspector 390 | public Text uiText; 391 | // Set the target UI image in Inspector. UI Image must be "filled" type 392 | public Image uiImage; 393 | private int uiNumber = 5; 394 | 395 | void Update() { 396 | 397 | if (Input.GetButtonDown("Jump")) { 398 | // Basic usage 399 | uiText.text = "CODEMAKER"; 400 | // Fill amount is in a range from 0-1. Empty 401 | uiImage.fillAmount = 0; 402 | } else if (Input.GetButtonDown("Fire1")) { 403 | // Numbers must be converted to strings 404 | uiText.text = uiNumber.ToString(); 405 | 406 | // Larger ranges of number can be converted by dividing with the max value 407 | uiImage.fillAmount = 2.5f/uiNumber; 408 | } else if (Input.GetButtonDown("Fire2")) { 409 | // Numbers can be formatted to display a certain number of places 410 | uiText.text = uiNumber.ToString("000"); 411 | // Full 412 | uiImage.fillAmount = 1; 413 | } 414 | } 415 | } 416 | ``` --------------------------------------------------------------------------------