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 用户所选选项的索引。

描述

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

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


Popup in and Editor Window.

// Adds a component to the selected GameObjects

class EditorGUIPopup extends EditorWindow { var options : String[] = ["Rigidbody", "Box Collider", "Sphere Collider"]; var index : int = 0;

@MenuItem("Examples/Editor GUI Popup usage") static function Init() { var window = GetWindow(EditorGUIPopup); window.position = Rect(0,0,180,80); window.Show(); }

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

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

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

for(var go : GameObject in Selection.gameObjects) switch (index) { case 0: go.AddComponent.<Rigidbody>(); break;

case 1: go.AddComponent.<BoxCollider>();

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

注意: displayedOptions 列出选项数组。若这些元素中包含“/”(斜杠字符)时,即表示它们用于子菜单。斜杠左边的文本决定了结构。