Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity - Is it possible to access OnValidate() when using a custom inspector?

Tags:

c#

unity3d

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?

like image 538
JoeyL Avatar asked Sep 15 '15 00:09

JoeyL


1 Answers

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();
         }
    }
like image 84
JoeyL Avatar answered Oct 04 '22 01:10

JoeyL