- Home /
How to execute a method from a PropertyDrawer?
Hi,
How can i execute a method that belong to a script that attached to some GameObject from a PropertyDrawer?
for example i have these classes:
using UnityEngine;
[System.Serializable]
public class Data{
public Vector3 size = Vector3.zero;
public float speed;
}
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class Test: MonoBehaviour {
public Data data;
public void DoSomething(){/*..*/}
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(Data)]
public class DataDrawer : PropertyDrawer {
public override void OnGUI (Rect pos, SerializedProperty prop, GUIContent label)
{
SerializedProperty size = prop.FindPropertyRelative ("size");
SerializedProperty speed = prop.FindPropertyRelative ("speed");
...
if( GUI.Button(new Rect ( 0 , 0,100, 20),"Click Me" ) ){
//HOW TO EXECUTE DoSomething() method from this point?
}
}
}
I need to call "DoSomething" method that belong to "Test" class from the PropertyDrawer that decorate the "Data" class.
Help Please... Thanks
Answer by troien · Nov 16, 2014 at 08:58 PM
Well, I guess that normally it is actually bad practice to use something of the class you attatch your data to (Beecause then Data can only be added to that specific class.
If you don't mind though, the following line will execute your method.
(prop.serializedObject.targetObject as Test).DoSomething();
Can't you do something with/like Reflection?
.GetType().Get$$anonymous$$ethod(attribute.methodName);
and then execute the function on your targetObject?
ps: ofc the attribute has to have a string constructor property and store the methodName so you can use it on other classes as well?
Your answer
Follow this Question
Related Questions
Execute in EditMode every time a script's parameter is changed 0 Answers
What Does "ExecuteInEditMode" Actually Do? 1 Answer
ExecuteInEditMode and OnSceneGUI 2 Answers
Game is not running on mac 1 Answer
No methods executing in build 1 Answer