Unity QuickTip: Designer Variables
Hey everyone. I wanted to share a quick tip that I've been using in Unity to keep my class integrity intact, make my code more readable and expose it to the inspector. I call them designer variables and declare them like this:
The "tip" is combining the SerializeField and Tooltip attributes to prevent extra vertical line usage and declaring the variable as private with the _ in front.
It's a simple thing but I don't see it often and I've found that it makes code a lot easier to read/understand. This is especially true when working in teams. It immediately communicates that the property is expected to be changed in the inspector. It also allows designers to get a hint as to what the variable changes while in the inspector view.
There is a tertiary effect of not providing intellisense that you would normally get with tags. This can be a pro or con depending on the usage, but you can still add /// Property descriptions. without any problems.
Some links to learn more about attributes
Tutorials Point - C# Attributes
Microsoft - C# Attributes
List of Unity attributes (Found in the left hand pane)
[SerializeField, Tooltip("This tooltip will show in the editor.")] private float _fooNumber;// This will display as Foo Number in the inspector.
The "tip" is combining the SerializeField and Tooltip attributes to prevent extra vertical line usage and declaring the variable as private with the _ in front.
It's a simple thing but I don't see it often and I've found that it makes code a lot easier to read/understand. This is especially true when working in teams. It immediately communicates that the property is expected to be changed in the inspector. It also allows designers to get a hint as to what the variable changes while in the inspector view.
There is a tertiary effect of not providing intellisense that you would normally get with tags. This can be a pro or con depending on the usage, but you can still add /// Property descriptions. without any problems.
Some links to learn more about attributes
Tutorials Point - C# Attributes
Microsoft - C# Attributes
List of Unity attributes (Found in the left hand pane)
Comments
Use [Header("abc")] to group your inspector vars under ..u guessed it .. a header
so Something like ..
[Header("Gun Attributes")]
public float GunMass = 0.25f; //Used for default speed reduction
public float FireSpeed = 0.25f;
public bool CanLook = true;
also..
to limit numbers you can use ....
[Range(0f,100f)]
public float UnitSpeedReduction = 0f;
will add a slider to the inspector making sure that engineers cant assign a value outside of the range
Can also be helpful for designer doing stuff
There's also a hacked together button attribute you can use to make a inspector button out of a functino instead of always making a custom inspector to do it.
Tutorials Point - C# Attributes
Microsoft - C# Attributes
List of Unity attributes (Found in the left hand pane)