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
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;
– mojtaba64Set 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!).
– mojtaba64Of 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.
– Bunny83It'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@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?
– nowletsstart