- Home /
Unity Editor: Calling EditorCoroutine from MenuItem
I'm trying to write an Editor script that will load a scene additively, then get every gameobject in that scene and move it a set distance. EditorCoroutineUtility wants me to pass it an instance to run the coroutine on, and MenuItem wants the method to be static.
I tried making a static instance like I do with my Scriptable Objects and running the coroutine on that, but the coroutine wont start as far as I can tell. Here is where I am at, the only Debug message I get is "Calling coroutine"
public class MapEditor : Editor
{
//I dont honestly understand how these two lines are even supposed to work
//but they do for my scriptable objects
private static MapEditor instance;
public static MapEditor Instance { get { return instance; } }
private void OnEnable()
{
instance = new MapEditor();
}
[MenuItem("MapEditor/Display Neighbors")]
public static void displayNeighbors()
{
Debug.Log("Calling coroutine");
EditorCoroutineUtility.StartCoroutine(loadScene(), Instance);
}
static IEnumerator loadScene()
{
Debug.Log("loading scene");
yield return EditorSceneManager.LoadSceneAsync("pinkFruitTree", LoadSceneMode.Additive);
Debug.Log("loaded scene");
Scene s = EditorSceneManager.GetSceneByName("pinkFruitTree");
GameObject[] gameObjects = s.GetRootGameObjects();
Debug.Log(gameObjects.Length);
foreach (GameObject g in gameObjects)
{
g.transform.position += Vector3.right * 20;
}
}
}
Answer by diego-giacomelli · Nov 01, 2020 at 10:09 AM
In line 10
you are creating a new MapEditor
and you are not using the current one.
Try changing line 10
to: instance = this;
Answer by Gretsok · Jun 01 at 09:59 AM
"EditorSceneManager.LoadSceneAsync" returns a classic Coroutine which cannot be used when the editor is not playing. Sadly, I don'y think you could nest EditorCoroutines to make it work.