ExposePropertiesInInspector SetOnlyWhenChanged

Original author: Mift (mift)

Variant author: Venryx (venryx)

Note: An updated version of this script package (which allows for exposing properties without dedicated editor scripts) can be found here: ExposePropertiesInInspector_Generic

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 only calls the property's setter method with the new value, when the new value has become different than the old value. This prevents problems in some projects where the setters contain extra logic that could mess things up if called every frame (which happens when the object is open in the inspector, and in play mode).

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:

Note
As you probably have seen in the code above, only the following types are currently exposed (though it should be easy enough to implement other ones): - Integer - Float - Boolean - String - Vector2 - Vector3 - Enum

Example component
Now create your type, i.e. MyType. 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].

The custom editor
Finally, create a custom editor script in "Assets/Editor" for the type whose properties you want to see exposed. For example: