【Unity编辑器扩展基础】、GUILayout

49 篇文章 1 订阅
15 篇文章 9 订阅

小结:

1、TextArea、TextField等文本输入框不能复制,TextArea可以换行,可以自适应宽。

2、参数可以传GUIContent的都可以显示图片或文字。

3、 GUILayout.Window需要调用BeginWindows、 EndWindows才能显示窗口。

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

/// <summary>
/// Unity 5.6
/// GUILayoutOption参数:
/// GUILayout.Width, GUILayout.Height, GUILayout.MinWidth, GUILayout.MaxWidth, GUILayout.MinHeight, GUILayout.MaxHeight, GUILayout.ExpandWidth, GUILayout.ExpandHeight
/// </summary>
public class GUILayoutControlExample : EditorWindow
{

    [MenuItem("Examples/GUILayoutControlExample")]
    static void Init()
    {
        GUILayoutControlExample window = (GUILayoutControlExample)EditorWindow.GetWindow(typeof(GUILayoutControlExample));
        window.Show();
    }

    void OnEnable()
    {
        #region Box
        m_boxTexture = Resources.Load("Textures/EquipIcon/12001") as Texture;
        m_boxLable = "GUILayout的Box控件!改变窗口大小后文本可以自适应";
        #endregion
    }
    // Box
    Texture m_boxTexture;
    string m_boxLable ;
    
    //Label
    string m_lableText = "GUILayout的Label控件!改变窗口大小后文本不能自适应";

    //PasswordField
    string m_passwordFieldText = "";

    //  SelectionGrid
    int selGridInt = 0;
    string[] selStrings = new string[] { "radio1", "radio2", "radio3", "radio4" };
    int selectIndex = -1;

    //BeginScrollView
    public Vector2 scrollPosition;
    public string longString = "This is a long-ish string";
    //BeginArea
    Rect area = new Rect(500, 20, 200, 200);
    //GUILayout.Window
    Rect windowRect = new Rect(200, 50, 200, 50);

    //Scrollbar
    public float vSbarValue;
    public float hSbarValue;
    //Slider
    public float vSliderValue;
    public float hSliderValue;
    //TextArea
    string m_textAtea = "TextArea 文本框";
    //TextField
    string m_textField = "TextField 输入框";
    //Toggle
    bool m_toggle = true;
    //ToolBar
    int toolbarInt = 0;
    int toolbarIndex = -1;
    string[] toolbarStrings = new string[] { "Toolbar1", "Toolbar2", "Toolbar3" };

    void OnGUI()
    {
        //参数可以传GUIContent的都可以显示图片或文字
        #region Box 
        //显示的图片大小就是资源大小,文本可以自适应
        if (m_boxTexture != null)
        {
            GUILayout.Box(m_boxTexture);
           //GUILayout.Box(new GUIContent(m_boxLable, m_boxTexture));
        }
       
        GUILayout.Box(m_boxLable);
        #endregion

        #region Button 自动布局按钮
        //Button:鼠标按下抬起是一次有效点击,按下抬起才返回True。
        //RepeatButton:鼠标按下后会一直返回True。(如果没有调用Repaint函数,按下、抬起返回True)
        if (GUILayout.Button("Button"))
        {
            Debug.Log("点击按钮");
            Repaint();
        }
        if (GUILayout.RepeatButton("RepeatButton"))
        {
            Debug.Log("点击RepeatButton按钮");
            Repaint();
        }
        #endregion

        //FlexibleSpace  自适应间距
        GUILayout.FlexibleSpace();

        GUILayout.Label("Lable:"+ m_lableText);
        //PasswordField  传入的string需要先初始化一个默认值,不然会报空异常
        m_passwordFieldText = GUILayout.PasswordField(m_passwordFieldText, '*');
        GUI.color = Color.red;
        GUILayout.Label("PasswordField:" + m_passwordFieldText);
        GUI.color = Color.white;
        #region  SelectionGrid
        //感觉就跟UGUI的GridLayoutGroup一样
        selGridInt = GUILayout.SelectionGrid(selGridInt, selStrings, 2);
        if (selectIndex!= selGridInt)
        {
            Debug.Log("选择了: " + selStrings[selGridInt]);
            selectIndex = selGridInt;
        }

        #endregion
        GUILayout.Space(20);
        //可以自适应宽,可以换行
        m_textAtea = GUILayout.TextArea(m_textAtea);
        //不能自适应,只有一行
        m_textField = GUILayout.TextField(m_textField);
        m_toggle = GUILayout.Toggle(m_toggle, "Toggle:");

        toolbarInt = GUILayout.Toolbar(toolbarInt, toolbarStrings);
        if (toolbarIndex!=toolbarInt)
        {
            Debug.Log("点击了:" + toolbarStrings[toolbarInt]);
            toolbarIndex = toolbarInt;
        }
        //滚动条
        vSbarValue = GUILayout.VerticalScrollbar(vSbarValue, 1.0F, 10.0F, 0.0F);
        hSbarValue = GUILayout.HorizontalScrollbar(hSbarValue, 1.0F, 0.0F, 10.0F);
        //滑动条
        vSliderValue = GUILayout.VerticalSlider(vSliderValue, 10.0F, 0.0F);
        hSliderValue = GUILayout.HorizontalSlider(hSliderValue, 0.0F, 10.0F);



        //创建一个GUI的区域,区域内的控件只受区域影响
        GUILayout.BeginArea(area);
        GUILayout.BeginHorizontal();

        #region Vertical A
        GUILayout.BeginVertical();
        GUILayout.Box("Text1");
        //控件与控件之间空隙一个距离
        GUILayout.Space(20f);
        GUILayout.Box("Text1");
        GUILayout.Space(20f);
        GUILayout.Button("Button");
        GUILayout.EndVertical();
        #endregion

        #region Vertical B
        GUILayout.BeginVertical();
        GUILayout.Box("Text3");
        //自动测算空隙距离
        GUILayout.FlexibleSpace();
        GUILayout.Box("Text4");
        GUILayout.FlexibleSpace();
        GUILayout.Button("Button");
        GUILayout.EndVertical();

        #endregion

        GUILayout.EndHorizontal();
        GUILayout.EndArea();
        //
        scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(300), GUILayout.Height(100));
        GUILayout.Label(longString);
        if (GUILayout.Button("Clear"))
            longString = "";

        //
        GUILayout.EndScrollView();
        if (GUILayout.Button("Add More Text"))
            longString += "\nHere is another line";

        //
        BeginWindows();
        windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "GUILayout的Window");
        EndWindows();
      
    }

    void DoMyWindow(int windowID)
    {
        if (GUILayout.Button("Button"))
            Debug.Log("点击按钮!");
        GUI.DragWindow();
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity编辑器扩展是指通过自定义脚本来扩展Unity编辑器的功能和界面。在Unity中,可以使用编辑器扩展来创建自定义的面板、工具栏、菜单项等,以便更好地支持开发者的工作流程和需求。 在编辑器扩展中,有几个重要的类需要了解。首先是Editor类,它是所有自定义编辑器的基类,通过继承Editor类可以创建自定义的Inspector面板,用于编辑特定组件的属性。其次是EditorWindow类,它是创建自定义窗口的基类,可以用于创建独立的编辑器工具窗口。然后是EditorGUI和EditorGUILayout类,它们用于在自定义面板中创建UI元素,如按钮、文本框、滑动条等。这两个类的区别在于EditorGUI需要手动计算UI元素的位置和大小,而EditorGUILayout会自动进行排版和计算坐标。此外,还有一些其他常用的类,如GUIGUILayout、EditorGUIUtility、EditorUtility、AssetDatabase、SceneView和Handles等,它们提供了各种用于编辑器扩展的功能和工具。 使用编辑器扩展可以提高开发效率,使开发者能够更方便地进行编辑器自定义和工作流程优化。通过自定义面板和工具,可以根据项目需求添加额外的功能和工具,提供更好的用户体验和开发环境。同时,编辑器扩展也为开发者提供了更多的自由度和创造力,可以根据自己的需求进行定制和扩展。 总结起来,Unity编辑器扩展是通过自定义脚本来扩展Unity编辑器的功能和界面,可以创建自定义的面板、工具栏、菜单项等,提供更好的开发环境和用户体验。常用的类包括Editor、EditorWindow、EditorGUI、EditorGUILayoutGUIGUILayout、EditorGUIUtility、EditorUtility、AssetDatabase、SceneView和Handles等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值