Last active
June 26, 2017 20:23
-
-
Save jahe/0db970450eadade0fcddeafb287d9564 to your computer and use it in GitHub Desktop.
Unity Cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ------- C# ------- | |
| // Generator functions | |
| // | |
| // Has to be a return value of IEnumerable, IEnumerable<T>, IEnumerator or IEnumerator<T> | |
| // in C# a Generator function is inferred by looking at the function body (looking for yield statements) | |
| // IEnumerable is an item that can be iterated on | |
| public IEnumerable<int> GetValues(int count) { | |
| for (int i = 0; i < count; ++i) { | |
| yield return i; | |
| } | |
| } | |
| // Enumerables in C# are classes that declare a method named "GetEnumerator" | |
| // that in turn returns an IEnumerator<T>. "Enumerator" is the C# name for "iterator". | |
| // | |
| // It is possible to iterate multiple times on the same instance returned by our GetValues method. | |
| // Every time GetEnumerator is called, the function starts from scratch. | |
| // | |
| // Important members of the IEnumerator Interface: | |
| // * Current | |
| // * MoveNext | |
| // | |
| // When MoveNext is called, the function is executed up to the point of the next "yield return" | |
| // (or the end of the method, if it there aren't any more "yield return" statements). | |
| // MoveNext returns true if the generator reached a "yield return" statement, false if it reached the end. | |
| // The value returned by the "yield return" statement can be read by the "Current" property. | |
| // | |
| // So let's review the contract associated with yield in C#: | |
| // 1. The caller calls a method that returns an IEnumerable | |
| // 2. The caller calls enumerable.GetEnumerator() | |
| // 3. The caller calls enumerator.MoveNext() followed by enumerator.Current as many times as it wants to. | |
| // Async Functions | |
| public async Task<int> GetWordCount(string url) { | |
| var page = await DownloadDocumentAsync(url); | |
| var words = page.Words; | |
| return words.Count(); | |
| } | |
| // Property with private field | |
| private double _foo; | |
| public double Foo { | |
| get { return _foo; } | |
| set { _foo = value; } | |
| } | |
| // Property without field | |
| public double Foo { get; set; } | |
| // Property with private setter | |
| public double Foo { get; private set; } | |
| // Get Property | |
| MyClass.Foo | |
| // ------- Unity ------- | |
| // Get Object Tag of colliding element | |
| Collider2D.rigidbody2D.tag | |
| // Get a property (HP) of an objects nested script (Enemy) | |
| Rigidbody2D.gameObject.GetComponent<Enemy>().HP | |
| // Destroy an object | |
| Destroy(gameObject); | |
| // Play Sound Effect on a specific position | |
| AudioSource.PlayClipAtPoint(AudioClip, position); | |
| // Invoke a function within a script every 3 seconds after a first delay of 1 second | |
| public class MyScript : MonoBehaviour { | |
| void Start() { | |
| InvokeRepeating("foo", 1, 3); | |
| } | |
| void foo() { | |
| // do stuff repeatedly | |
| } | |
| } | |
| // Instantiate a prefab by name (has to be placed in a directory called "resources" within the project) | |
| GameObject gameObject = Instantiate(Resources.Load("enemy")); | |
| // Move a GameObject from position to another with a constant speed | |
| // In the Update() method | |
| transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed); | |
| // Check weather a GameObject has reached the target position | |
| // In the Update() method | |
| if (Vector3.Distance(transform.position, target.transform.position) <= 0) { | |
| // do something | |
| } | |
| // Update() vs FixedUpdate() | |
| // | |
| // Update() | |
| // * is called every frame without any specific time between the calls | |
| // * Usecases: Moving Non-Physics objects, Receiving Input, Simple Timers | |
| // | |
| // FixedUpdate() | |
| // * is called in a fixed interval | |
| // * after FixedUpdate() is called any physics calculations are being made | |
| // * Usecase: Any updates to a rigidbody | |
| // Step-By-Step Guide - How to reverse engineer an .apk Unity game | |
| // 1. > unzip game.apk -d ./extracted-apk | |
| // 2. Download ILSpy (http://ilspy.net/) - Windows based .NET Decompiler | |
| // 3. Start ILSpy | |
| // 4. Go to "View > Options > Decompiler" | |
| // and uncheck "Decompile enumerators (yield return)" to see the content of C# generator functions | |
| // 5. Go to "File > Open" and open "extracted-apk/assets/bin/Data/Managed/Assembly-CSharp.dll" | |
| // 6. Happy source code reading! | |
| // Step-By-Step Guide - How to reverse engineer assets from an .apk Unity game | |
| // 1. > unzip game.apk -d ./extracted-apk | |
| // 2. Download "Unity Studio" v0.7 from "https://github.com/Perfare/UnityStudio/releases" | |
| // 3. Launch "Unity Studio.exe" | |
| // 4. Go to "File > Load folder..." and select "extracted-apk/assets" | |
| // 5. Happy inspecting scenes and assets! | |
| // Setting up references in the Awake function | |
| public class PlayerControl : MonoBehaviour { | |
| private Transform groundCheck; | |
| private Animator anim; | |
| void Awake() { | |
| groundCheck = transform.Find("groundCheck"); | |
| anim = GetComponent<Animator>(); | |
| } | |
| } | |
| // Get a reference to a child by name | |
| Transfrom child = transform.Find("nameOfTheGameObject"); | |
| // Get a Component from the current GameObject | |
| Animator animator = GetComponent<Animator>(); | |
| // React on Jump Keypress | |
| void Update() { | |
| if (Input.GetButtonDown("Jump") && grounded) { | |
| jump = true; | |
| } | |
| } | |
| // Parameterize a Script with a list of GameObject's Sprites (here Transform) | |
| public Transform[] backgrounds; | |
| // Access the Main Cameras position | |
| Transform cam = Camera.main.transform; | |
| // Set an Animator float parameter via code | |
| animator.setFloat("Speed", 2.0); | |
| // Trigger an Animator boolean parameter via code - It resets itself on the next frame so they can be called again (perfect for actions like shooting) | |
| animator.SetTrigger("Jump"); | |
| // Add a vertical force to the character to make him jump | |
| rigidbody2D.AddForce(new Vector2(0f, 1000)); | |
| // Implement a Jump Action for a character | |
| void Update() { | |
| if (Input.GetButtonDown("Jump") && grounded) { | |
| jump = true; | |
| } | |
| } | |
| void FixedUpdate() { | |
| if (jump) { | |
| rigidbody2D.AddForce(new Vector2(0f, jumpForce)); | |
| jump = false; | |
| } | |
| } | |
| // Get the current input for the horizontal direction from -1.0 to 1.0 (e.g. User presses left arrow key on the keyboard: It retrieves -1.0, it supports joysticks as well by having this range) | |
| float horizontalInput = Input.getAxis("Horizontal"); | |
| // Switch the horizontal facing direction of a GameObject | |
| Vector3 currentScale = transform.localScale; | |
| currentScale.x *= -1; | |
| transform.localScale = currentScale; | |
| // Check if a character is grounded | |
| // 1. Add a Layer called "Ground" | |
| // 2. Apply it to all walkable foreground surfaces (GameObjects) | |
| // 3. Check wheather there is something with a "Ground" layer below the character's feet: | |
| boolean grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")); | |
| // The groundCheck is an Gizmo that represents the point at which to check for the ground | |
| // Gizmo - Gizmos are used to give visual debugging or setup aids in the scene view | |
| // Get a Component of the root element in the hierarchy | |
| PlayerControl playerControl = transform.root.GetComponent<PlayerControl>(); | |
| // Play the current Audio Source component attached to the GameObject | |
| audio.Play(); | |
| // Instantiate a Prefab and give it a direction of right and a velocity | |
| public RigidBody2D rocket; | |
| public float speed = 20f; | |
| void Update() { | |
| Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D; | |
| bulletInstance.velocity = new Vector2(speed, 0) | |
| } | |
| // Instantiate a Prfab and give it a direction o left and a velocity | |
| public RigidBody2D rocket; | |
| public float speed = 20f; | |
| void Update() { | |
| Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D; | |
| bulletInstance.velocity = new Vector2(-speed, 0) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment