PopupWindow

https://docs.unity3d.com/ScriptReference/PopupWindow.html

Description

Class used to display popup windows that inherit from PopupWindowContent.

Popup Windows are borderless, and not draggable or resizable. They also will automatically close when they lose focus. They are intended to show short-lived information or options.

An example of a Popup window in the editor is the "Scene View Effects" options, in the Editor's Scene View toolbar:

Unity PopupWindow

Below is an example of a custom popup window which is displayed via a button in an editor window. The Popup has three toggle values, and will automatically close when it loses focus. The example is given as two scripts. The first defines an editor window that can be opened via a menu item. That editor window has a button which shows the popup. The second script defines the contents of the popup itself as a separate class.

First, this is the code for the simple editor window which launches the popup:

using UnityEngine;
using UnityEditor;

public class EditorWindowWithPopup : EditorWindow
{
    // Add menu item
    [MenuItem("Example/Popup Example")]
    static void Init()
    {
        EditorWindow window = EditorWindow.CreateInstance<EditorWindowWithPopup>();
        window.Show();
    }

    Rect buttonRect;
    void OnGUI()
    {
        {
            GUILayout.Label("Editor window with Popup example", EditorStyles.boldLabel);
            if (GUILayout.Button("Popup Options", GUILayout.Width(200)))
            {
                PopupWindow.Show(buttonRect, new PopupExample());
            }
            if (Event.current.type == EventType.Repaint) buttonRect = GUILayoutUtility.GetLastRect();
        }
    }
}

Next, this is the code for the popup itself:

using UnityEngine;
using UnityEditor;

public class PopupExample : PopupWindowContent
{
    bool toggle1 = true;
    bool toggle2 = true;
    bool toggle3 = true;

    public override Vector2 GetWindowSize()
    {
        return new Vector2(200, 150);
    }

    public override void OnGUI(Rect rect)
    {
        GUILayout.Label("Popup Options Example", EditorStyles.boldLabel);
        toggle1 = EditorGUILayout.Toggle("Toggle 1", toggle1);
        toggle2 = EditorGUILayout.Toggle("Toggle 2", toggle2);
        toggle3 = EditorGUILayout.Toggle("Toggle 3", toggle3);
    }

    public override void OnOpen()
    {
        Debug.Log("Popup opened: " + this);
    }

    public override void OnClose()
    {
        Debug.Log("Popup closed: " + this);
    }
}

Each of these should be saved as separate files named after their class name. Neither are behaviours, so you do not need to place them on a gameobject. Once they are in your project, try it by going to the new "Example" menu and selecting Popup Example. Then click the button in the new editor window to reveal the popup options window.

Unity PopupWindow.

 

 

 

 

 

相关文章: