EditorGUILayout

作者:追风剑情 发布于:2015-11-7 16:47 分类:Unity3d

EditorGUILayout中基本控件的使用

using UnityEngine;
using UnityEditor;

public class GUITest : EditorWindow
{
    enum EnumType
    {
        EnumType1,
        EnumType2,
    }

    enum EnumMask
    {
        EnumMask1,
        EnumMask2,
        EnumMask3
    }

    string textField1 = "Text Field 1";
    string textField2 = "Text Field 2";
    string textArea = "Text Area";
    string helpBox = "Help...";
    int prefabLayer = 1;
    int mask = 1;
    int number = 95;
    int popupNum = 1;
    int[] popupNumArr = new int[3] { 1, 2, 3 };
    string[] popupNumNames = new string[3] { "Number1", "Number2", "Number3" };
    float sliderValue = 20f;
    bool toggleGroup = false;
    bool toggle = true;
    bool toggle1 = false;
    bool foldout = false;
    bool foldout1 = false;
    Color color = Color.white;
    Vector2 vector2 = Vector2.zero;
    Vector4 vector4 = Vector4.zero;
    Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
    AnimationCurve curve = new AnimationCurve();
    EnumType enumType;
    EnumMask enumMask;
    Transform tran;

    void OnGUI()
    {
        GUILayout.BeginVertical();//开始纵向布局
        {
            GUILayout.Label("This is Label");
            EditorGUILayout.SelectableLabel("SelectableLabel");
            GUILayout.Space(2f);//间隔

            GUILayout.BeginHorizontal();//开始横向布局
            {
                //字符文本框
                textField1 = EditorGUILayout.TextField(textField1);
                textField2 = EditorGUILayout.TextField(textField2);
            }
            GUILayout.EndHorizontal();//结束横向布局

            //数字文本框
            number = EditorGUILayout.IntField("Number", number, GUILayout.Width(180f));

            //整数弹出选择菜单
            popupNum = EditorGUILayout.IntPopup("IntPopup", popupNum, popupNumNames, popupNumArr);

            //密码框
            EditorGUILayout.PasswordField("password");

            //物体层级选择框
            prefabLayer = EditorGUILayout.LayerField("Layer:", prefabLayer);

            //2D向量
            vector2 = EditorGUILayout.Vector2Field("Vector2", vector2);

            EditorGUILayout.Separator();//分割(增大间距)

            //单选按钮组
            toggleGroup = EditorGUILayout.BeginToggleGroup("ToggleGroup", toggleGroup);
            {
                toggle = EditorGUILayout.Toggle("Toggle", toggle, GUILayout.Width(180f));
                toggle1 = EditorGUILayout.Toggle("Toggle1", toggle1, GUILayout.Width(180f));
            }
            EditorGUILayout.EndToggleGroup();

            //包围盒
            bounds = EditorGUILayout.BoundsField(bounds);

            //动画曲线
            curve = EditorGUILayout.CurveField(curve, GUILayout.Height(30f));

            //枚举
            enumType = (EnumType)EditorGUILayout.EnumPopup("EnumPopup", enumType, GUILayout.Width(240f));

            //遮罩枚举
            enumMask = (EnumMask)EditorGUILayout.EnumMaskField("EnumMask", enumMask);
            //遮罩菜单
            mask = EditorGUILayout.MaskField(mask, new string[3] { "mask1", "mask2", "mask3" }, GUILayout.Width(240f));

            //Label
            EditorGUILayout.PrefixLabel("PrefixLabel");

            //折叠标签
            foldout = EditorGUILayout.Foldout(foldout, "Foldout");
            if (foldout)
            {
                //显示子组件
            }

            //检视面板标题栏
            /*if (null != Selection.activeGameObject.transform)
            {
                foldout1 = EditorGUILayout.InspectorTitlebar(foldout1, Selection.activeGameObject.transform);
                if (foldout1)
                {
                    //显示子组件
                }
            }*/

            //颜色选取器
            color = EditorGUILayout.ColorField(color, GUILayout.Width(150));

            //对象
            tran = EditorGUILayout.ObjectField("Transform", tran, typeof(Transform), true) as Transform;

            //文本区
            textArea = EditorGUILayout.TextArea(textArea, GUI.skin.textArea, GUILayout.Height(50f));

            EditorGUILayout.HelpBox(helpBox, MessageType.Info);

            //滑动条
            sliderValue = EditorGUILayout.Slider(sliderValue, 0f, 100f, GUILayout.Width(240f));

            //按钮
            if (GUILayout.Button("Button"))
            {

            }
        }
        GUILayout.EndVertical();//结束纵向布局
    }
}

运行效果

gui.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号