Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to open a second window from within a window and send data to or from it?

Discussion in 'UI Toolkit' started by KristofferH, Dec 23, 2021.

  1. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    51
    I have an EditorWindow for automating some stuff and speed up workflow in the editor. Something like this:

    Code (CSharp):
    1. public partial class MyInspector : EditorWindow
    2. {
    3.     [MenuItem("My Editors/My Inspector _I", false, 0)]
    4.     public static void ShowWindow()
    5.     {
    6.         // Opens the window, otherwise focuses it if it’s already open.
    7.         var window = GetWindow<MyInspector>();
    8.         // Adds a title to the window.
    9.         window.titleContent = new GUIContent("My Inspector");
    10.         // Sets a minimum size to the window.
    11.         window.minSize = new Vector2(250, 50);
    12.     }
    13.  
    14.     public void OnEnable()
    15.     {
    16.         VisualTreeAsset visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/MyInspector.uxml");
    17.         VisualElement ui = visualTree.Instantiate();
    18.         rootVisualElement.Add(ui);
    19.  
    20.         StyleSheet styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Editor/MyInspector.uss");
    21.         rootVisualElement.styleSheets.Add(styleSheet);
    22.  
    23.         // more code . . .
    24.     }
    25. }
    But I would like to be able to open more windows for specific purposes from pressing a button or something like that. So how do I make a button open a new window, and how can I send objects or values to this new window, and then get objects or values back from it again to the main window?

    Example:
    I would like to be able to press a button to open a new window and search/filter out specific component that are children of a specific GameObject and create buttons for each result. So I need to send the parent object to the window when it opens and then when I press one of the result buttons it should send that child object back to the main window and assign it there as a reference. Unity of course already has this feature for ObjectFields, but I want to make my own window so that I can customise it more and add other specific features.
     
  2. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    51
    Any ideas or input on this question?
     
  3. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    51
  4. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi,

    It's possible to open many window by using ScriptableObject.CreateInstance instead of GetWindow.
    Code (CSharp):
    1. var window =ScriptableObject.CreateInstance<MyInspector>();
    2. window.Show();
    From there you have the window instance and have full control over it. You can then send values between window instances and so on.
     
    Last edited: Feb 8, 2022