Version: 2021.1
言語: 日本語
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 ユーザーによって設定された値

説明

Makes a generic popup selection field.

現在選択されているインデックスをパラメーターとして受け取り、ユーザーによって選択されたインデックスを返します。


Popup in and Editor Window.

using UnityEngine;
using UnityEditor;

// Adds a component to the selected GameObjects

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

[MenuItem("Examples/Editor GUI Popup usage")] static void Init() { var window = GetWindow<EditorGUIPopup>(); 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; } } } }

注意: displayedOptions はオプションの配列をリストします。これらのエレメントに "/" (スラッシュ文字) が含まれているとき、エレメントはサブメニューのために使います。スラッシュの左側のテキストが構造を決定します。