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

2 Answers

2

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 <a href="http://docs.unity3d.com/Documentation/ScriptReference/EditorPrefs.html">EditorPrefs</a> If you want to store information per "edited object", those variables should be part of your actual inspected object (in your case TestClass).

@Bunny83 could you please take a look at this (https://answers.unity.com/questions/1563071/scene-view-cannot-recognise-movement-of-several-ob.html) editor related question for me?

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 ();