Tips and Tricks
Various little tips and tricks.
Editor
Search bar
The search bar has a lot of nifty tricks, check the
The Great QuickSearch Syntax Cheat Sheet
Pause Editor in Code
using UnityEngine;
using UnityEditor; // <--- This is needed - Doh!
public class ExampleScript: MonoBehaviour
{
void Update()
{
if (Time.time >=2f)
{
EditorApplication.isPaused=true;
}
}
}
Only use in editor mode
If you want to use a script field only in editor mode, you can use a compiler IF for it:
#if UNITY_EDITOR
[HideInInspector]
public string Description = "";
#endif
Plotting Debug Values Snippet
using UnityEngine;
public class ExampleScript: MonoBehaviour
{
public AnimationCurve plot = new AnimationCurve();
void Update()
{
float value=Math.Sin(Time.time);
plot.AddKey(Time.realtimeSinceStartup, value);
}
}
Package namespace not adressable
If the namespace of an important package isn't available, open the asmdef file in package > Runtime and set "Auto Referenced" to true. For example the Unity.DemoTeam.Attributes.asmdef the "autoReferenced" must set to true:
{
"name": "Unity.DemoTeam.Attributes",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
Game Architecture
Singletons Tutorial
Various
Random bool:
bool trueOrFalse = (Random.value>.5f);
Faster vector distance:
// Don't do this
if (Vector3.Distance(Point1, Point2) < distance) DoSomething();
// instead do this
if ((Point1 - Point2).sqrMagnitude < distance) DoSomething();
// This saves a square root call.
Comparing tags is faster with the CompareTags-function:
// Don't do this
if (other.tag=="Enemy") DoSomething();
// Do this
if (other.CompareTag("Enemy")) DoSomething();
NEVER use Camera.main
// This
Camera cam = Camera.main
// does basically this, which is amazingly inefficient.
Camera cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
// Rather use a public ref!
public Camera cam = null;
Switch for Components:
Component myComponent = GetRandomComponent();
switch (myComponent)
{
case Transform t:
Debug.Log($"Position is {t.position}.");
break;
case ParticleSystem ps:
ps.Play(true);
break;
case Camera cam:
cam.aspect=1.5;
break;
}
Write out some dictionary values in a very dirty fashion
string fileName = "Some_Dictionary_Data.txt";
File.WriteAllText(fileName, "var def = new Dictionary<int, string>\n{\n");
foreach (var keyValuePair in expressions)
{
File.AppendAllText(fileName, $"\t{keyValuePair.Key}, {keyValuePair.Value}\n");
}
File.AppendAllText(fileName,"}");
Input
3D Mouse Test
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 point = hit.point;
Vector2Int gridPoint = Geometry.GridFromPoint(point);
tileHighlight.SetActive(true);
tileHighlight.transform.position = Geometry.PointFromGrid(gridPoint);
}
Make strings for inputs
Define strings to identify joystick axis, names and other static readonly
using UnityEngine;
public class ExampleScript: MonoBehaviour
{
static readonly string Fire1 = "Fire1"; // <---- Like this!
void Update()
{
If (Input.GetAxis(Fire1)) doSomething();
}
}