- Home /
Call inspector editor script function from another editor script
Hi,
[CustomEditor (typeof(TestClass))]
public class TestClassEditor : Editor
{
float a = 1;
float b = 2;
public void sum ()
{
Debug.Log (a + b);
}
public override void OnInspectorGUI ()
{
a = EditorGUILayout.FloatField (a);
b = EditorGUILayout.FloatField (b);
if (GUI.Button(new Rect(20, 300, 100, 30), new GUIContent("sum")))
sum ();
}
}
In editor i can select gameobject and click on "sum" button, but I want to do this (call sum function) from another editor script.
e.g I could access to the game object with this code in another editor script:
GameObject obj = GameObject.Find ("object name");
obj.GetComponent <TestClass> (); // not TestClassEditor
but I want to do something like this:
GameObject obj = GameObject.Find ("object name");
obj.GetComponent <TestClassEditor> ().sum ();
Any idea?
Thanks
Answer by Bunny83 · Mar 19, 2013 at 09:16 AM
You have to get a reference to your Editor-instance. Keep in mind that it's possible that there is none or multiple (since you can open multiple Inspectors). With Resources.FindObjectsOfTypeAll you can search for any instances that currently exist.
TestClassEditor editors = (TestClassEditor[])Resources.FindObjectsOfTypeAll(typeof(TestClassEditor));
if (editors.Length >0)
{
editors[0].sum();
}
This will execute the sum function of the first instance it can find.
Thanks, it's solved my problem. gameObject should be in selection so Resources.FindObjectsOfTypeAll could return in so I add this code before your code:
Selection.activeTransform = GameObject.Find ("GameObject").transform;
Set Selection.activeTransform didn't work. It seems that when you select an object it won't load instantly
Selection.activeGameObject = GameObject.Find ("TestObject");
Debug.Log (Resources.FindObjectsOfTypeAll(typeof(TestClassEditor)).Length);
It prints 0 on first run, but on second run (or if i selected TestObject already) it prints 1.
btw FindObjectsOfTypeAll won't print 2 if I select 2 TestObject (commented line 1!).
Of course. If you select two objects but use only one inspector there will be only one editor instance. If you have two or more Inspector windows (by using the "add new tab" menu) you will get multiple instances, even for one selected object unless one inspector is "locked" on a different object.
It's actually a bit strange to have local variables in the editor class. They will be lost when you deselect the object. What's the purpose of thoas variables? Usually you would use either static variables or save / load them with EditorPrefs
If you want to store information per "edited object", those variables should be part of your actual inspected object (in your case TestClass).
I used variables just as a sample... Actually I'm using UniTile and I want to optimize it:
I didn't write UniTile so it's a bit hard for me to change it.
I understand the FindObjectsOfTypeAll now. I'm working on a EditorWindow that let me select GameObject, and when it opened (after 1 frame) I use FindObjectsOfTypeAll to get editor script...
If it doesn't work I have to change UniTile scripts (not editor scripts) as you said.
Thanks you very much, It would be great if you post anything that could help.
Answer by Chronos-L · Mar 19, 2013 at 08:22 AM
static function.
public class Operate {
public static void Sum( int a, int b ) {
Debug.Log( a + b );
}
}
To use it:
if (GUI.Button(new Rect(20, 300, 100, 30), new GUIContent("sum"))) {
Operate.Sum( a, b );
}
Thanks, but I want to access sum from another editor script, how could I access to Sum function from another editor script.
e.g I could access to the game object with this code in another editor script:
GameObject obj = GameObject.Find ("object name");
obj.GetComponent <TestClass> (); // not TestClassEditor
but I want to do something like this:
GameObject obj = GameObject.Find ("object name");
obj.GetComponent <TestClassEditor> ().sum ();
I don't think obj.GetComponent ()
is feasible.
GetComponent() returns Component, while Component is extended from Object. However, Editor inherits from ScriptableObject, which has nothing to do with Component
at all.