Unity編輯器擴展基礎三、EditorGUILayout (三)

小結:

1、畫下拉框的方法有DropdownButton、EnumMaskField、EnumPopup、IntPopup、Popup、EnumMaskPopup、MaskField,DropdownButton比較麻煩,EnumMaskField、EnumMaskPopup、MaskField可以多選。

2、畫Slider的都沒什么區(qū)別,只是MinMaxSlider是取值范圍。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
/// <summary>
/// Unity 5.6   
/// </summary>
public class EditorGUILayoutOtherExample : EditorWindow
{
    //PropertyField  GetControlRect  PrefixLabel
    [MenuItem("EditorGUILayout/EditorGUILayoutOtherExample")]
    static void Init()
    {
        EditorGUILayoutOtherExample window = (EditorGUILayoutOtherExample)EditorWindow.GetWindow(typeof(EditorGUILayoutOtherExample));
        window.Show();
    }

    #region  DropdownButton
    private string m_itemString = "";
    #endregion

    #region EnumMaskField
    public enum Example
    {
        Option_One = 1, //bits: 0000 0001
        Option_Two = 2,  //bits: 0000 0010
        Option_Three = 4     //bits: 0000 0100
    }
    Example staticFlagMask = 0;
    #endregion

    #region EnumPopup
    public enum OPTIONS
    {
        CUBE = 0,
        SPHERE = 1,
        PLANE = 2
    }
    public OPTIONS op;
    #endregion

    #region IntPopup
    int selectedSize = 1;
    string[] names = new string[] { "Normal", "Double", "Quadruple" };
    int[] sizes = new int[]{1, 2, 4};
    #endregion

    #region Popup
    public string[] options = new string[] { "Cube", "Sphere", "Plane" };
    public int index = 0;
    #endregion

    #region EnumMaskPopup
    public enum Options
    {
        CUBE = 0,
        SPHERE = 1,
        PLANE = 2
    }
    public Options m_options;
    #endregion

    #region InspectorTitlebar
    bool fold = true;
    bool fold2 = true;
    Transform selectedTransform;
    GameObject selectedGameObject;
    #endregion

    #region IntSlider
    int m_intSlider = 1;
    #endregion
    #region IntSlider
    float scale = 0.0f;
    #endregion

    #region MinMaxSlider
    float minVal = -10;
    float maxVal = 10;

    float minLimit = -20;
    float maxLimit = 20;
    #endregion

    #region PasswordField
    string m_passwordField = "";
    #endregion

    void OnGUI()
    {
        #region  DropdownButton
        //比較麻煩
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("DropdownButton:");
        if (EditorGUILayout.DropdownButton(new GUIContent(m_itemString), FocusType.Keyboard))
        {
            var alls = new string[4] { "A", "B", "C", "D" };
            GenericMenu _menu = new GenericMenu();
            foreach (var item in alls)
            {
                if (string.IsNullOrEmpty(item))
                {
                    continue;
                }
                //添加菜單
                _menu.AddItem(new GUIContent(item), m_itemString.Equals(item), OnValueSelected, item);
            }
            _menu.ShowAsContext();//顯示菜單
        }
        EditorGUILayout.EndHorizontal();
        #endregion

        #region EnumMaskField
        //可以多選
        staticFlagMask = (Example)EditorGUILayout.EnumMaskField("EnumMaskField:", staticFlagMask);
        #endregion

        #region EnumPopup
        op = (OPTIONS)EditorGUILayout.EnumPopup("EnumPopup:", op);
        #endregion

        #region IntPopup
        selectedSize = EditorGUILayout.IntPopup("IntPopup: ", selectedSize, names, sizes);
        #endregion

        #region Popup
        index = EditorGUILayout.Popup("Popup:",index, options);
        #endregion

        #region EnumMaskPopup
        m_options = (Options)EditorGUILayout.EnumMaskPopup("EnumMaskPopup:", m_options);
        #endregion

        #region HelpBox
        EditorGUILayout.HelpBox("HelpBox Error:", MessageType.Error);
        EditorGUILayout.HelpBox("HelpBox Info:", MessageType.Info);
        EditorGUILayout.HelpBox("HelpBox None:", MessageType.None);
        EditorGUILayout.HelpBox("HelpBox Warning:", MessageType.Warning);
        #endregion

        #region InspectorTitlebar
        selectedTransform = Selection.activeGameObject.transform;
        selectedGameObject = Selection.activeGameObject;
        fold = EditorGUILayout.InspectorTitlebar(fold, selectedTransform);
        fold2 = EditorGUILayout.InspectorTitlebar(fold2, selectedGameObject);
        #endregion

        #region IntSlider
        //包括最大最小值
        m_intSlider = EditorGUILayout.IntSlider("IntSlider:",m_intSlider, 1, 10);
        #endregion

        #region MinMaxSlider
        //取值范圍
        EditorGUILayout.LabelField("Min Val:", minVal.ToString());
        EditorGUILayout.LabelField("Max Val:", maxVal.ToString());
        EditorGUILayout.MinMaxSlider("MinMaxSlider",ref minVal, ref maxVal, minLimit, maxLimit);
        #endregion
        EditorGUILayout.Space();

        #region PasswordField
        m_passwordField = EditorGUILayout.PasswordField("PasswordField:", m_passwordField);
        EditorGUILayout.LabelField("輸入的文本:", m_passwordField);
        #endregion

        #region SelectableLabel
        //可以選擇,復制粘貼
        EditorGUILayout.SelectableLabel("SelectableLabel");
        #endregion

        scale = EditorGUILayout.Slider("Slider:",scale, 1, 100);
        //自適應高,不能自適應寬
        m_textArea = EditorGUILayout.TextArea(m_textArea);

        m_vector2 = EditorGUILayout.Vector2Field("Vector2:", m_vector2);
        m_vector3 = EditorGUILayout.Vector3Field("Vector3:", m_vector3);
        m_vector4 = EditorGUILayout.Vector4Field("Vector4:", m_vector4);

    }

    string m_textArea = "";
    Vector2 m_vector2;
    Vector3 m_vector3;
    Vector4 m_vector4;
    #region  DropdownButton
    void OnValueSelected(object value)
    {
        m_itemString = value.ToString();
    }
    #endregion
}

到這里,5.6版本的EditorGUILayout類所有方法都試了一遍了。


效果圖:
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 1.圖片瀏覽控件MWPhotoBrowser 實現(xiàn)了一個照片瀏覽器類似 iOS 自帶的相冊應用,可顯示來自手機的圖...
    萬忍閱讀 1,572評論 0 6
  • 轉眼又到了春光明媚的時候,花兒也開了,樹也綠了,小伙伴們的心也活了。清明小長假說來也就來了,估計很多小伙伴們已經在...
    大胖貓一只閱讀 384評論 0 0
  • 越過夏春秋冬的時光 穿梭風雨雷電的瘋狂 探望你 看大張旗鼓的 神山威嚴 岷江馳騁 看應接不暇的 白色云朵 雪蓮般怒...
    NFEX吳窮閱讀 219評論 0 0
  • 任性萌小樂之初識(三) 很多人都說過,夢境不可信。夢是人心之所向卻不是現(xiàn)實。但可曾想過,我們就在夢中,卻只是自我認...
    任性萌小樂閱讀 190評論 0 0
  • 方法1已驗證 方法1:找到所有的key然后remove掉/*** 清除所有的存儲本地的數據 */- (void)c...
    Eafy閱讀 2,035評論 0 0

友情鏈接更多精彩內容