label | (可选)字段前的标签。 |
selected | 该字段显示的枚举选项。 |
style | 可选 GUIStyle。 |
options | 一个可选的布局选项列表,用于指定额外的布局属性。此处传递的任何值都将覆盖 style 定义的设置。另请参阅:GUILayout.Width、GUILayout.Height、GUILayout.MinWidth、GUILayout.MaxWidth、GUILayout.MinHeight、 GUILayout.MaxHeight、GUILayout.ExpandWidth、GUILayout.ExpandHeight。 |
Enum 用户已选择的枚举选项。
创建一个枚举弹出选择字段。
将当前所选的枚举值作为参数,并返回用户选择的枚举值。
Creates a primitive selected by the user.
using UnityEditor; using UnityEngine; using System.Collections;
// Creates an instance of a primitive depending on the option selected by the user.
public enum OPTIONS { CUBE = 0, SPHERE = 1, PLANE = 2 }
public class EditorGUILayoutEnumPopup : EditorWindow { public OPTIONS op; [MenuItem("Examples/Editor GUILayout Enum Popup usage")] static void Init() { UnityEditor.EditorWindow window = GetWindow(typeof(EditorGUILayoutEnumPopup)); window.Show(); }
void OnGUI() { op = (OPTIONS)EditorGUILayout.EnumPopup("Primitive to create:", op); if (GUILayout.Button("Create")) InstantiatePrimitive(op); }
void InstantiatePrimitive(OPTIONS op) { switch (op) { case OPTIONS.CUBE: GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = Vector3.zero; break; case OPTIONS.SPHERE: GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = Vector3.zero; break; case OPTIONS.PLANE: GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); plane.transform.position = Vector3.zero; break; default: Debug.LogError("Unrecognized Option"); break; } } }