- Home /
How to show an Editor Window based upon Selection
So I want to show a custom editor window, but only when the selected gameobject in the hierarchy has a certain component or components. So click on a gameobject with a rigidbody component and the window shows, click on a gameobject without one (or deselect) and it hides.
I know I can manually trigger the window and I know I can check Selection.activeGameObject for those components, but I'm at a loss at how to run that check WITHOUT having an editor window open. Any ideas? Workarounds? I imagine I'll have to have an instance of the script somewhere, right? :/
Answer by drod7425 · Nov 02, 2014 at 04:49 AM
Well, sorry for answering my own question, but I think I figured it out! I can use InitializeOnLoad to run a static constructor to hook a static Update function to the EditorApplication.update delegate. Exactly the sort of functionality I was looking for and it works perfectly!
Here is the page of the manual that got me the answer I was looking for: http://docs.unity3d.com/Manual/RunningEditorCodeOnLaunch.html
And here is a bit of (rough) code that shows my solution. I still need to pare it down, but it illustrates the point:
using UnityEngine; using UnityEditor; using System.Collections;
[InitializeOnLoad] class Launcher{
static Launcher(){
EditorApplication.update+=Update;
}
// Update is called once per frame
static void Update () {
if(Selection.activeObject){
if(MyWindow.myWindow==null){
Debug.Log("have object!");
MyWindow.ShowWindow();
}
}else if(MyWindow.myWindow!=null){
MyWindow.HideWindow();
}
}
}