Unity QuickTip: Designer Variables

edited in Tutorials
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:

[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

  • To add to the editor vars inspector organisation:

    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
    Thanked by 1Rigormortis
  • +1 to this because using [SerializeField] instead of just making your fields public means that field accessibility isn't being dictated by whether you want something exposed to the editor. Other classes shouldn't have access to fields by default just because you want design time access to them. Sorry, pet peeve of mine in usual Unity code :P
  • Learnt about this recently too and it can be super useful - can call a function in your script by right clicking the component and finding the function at the bottom if you use [ContextMenu(" ")].

    Can also be helpful for designer doing stuff
    /// Add a context menu named "Do Something" in the inspector
    /// of the attached script.
    
    [ContextMenu("Do Something")]
    void DoSomething()
     {
      	Debug.Log("Perform operation");
    }


    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.

    image
    inspector.png
    436 x 149 - 7K
    Thanked by 2Elyaradine critic
  • I didn't originally intend for this to be about attributes, but since people seem to want to learn/discuss more about it here are some helpful links. :)

    Tutorials Point - C# Attributes
    Microsoft - C# Attributes
    List of Unity attributes (Found in the left hand pane)
  • You also have attributes in shaders, and you can define your own property drawers and things.
    // No UV Offsets.  (note: #NAME#_ST float4 declarations will still work)
    [NoScaleOffset]_MainTex ("Texture", 2D) = "white" {}
    
    // hdr
    [HDR]_PoopColor("My Poop Color", Color) = (0.23, 1, 0.55, 1) 
    
    // - this is shader equiv of inspector's EnumPopup or Popup calls.
    [KeywordEnum(Soft, Chunky, Salty, MethaneRainbow, ExtraShitty)]
    _PoopStyle("Poop Kind", Int) = 0
    
    // Also enums, but every second element is a numeric value for the name left of it
    // - this is shader equiv of inspector's IntPopup call.
    [Enum(Metallic, 10, Specular, 20, Splatty, 30)]
    _WorkFlow("Surface", Int) = 1
    
    // We are ashamed of poop granularity. So don't expose our secrets.
    [HideInInspector] _PoopGranularity("Granularity", Int) = 1
      
    [ToggleOff]  _SomeFloat("No, don't turn me off, i am a float.", Float) = 1.0
    [MaterialToggle] _Snap ("Snap", Int) = 0
    
    [Toggle] _Glimmer("Glimmer", float) =0
    // Also:
    [Toggle(SOME_SHADER_KEYWORD)] _Glimmer2("Glimmer2", float) =0 
    
    [Gamma] _Diffuse("Color Texture", 2D) = "grey"{}
    [Linear] _Diffuse2("Color Texture", 2D) = "grey"{}
    
    // This property does not appear on the material,
    //  renderer should display and handle it's behaviour. (Custom Renderers and custom batching)
    [PerRendererData] _SomeProperty("Property", Int) = 1
     
    [PowerSlider(5.0)] _Luminosity("Luminblahblah", Range(0.1, 1.5)) = 1

    Thanked by 1Elyaradine
Sign In or Register to comment.