Tips & Tricks
Helpful tips and tricks for working in Unity.
- Snap GameObject to floor/surface/vertex
- Attributes
- Logs
- Shortcut to add/subtract or increment/decrement
- Save / Load object selection in hierarchy and project view
- Save content from play mode in edit mode
- Hierarchy - Expand/Collapse All
- Inspector - Expressions
- Inspector - Show auto-properties
- Physics - OnTrigger/OnCollision called on parent only
- TryGetComponent vs GetComponent
Snap GameObject to floor/surface/vertex
Select a GameObject with the Move Tool W
, then hold V
.
- Snap vertex to vertex - Drag the square over any vertex.
- Snap vertex to vertex (with rounded position numbers) - Drag the square over any vertex while holding
Ctrl
. - Snap vertex to surface - Drag the square over any surface while holding
Ctrl-Shift
.
Attributes
Attributes are placed above variables/functions. Here are some useful ones.
Header
Header allows the inspector to display a title/header above variables.
TextArea
Multiline
HideInInspector
Serializes a field, but will not show it in the inspector.
Range
SerializeField
Allows private fields to show in the inspector:
Including auto-generated backing fields for Properties:
ContextMenu
ContextMenu allows you to run a function by right-clicking on a Component in the inspector.
ContextMenuItem
ContextMenuItem allows you to run a function by right-clicking on a variable/field in the inspector.
MenuItem
MenuItem allows you to run a function from an editor dropdown menu or hotkey.
CreateAssetMenu
Creates a ScriptableObject asset on disk. It can store data and be used like a profile.
SelectionBase
Allows you to select this GameObject in the scene instead of a child.
Attribute Extensions
Logs
Here are some cool things you can do with Debug.Log and the Console.
Single-line console log entries with timestamps
You can reduce the size of console (Window > General > Console) entries by clicking the 3 dots in the top-right and choosing Log Entry > 1 line.
Show Timestamp is also useful.
Formatting
Use Rich Text for formatting.
Line break / New Line
Use \n
to create a new line.
Highlight GameObject in hierarchy
When selecting a log entry, you can highlight the GameObject it came from.
Shortcut to add/subtract or increment/decrement
Save / Load object selection in hierarchy and project view
Ctrl + Alt + (Number) = Save selection Ctrl + Shift + (Number) = Load selection
It works with GameObjects and Assets, including mixing the two.
Save content from play mode in edit mode
- Play your game, make changes.
- Right-click and Copy the gameobject(s) you want to save.
- Stop playing, right-click in hierarchy and Paste.
The same also works for modified components.
Hierarchy - Expand/Collapse All
- Hold left-alt when clicking an arrow to expand/collapse all of the children of a GameObject.
Inspector - Expressions
Number fields support math expressions. Typing 1+1 then enter will output 2.
Function support - sqrt, sin, cos, tan, floor, ceil, round.
Distribution over multi-selection (L, R) and can refer to current value to change it across multi-selection (+=3, *=2).
Inspector - Show auto-properties
You can now use [field: SerializeField]
to show auto-implemented properties in Unity’s inspector.
Physics - OnTrigger/OnCollision called on parent only
If a parent GameObject contains a Rigidbody component and a Component with an OnTrigger/OnCollision function, child Colliders will call the function on the parent.
This allows Compound Colliders and allows you to keep all of your scripts on the parent/root GameObject.
TryGetComponent vs GetComponent
TryGetComponent can reduce lines of code and avoid allocations in the editor.
Old code:
New code (Unity 2019.2 or higher):
TryGetComponent returns true or false, but also gives you the component as a reference via the out parameter.
C#7 allows you to inline the out variable.