Unity编辑器扩展基础一、EditorGUILayout (一)

小结:

1、EditorGUILayout的各种Field和GUILayout的类似,但是EditorGUILayout可以复制粘贴;

2、因为OnGUI是每帧执行的,EditorGUILayout中类似TextField、IntSlider、ColorField需要传一个值,返回修改值的,需要将返回值赋值给临时变量;

3、params GUILayoutOption[] options可以传的参数在GUILayout类中可以找到: GUILayout.Width, GUILayout.Height, GUILayout.MinWidth, GUILayout.MaxWidth, GUILayout.MinHeight, GUILayout.MaxHeight, GUILayout.ExpandWidth, GUILayout.ExpandHeight

4、DelayedField延迟的输入框 在按下回车键或将焦点从文本字段移开之前,不会返回新值。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class EditorGUILayoutFieldExample : EditorWindow
{
    #region Field
    //BoundsField
    static Vector3 center = new Vector3(1, 2, 3);
    static Vector3 size = new Vector3(1, 2, 3);
    Bounds _bounds = new Bounds(center, size);

    //ColorField
    Color m_color = Color.white;

    //CurveField
    AnimationCurve m_curve = AnimationCurve.Linear(0, 0, 10, 10);

    //DelayedDoubleField DelayedFloatField DelayedIntField DelayedTextField
    double m_delayedDouble = 0;
    float m_delayedFloat = 0;
    int m_delayedInt = 0;
    string m_delayedString = "";

    //DoubleField FloatField IntField TextField
    double m_fieldDouble = 0;
    float m_fieldFloat = 0;
    int m_fieldInt = 0;
    string m_fieldString = "";
    long m_fieldLong = 0;

    //LayerField
    int selectedLayer = 0;

    //ObjectField
    Object m_fieldObject;

    //MaskField
    int flags = 0;
    string[] options = new string[] { "CanJump", "CanShoot", "CanSwim" };
    //TagField
    string tagStr = "Untagged";
    //RectField
    Rect m_rectFieldPos;

    #endregion

    [MenuItem("EditorGUILayout/EditorGUILayoutFieldExample")]
    static void Init()
    {
        EditorGUILayoutFieldExample window = (EditorGUILayoutFieldExample)EditorWindow.GetWindow(typeof(EditorGUILayoutFieldExample));
        window.Show();
    }
    void OnGUI()
    {      
        #region Field  输入框
        //BoundsField 边界输入框
        _bounds = EditorGUILayout.BoundsField("BoundsField:",_bounds);
        //ColorField 颜色输入框
        m_color = EditorGUILayout.ColorField("ColorField:",m_color);
        //CurveField 曲线输入框
        m_curve = EditorGUILayout.CurveField("CurveField:", m_curve);

        //DelayedField 
        m_delayedDouble = EditorGUILayout.DelayedDoubleField("DelayedDoubleField:", m_delayedDouble);
        m_delayedFloat = EditorGUILayout.DelayedFloatField("DelayedFloatField:", m_delayedFloat);
        m_delayedInt = EditorGUILayout.DelayedIntField("DelayedIntField:", m_delayedInt);
        m_delayedString = EditorGUILayout.DelayedTextField("DelayedTextField:", m_delayedString);

        m_fieldDouble = EditorGUILayout.DoubleField("DoubleField:", m_fieldDouble);
        m_fieldFloat = EditorGUILayout.FloatField("FloatField:", m_fieldFloat);
        m_fieldInt = EditorGUILayout.IntField("IntField:", m_fieldInt);
        m_fieldString = EditorGUILayout.TextField("TextField:", m_fieldString);
        m_fieldLong = EditorGUILayout.LongField("LongField:", m_fieldLong);

        //LabelField
        EditorGUILayout.LabelField("这是一个LabelField! ");
        //LayerField 可以获取所有的Layer
        selectedLayer = EditorGUILayout.LayerField("LayerField:", selectedLayer);
        //ObjectField
        m_fieldObject = EditorGUILayout.ObjectField(m_fieldObject, typeof(Object), true);
        //MaskField
        flags = EditorGUILayout.MaskField("MaskField:", flags, options);
        //RectField
        m_rectFieldPos = EditorGUILayout.RectField("RectField:", m_rectFieldPos);
        //TagField
        tagStr = EditorGUILayout.TagField("TagField:", tagStr);

        #endregion
    }
}

image.gif

以上就是EditorGUILayout中各种Field的用法了。

效果图:

image
image.gif

你可能感兴趣的:(Unity编辑器扩展基础一、EditorGUILayout (一))