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:
- It is a field.
- It is not static.
- It does not have the const keyword.
- It does not have the readonly keyword.
- It is not generic type.
- It is not abstract type.
- Its type is primitive, enum, Unity built-in type or struct or class with the Serializable attribute.
- It is public, or has the SerializeField attribute.
- (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.
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
- Add “using Sisus.Attributes;” at the beginning of your script file.
- Add “[ShowInInspector]” before the definitions of class members that you want to show up in the inspector.
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
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.
Power Inspector will display any fields that have this attribute (note: fields will be visible in Power Inspector even if Unity can’t actually serialize the field).
Usage
- Add “using UnityEngine;” at the beginning of your script file.
- Add “[SerializeField]” before the definitions of fields that you want to show up in the inspector.
SerializeField with Properties
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.
Power Inspector will automatically display the property backing field with label generated from the name of the property.
Usage
- Add “using UnityEngine;” at the beginning of your script file.
- Add “[field: SerializeField]” before the definitions of auto-properties that you want to show up in the inspector.
Serialize Reference
For serializing more complex data types such as abstract types, generic types and classes containing cyclical references Unity offers the SerializeReference attribute.
Power Inspector will display any fields that have this attribute (note: fields will be visible in Power Inspector even if Unity can’t actually serialize the field).
For abstract types Power Inspector will allow you to assign an instance of any implementing type through a convenient popup menu.
Usage
- Add “using UnityEngine;” at the beginning of your script file.
- Add “[SerializeReference]” before the definitions of fields that you want to show up in the inspector.
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.
Why EditorBrowsable? 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. This can be convenient if you ever need to use the same code in a project that doesn’t include Power Inspector.
Usage
- Add the line “using System.ComponentModel;” at the beginning of your script file.
- 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
- Make sure you have the line “using UnityEngine;” at the beginning of your script file.
- 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
- Make sure you have the line “using UnityEngine;” at the beginning of your script file.
- Add the line “[HideInInspector]” before the definitions of class members that you want to hide in the Inspector View.