Search Unity

Question How to get the path of an editor window?

Discussion in 'Scripting' started by ClearRoseOfWar, Feb 11, 2022.

  1. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    Hello wonderful people!

    I am in need of some quick assistance from someone who has done this before, or knows how to do this.
    I have researched extensively for the last 2 or 3 hours and have had no luck in finding what I thought would be a simple answer.

    As most of us know, the path to the game view when opening it from script is:

    Code (CSharp):
    1. EditorApplication.ExecuteMenuItem ("Window/General/Game");
    Which works great when you know the editor window you want to open.

    TLDR;

    How do I get the path of an editor window, to use like so:
    Code (CSharp):
    1. // This is sudo code, and doesnt actually work!!!!
    2. string lastWindow = EditorWindow.focusedWindow.Menu.Item.Path;// To output this-> ("Window/General/Game")
    3. ...
    4. // and use like this
    5. EditorApplication.ExecuteMenuItem (lastWindow);
    Thank you for reading!
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    What you consider a "path" is just an editor menu item that was probably created by a MenuItem attribute. You can create such a menu item for any kind of static method, not just for opening editor windows. Also you can also have multiple menu items all doing the same thing. So your request doesn't make much sense.

    As it's almost always the case with such exotic questions: Without knowing that the end result of this endeavour is, we can't really suggest anything else. Why do you want to know what menu item may have opened a certain window? Do you want to re-open the window later automatically? If that's the case, you can open end editor window directly when you know the type of the editor window. You usually open an editor window with EditorWindow.GetWindow. which will focus the window if it's already open or open a new one if not. If you want to open another window of the same type, you can use CreateWindow and pass the type of the window you want to open.

    You can get the type of a reference by using GetType() on that reference. But again, without knowing the purpose we're stabbing in the dark.
     
  3. ClearRoseOfWar

    ClearRoseOfWar

    Joined:
    Sep 6, 2015
    Posts:
    89
    Sorry. I was trying to keep it short. People seem more likely to help if you keep it short..

    That said, That was exactly what I needed. Rather than a path, just use the type. Perfect. Thank you so much!