As you see I’ve even tried passing propOwner
( this
) as argument to [MyProperty()]
, but it’s just stupid. I feel like there is an easy way to get what I want without all these macaroni that I wrote.
public class MyMonoBehaviour: MonoBehaviour //<-- I get instance of this with PropertyDrawer.serializedObject.targetObject
{
[System.Serializable]public class MyNestedClass
{
[MyProperty]public bool myBool;
//[MyProperty(this)]public bool myBool; //Didn't work
public void MyMethod(){} //<-- Actually I need to call this
}
public MyNestedClass myNestedClassInstance; //<-- But I need this!
void MyMethod(){} //<-- This being called instead
}
public class MyPropertyAttribute: PropertyAttribute
{
object propOwner; //Can't find any handles here too
public MyPropertyAttribute(object propOwner = null) //Optional attribute for testing
{
this.propOwner = propOwner;
}
}
[CustomPropertyDrawer(typeof(MyPropertyAttribute))]
public class MyPropertyDrawer: PropertyDrawer
{
MyPropertyAttribute myPropertyAttribute = (MyPropertyAttribute) attribute; // <-- Found no handles
Type ownerType = fieldInfo.DeclaringType; //<-- The only thing I've found, but I need instance anyway
string myMethodPath = prop.propertyPath.Replace("."+prop.name, ""); // I've felt so close with this, but it
var kindOfWhatINeed = prop.serializedObject.FindProperty(propPath); //returns SerializedProperty and I
//can't find any handles to actual instance or Invoke on this directly (but .type shows MyNestedClass)
public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
{
if (GUI.Button(position, prop.serializedObject.targetObject)) //<-- Shows MyMonoBehaviour instance
{
prop.serializedObject.targetObject.GetType().GetMethod("MyMethod")
.Invoke(prop.serializedObject.targetObject); //Invokes MyMethod of MyMonoBehaviour
//object ownerInstance = prop.owner; <-- I need something like this (returns myNestedClassInstance)
//ownerType.GetMethod("MyMethod").Invoke(ownerInstance, null); //<-- That's what I want
}
}
}