Search Unity

Get the instance the SerializedProperty belongs to in a CustomPropertyDrawer

Discussion in 'Scripting' started by Maeslezo, Oct 9, 2015.

  1. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    332
    Hello,

    I think this is an old issue and I found several people with the same problem but not a real answer.

    The problem is when you have a Class ClassWithCustomPropertyDrawer with a custom property drawer and you have an array of this class or you have a class A that contains an object of class B that contains an object of type ClassWithCustomPropertyDrawer.
    ClassA
    - ClassB
    -- ClassWithCustomPropertyDrawer


    The problem is that fieldInfo.GetValue(property.serializedObject.targetObject) get an error because targetObject returns the monobehaviour an not the actual object.

    I have seen some workarrounds and pseudo-fixes but they are "too workarround" and they doesn't work sometimes.

    Isn't there any Unity built-in real solution for this problem?

    I am pretty sure that this problem has to be very common but I can not find any solution.

    Thank you very much.

    Other post about this:
    http://answers.unity3d.com/questions/1005030/intractable-custompropertydrawer.html
    http://answers.unity3d.com/questions/425012/get-the-instance-the-serializedproperty-belongs-to.html
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I don't think you're meant to do that... I also asked this question a while ago, and i was told that the managed (C#) object may not even exist. Recall that SerializedProperties are merely a "view" for a serialized stream of data; it may not exactly correspond to a managed object at all...