Debuggable Editors - edcasillas/unity-common-utils GitHub Wiki
With Debuggable Editors, you can make use of the [ShowInInspector]
attribute to expose properties and methods at runtime in the Inspector.
[ShowInInspector]
does the opposite to the built-in [HideInInspector]
attribute, except the latter is for fields while the former is for properties and methods.
This is a fantastic tool for debugging, since it allows you to inspect values only available at runtime without the need to write any logs to the console, as well as execute methods on game object components by directly clicking on the Inspector.
public class SampleObject : MonoBehaviour {
[ShowInInspector] public int IntValue => 5;
[ShowInInspector]
public IEnumerable<string> ListOfStrings => new[] { "This", "is", "a", "list", "of", "strings" };
[ShowInInspector] public void Echo(string input = "Hello") => Debug.Log(input);
[ShowInInspector] public int AddOne(int input) => input + 1;
}
At runtime, the Inspector will look like this when the game object with the SampleObject
script attached to it is selected:
In order to make use of the [ShowInInspector]
attribute, a custom editor must be defined for the class using it. Add the following script to your Editor
folder and replace SampleObject
with the name of your own class:
using CommonUtils.Editor.DebuggableEditors;
using UnityEditor;
[CustomEditor(typeof(SampleObject))]
public class SampleObjectEditor : AbstractDebuggableEditor<SampleObject> { }
To make things even easier, the EnhancedMonoBehaviour
base class already defines a debuggable editor. Make your class derive from EnhancedMonoBehaviour
instead of MonoBehaviour
and you're ready to use the [ShowInInspector]
attribute.
public class SampleObject : EnhancedMonoBehaviour {
[ShowInInspector] public int IntValue => 5;
}