03. Class Member Visibility

  03. Getting Started No Comments

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 via the preferences.

Member Visibility in the Default Inspector

The default inspector only displays class members when 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.

Attributes For Controlling Member Visibility

ShowInInspector

Power Inspector introduces a new attribute for customizing member visibility: ShowInInspector.
When added before any field, property, indexer or method, the class member will become visible inside the inspector view.
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 via the Inspector!

Usage

  1. Add “using Sisus.Attributes;” at the beginning of your script file.
  2. Add “[ShowInInspector]” before 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.

SerializeField with Auto-Properties

Unity has the SerializeField attribute, which can be added to non-public fields in order to tell Unity to serialize them and show them in the inspector.

C# has a feature that makes it possible to have an attribute target the field of an auto-implemented property. Using this feature, it is possible to both serialize and expose properties in the inspector.

Unfortunately, in the default inspector the prefix label of the field will be quite unreadable. Power Inspector will automatically display these fields with names matching the property that contains the SerializeField attribute.

Usage

  1. Add “using UnityEngine;” at the beginning of your script file.
  2. Add “[field: SerializeField]” before the definitions of auto-properties that you want to show up in the inspector view.

EditorBrowsable

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 before 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]” before 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”)]” before 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]” before the definitions of class members that you want to hide in the Inspector View.