Unity cheat sheet
Or just “refresh my memory sheet”
Monobehaviour
Always a component of a gameobject.
Comfusingly these event methods aren’t override, they’re just kinda there.
Awake: When loading the script into memory for the first time, regardless if the script is enabled or not.Start: When a script is enabled for the first time.Update: Once per frame.LateUpdate: Once per frame after all theUpdates have been called.FixedUpdate: At 50hz.
To make a field writable from the editor (allows “dependency injection”) make a field in the c# class and annotate it with [SerializeField].
“New input system”
The old one was apparently hot garbage. The new one is an (enabled-by-default) package. Everything is under using UnityEngine.InputSystem.
InputActionAsset. One always seems to be pre-created. Can edit with the nice gui. One asset can be set as the project-wide input actions, which makes it globally accessible from code at InputSystem.actions.
They contain action maps, each action map contains InputActions.
- finding a (project-wide) action:
InputAction blah = InputSystem.actions.FindAction("name") - enabling an action:
blah.Enable()- all the project-wide ones are enabled by default
- finding a vector2 input:
blah.ReadValue<Vector2> - finding whether a button is pressed this frame:
blah.IsPressed() - button press callbacks:
blah.performed += SomeFunction- where
SomeFunctiontakes anInputAction.CallbackContext
- where
making a wasd action:
- make one called Move
- set Control Type to “Vector 2”
- right click Move and hit Add Up/Down/Left/Right Composite
- bind those to w, s, a, d
making a look action:
- set Control Type to “Vector 2”
- add a binding
- in “path” click Mouse -> Delta