Search Unity

Force custom PropertyDrawer to trigger OnValidate()

Discussion in 'Immediate Mode GUI (IMGUI)' started by MattRix, Jul 28, 2015.

  1. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    I have a PropertyDrawer for a custom Serializeable class type, and can edit and change it fine. However I've run into a snag where since I'm changing the property using fieldInfo with GetValue() and SetValue(), it doesn't seem to trigger OnValidate() on the target object.

    Undo.RecordObject() works fine, so I can undo the changes, but even after calling property.serializedObject.SetIsDifferentCacheDirty() and property.serializedObject.Update() I still don't get OnValidate() called.
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    I'm going to assume this isn't working because the serializedObject is detecting changes, since you're modifying those values using reflection rather than through the serializedObject/serializedProperties.

    maybe try calling it manually yourself.

    Code (CSharp):
    1. MonoBehaviour target = property.serializedObject.targetObject as MonoBehaviour;
    2.  
    3. if (target) {
    4.  
    5.     target.SendMessage("OnValidate", null, SendMessageOptions.DontRequireReceiver);
    6. }
     
    DrBoum, bstockwell and bWayneSu like this.