I recently made a custom inspector and I just realized that my OnValidate() is not being called when I edit a variable in the Inspector. Any ideas on how to get my calls back to OnValidate() again while keeping the custom inspector I used?
The answer was in serializing and propertyfield.
Example with my code, the first part here is just showing that in my main script I have this declared. Remember now, public variables are already serialized so no need to put that.
public class Original : MonoBehaviour {
// Used for the user to input their board section width and height.
[Tooltip("The desired Camera Width.")]
public float cameraWidth;
}
Now in my Custom inspector I have this:
pubilc class Original_Editor : Editor{
public override void OnInspectorGUI(){
serializedObject.Update();
// Get the camera width.
SerializedProperty width = serializedObject.FindProperty("cameraWidth");
// Set the layout.
EditorGUILayout.PropertyField(width);
// Clamp the desired values
width.floatValue = Mathf.Clamp((int)width.floatValue, 0, 9999);
// apply
serializedObject.ApplyModifiedProperties();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With