Version: 5.6
public static int Popup (int selectedIndex, string[] displayedOptions, params GUILayoutOption[] options);
public static int Popup (int selectedIndex, string[] displayedOptions, GUIStyle style, params GUILayoutOption[] options);
public static int Popup (int selectedIndex, GUIContent[] displayedOptions, params GUILayoutOption[] options);
public static int Popup (int selectedIndex, GUIContent[] displayedOptions, GUIStyle style, params GUILayoutOption[] options);
public static int Popup (string label, int selectedIndex, string[] displayedOptions, params GUILayoutOption[] options);
public static int Popup (string label, int selectedIndex, string[] displayedOptions, GUIStyle style, params GUILayoutOption[] options);
public static int Popup (GUIContent label, int selectedIndex, GUIContent[] displayedOptions, params GUILayoutOption[] options);
public static int Popup (GUIContent label, int selectedIndex, GUIContent[] displayedOptions, GUIStyle style, params GUILayoutOption[] options);

参数

label (可选)字段前的标签。
selectedIndex 字段显示的选项的索引。
displayedOptions 弹出菜单中所示选项的数组。
style 可选 GUIStyle
options 一个可选的布局选项列表,用于指定额外的布局属性。此处传递的任何值都将覆盖 style 定义的设置。
另请参阅:GUILayout.WidthGUILayout.HeightGUILayout.MinWidthGUILayout.MaxWidthGUILayout.MinHeightGUILayout.MaxHeightGUILayout.ExpandWidthGUILayout.ExpandHeight

返回

int 用户所选选项的索引。

描述

创建一个通用弹出选择字段。

以参数形式获取当前所选的索引,并返回用户选择的索引。


Create a primitive depending on the option selected.

using UnityEditor;
using UnityEngine;
using System.Collections;

// Creates an instance of a primitive depending on the option selected by the user. public class EditorGUILayoutPopup : EditorWindow { public string[] options = new string[] {"Cube", "Sphere", "Plane"}; public int index = 0; [MenuItem("Examples/Editor GUILayout Popup usage")] static void Init() { EditorWindow window = GetWindow(typeof(EditorGUILayoutPopup)); window.Show(); }

void OnGUI() { index = EditorGUILayout.Popup(index, options); if (GUILayout.Button("Create")) InstantiatePrimitive(); }

void InstantiatePrimitive() { switch (index) { case 0: GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = Vector3.zero; break; case 1: GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = Vector3.zero; break; case 2: GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); plane.transform.position = Vector3.zero; break; default: Debug.LogError("Unrecognized Option"); break; } } }