ExposePropertiesInInspector Generic

Original author: Venryx (venryx) (variant from Mift (mift) )

Variant author: Tiago Roldão (Tiago Roldão)

Original Description
The scripts below make the properties (i.e. members having getters/setters) of a class visible to the Unity inspector. This lets you keep the class's fields private, and force all external access to come through the properties (where you can add range limitations or other logic), without losing the ability to still edit those values using the Unity inspector.

Variant Description
This variant of the ExposeProperties system is a generic version, allowing for classes to have exposed properties in the inspector without the need for specific editor scripts for each, having only to inherit from the ExposableMonobehavior Class. Venryx's variant was used as the basis for this, as his changes to only trigger setters when values actually change, a difference that becomes even more important in this implementation.

The attribute
Create a new C# script named "ExposePropertyAttribute" and paste the following code:

The ExposeProperties static helper
Now create another script in the "Assets/Editor" folder named "ExposeProperties" and paste the following code:

The ExposableProperties Class and Editor
Create a script named "ExposableMonobehavior.cs" with the following Class definition:

And another script in the "Assets/Editor" folder named "ExposableMonobehaviorEditor.cs" with the following code:

Example component
Now create a Class extending ExposableMonobehaviour, and applying the correct attributes. Properties MUST have both getter and setter accessors AND [ExposeProperty] attribute set otherwise the property will not be shown.

In order for the property to maintain values when you hit play, you must add [SerializeField] to the field that the property is changing. Unfortunately, this exposes the field to the Editor (and editing this value does not call the property getter/setter), so you have to explicitly hide it with [HideInInspector].