I’ve been working on editor tools, which started out as simple editor windows, but now we also want to use them during automated build. This means that the actual tool should be functional without any GUI, but we also want the window when working in a specific scene and tweaking settings from a config file.
For now I am calling the static GetWindow function to get an instance of my tool class, even when the window is not required. This doesn’t feel right and also becomes an annoyance when several tools are run automatically and all windows pop up in the editor.
public class MeshProcessor : EditorWindow
{
[MenuItem("Window/Mesh Processor")]
public static MeshProcessor GetInstance()
{
return GetWindow<MeshProcessor>();
}
public void DoWork()
{
// ...
}
}
public static class AutomaticBuildPipeline
{
// This is run in batch mode or as part of some other automated tool in the editor.
public static void Build()
{
// Works fine, but also shows the editor window, although we don't need user interaction.
MeshProcessor meshProcessor = MeshProcessor.GetInstance();
meshProcessor.DoWork();
}
}
What would be a better general architecture for this scenario? Is there a way to get an editor window instance without showing it? Or should I separate the tool functionality from the window?
public class MeshProcessorWindow : EditorWindow
{
[MenuItem("Window/Mesh Processor")]
static void ShowWindow()
{
GetWindow<MeshProcessorWindow>();
}
MeshProcessor processor;
void OnEnable()
{
processor = new MeshProcessor();
processor.DoWork();
}
}
public class MeshProcessor
{
public void DoWork()
{
// ...
}
}
public static class AutomaticBuildPipeline
{
public static void Build()
{
MeshProcessor meshProcessor = new MeshProcessor();
meshProcessor.DoWork();
}
}
The second approach feels better, but it needs a lot of boilerplate code in practice. There are quite a few user settings, which would need to be brought over from the user window to the MeshProcessor instance, meaning that they would most likely have duplicated properties. Maybe I could pass a common settings object, but then I’m unsure about where to put the GUI code to draw those properties.
Thanks for your advice!