03. Class Member Visibility

  03. Getting Started No Comments

Member Visibility in the Default Inspector

The default inspector only displays class members if they pass all of the following conditions:

  1. It is a field.
  2. It is not static.
  3. It does not have the const keyword.
  4. It does not have the readonly keyword.
  5. It is not generic type.
  6. It is not abstract type.
  7. Its type is primitive, enum, Unity built-in type or struct or class with the Serializable attribute.
  8. It is public, or has the SerializeField attribute.
  9. (It does not have the HideInInspector attribute.)

These strict limitations leave out a lot of valid use cases, where it would be useful to see data in the Inspector but it’s not possible (at least without going through the trouble of writing Custom Editors). Say you would like to expose a Dictionary field in the inspector, and handle its serialization manually; this is not possible in the default inspector but can easily be done with Power Inspector.

Member Visibility in Power Inspector

By default Power Inspector displays class members similarly to the default inspector for consistency and intuitiveness.
However, it gives you the ability to expose practically any class members when you want to.

Tip: You can customize what fields, properties and methods are shown in Power Inspector by default using the preferences.

Attributes For Controlling Member Visibility

ShowInInspectorAttribute

The one new attribute that Power Inspector adds for customizing member visibility is the ShowInInspector attribute.
Any field, property, indexer or method that contains this attribute will be shown in Power Inspector.
Another benefit of using this attribute is that it tells the compiler that the class member in question can be assigned implicitly. What this does it gets rid of misleading warning messages like “Field ‘X’ is never assigned.” or “Method ‘X’ is never used.” – as they actually can be assigned and used using the Inspector!

Usage

  1. Add the line “using Sisus.Attributes;” at the beginning of your script file.
  2. Add the line “[ShowInInspector]” above the definitions of class members that you want to show up in the Inspector View.

Tip: If you have another attribute named exactly “ShowInInspector”, you can also use that to tag class members, and they will still be shown in Power Inspector.

EditorBrowsableAttribute

The EditorBrowsable attribute is a built-in alternative to the ShowInInspector attribute.
Any field, property, indexer or method that contains this attribute (initialized with the default constructor) will be shown in Power Inspector.
Note that this attribute does not automatically tell the compiler that the class member in question can be assigned implicitly. However, you can still do this manually by also adding the UsedImplicitly attribute above the class member.

Tip: You might choose to use this instead of the ShowInInspector attribute if you want to avoid coupling your code with references to attributes that don’t come built-in with Unity.

Usage

  1. Add the line “using System.ComponentModel;” at the beginning of your script file.
  2. Add the line “[EditorBrowsable]” above the definitions of class members that you want to show up in the Inspector View.

ContextMenu

If the Show Methods preference item has been configured to ContextMenu, then all methods with the ContextMenu attribute will show up as buttons in the Inspector View.
However, by default this behaviour has been disabled in Power Inspector.

Usage

  1. Make sure you have the line “using UnityEngine;” at the beginning of your script file.
  2. Add the line “[ContextMenu(“ItemName”)]” above the methods that you want to show up in the Inspector View.

HideInInspector

Class members with the HideInInspector attribute will not be shown in Power Inspector (unless Debug Mode+ is enabled).

Usage

  1. Make sure you have the line “using UnityEngine;” at the beginning of your script file.
  2. Add the line “[HideInInspector]” above the definitions of class members that you want to hide in the Inspector View.