EditorGUI.Popup

Unity · Версия 2020.3 LTS · Scripting · EditorGUI

EditorGUI.Popup

Объявление

public static int Popup(Rect position, int selectedIndex, string[] displayedOptions, GUIStyle style = EditorStyles.popup);

public static int Popup(Rect position, int selectedIndex, GUIContent[] displayedOptions, GUIStyle style = EditorStyles.popup);

public static int Popup(Rect position, string label, int selectedIndex, string[] displayedOptions, GUIStyle style = EditorStyles.popup);

public static int Popup(Rect position, GUIContent label, int selectedIndex, GUIContent[] displayedOptions, GUIStyle style = EditorStyles.popup);

Параметры

position Прямоугольник на экране использовать для поля.
label Необязательная метка перед полем.
selectedIndex Индекс опции, которую показывает поле.
displayedOptions Массив с параметрами, показанными во всплывающем окне.
style Необязательный стиль GUIStyle.

Возвращает

int Индекс опции, выбранной пользователем.

Описание

Создает стандартное всплывающее поле выбора.

Принимает текущий выбранный индекс в качестве параметра и возвращает индекс, выбранный пользователем.

Всплывающее окно и окно редактора.
using UnityEngine;
using UnityEditor;


// Добавляет компонент к выбранным GameObjects

class EditorGUIPopup : EditorWindow
{
    string[] options = { "Rigidbody", "BoxCollider", "Sphere Collider" };
    int index = 0;

    [MenuItem("Examples/EditorGUI Popup usage")]
    static void Init()
    {
        var window = GetWindow();
        window.position = new Rect(0, 0, 180, 80);
        window.Show();
    }

    void OnGUI()
    {
        index = EditorGUI.Popup(
            new Rect(0, 0, position.width, 20),
            "Component:",
            index,
            options);

        if (GUI.Button(new Rect(0, 25, position.width, position.height - 26), "Add Component"))
            AddComponentToObjects();
    }

    void AddComponentToObjects()
    {
        if (!Selection.activeGameObject)
        {
            Debug.LogError("Please select at least one GameObject first");
            return;
        }

        foreach (GameObject obj in Selection.gameObjects)
        {
            switch (index)
            {
                case 0:
                    obj.AddComponent<Rigidbody>();
                    break;

                case 1:
                    obj.AddComponent<BoxCollider>();
                    break;

                case 2:
                    obj.AddComponent<SphereCollider>();
                    break;
            }
        }
    }
}

Note: The displayedOptions lists an array of options. When these elements contain "/" (slash characters) the elements are use for sub-menus. The text to the left of the slashes determines the structure.

Обновлено 17 ноября 2026 г.