I’m developing a game that would take a hell of an time and effort if I did some tasks by myself. So I created a Menu Item which has some options that do the hard work for me. One of this options requires input from the developer, so I made it open an Editor Window. I have a coroutine that waits for the input. To use StartCoroutine I created (sort of) a Singleton class, which looks like this:
Singleton.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class Singleton : MonoBehaviour {
public static Singleton instance;
void Awake() {
if(instance == null) instance = this;
}
void Update() {
this.Awake();
}
}
And my MenuItem is something like this (simplified):
CustomMenu.cs
public class CustomMenu {
public static MonoBehaviour mb = Singleton.instance;
private class AskWindow : EditorWindow {
public static WindowData window;
public class WindowData {
public int n;
public bool closed = true;
}
public static void Init() {
AskWindow askwindow = EditorWindow.GetWindow<AskWindow>();
window = new WindowData();
askwindow.Show();
window.closed = false;
}
public void OnGUI() {
GUILayout.Label("Enter a number n (int)");
window.n = EditorGUILayout.IntField("n:",+1);
if(GUILayout.Button("Ok")) { Debug.Log("Window Closed"); window.closed = true; Close(); }
}
}
// ... Some options that are working ...
// but this one doesn't
[MenuItem("CustomMenu/Option %&\\",false,10)]
static void Option() {
mb.StartCoroutine(_Option());
}
private static IEnumerator _Option() {
AskWindow.Init();
while(!AskWindow.window.closed)
yield return null;
Debug.Log("Coroutine continued");
int n = AskWindow.window.n;
// do something with n
}
}
Now, when I go to CustomMenu > Option, an Editor Window opens with an input box. I enter an integer and press “Ok”, the text “Window closed” is printed on console, but the text “Coroutine continued” is not. And indeed, the action the option should do is not performed. Even stranger is that the moment I move anything in the scene or change something in my assets, “Coroutine continued” is printed and the action is performed. I don’t know how to solve this. Ant help would be much appreciated.