9.EditorGUI开关开关组

  1. 9.EditorGUI-开关开关组
    1. 9.1 知识点
      1. 开关控件
      2. 开关组控件
    2. 9.2 知识点代码
      1. Lesson09_EditorGUI_开关开关组
      2. MyEditorGUILearnWindow

9.EditorGUI-开关开关组


9.1 知识点

开关控件

bool变量 = EditorGUILayout.Toggle(“普通开关”, bool变量);
bool变量 = EditorGUILayout.ToggleLeft(“开关在左侧”, bool变量);

bool isTog;
bool isTogLeft;

private void OnGUI()
{
    // 开关控件
    isTog = EditorGUILayout.Toggle("开关控件", isTog);
    isTogLeft = EditorGUILayout.ToggleLeft("左侧开关", isTogLeft);
}

开关组控件

bool变量 = EditorGUILayout.BeginToggleGroup(“开关组”, bool变量);
其他控件绘制
EditorGUILayout.EndToggleGroup();

bool isTogGroup;

private void OnGUI()
{
    // 开关组控件
    isTogGroup = EditorGUILayout.BeginToggleGroup("开关组控件", isTogGroup);
    
    // 开关控件
    isTog = EditorGUILayout.Toggle("开关控件", isTog);
    isTogLeft = EditorGUILayout.ToggleLeft("左侧开关", isTogLeft);
    
    EditorGUILayout.EndToggleGroup();
}



9.2 知识点代码

Lesson09_EditorGUI_开关开关组

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

public class Lesson09_EditorGUI_开关开关组 : MonoBehaviour
{
    void Start()
    {
        #region 知识点一 开关控件

        //bool变量 = EditorGUILayout.Toggle("普通开关", bool变量);

        //bool变量 = EditorGUILayout.ToggleLeft("开关在左侧", bool变量);

        #endregion

        #region 知识点二 开关组控件

        //bool变量 = EditorGUILayout.BeginToggleGroup("开关组", bool变量);
        //其他控件绘制
        //EditorGUILayout.EndToggleGroup();

        #endregion
    }
}

MyEditorGUILearnWindow

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


public enum E_TestType
{
    One = 1,
    Two = 2,
    Three = 4,
    One_and_Two = 1 | 2,
}

public class MyEditorGUILearnWindow : EditorWindow
{
    [MenuItem("编辑器拓展教程/MyEditorGUILearnWindow")]
    private static void OpenMyEditorGUILearnWindow()
    {
        MyEditorGUILearnWindow win = EditorWindow.GetWindow<MyEditorGUILearnWindow>("EditorGUI知识讲解窗口");
        //win.titleContent = new GUIContent("EditorGUI知识讲解窗口");
        win.Show();
    }

    #region Lesson05_EditorGUI_文本层级标签颜色

    int layer;
    string tag;
    Color color;

    #endregion

    #region Lesson06_EditorGUI_枚举整数选择按下按钮

    E_TestType type;
    E_TestType type2;

    string[] strs = { "选择123", "选择234", "选择345" };
    int[] ints = { 123, 234, 345 };
    int num = 0;

    #endregion

    #region Lesson07_EditorGUI_对象关联各类型输入

    GameObject gameObj;


    int i;
    int i2;
    float f;
    double d;
    long l;

    string str;
    Vector2 vec2;
    Vector3 vec3;
    Vector4 vec4;

    Rect rect;
    Bounds bounds;
    BoundsInt boundsInt;

    #endregion

    #region Lesson08_EditorGUI_折叠折叠组

    bool isHide;
    bool isHideGroup;

    #endregion

    #region Lesson09_EditorGUI_开关开关组

    bool isTog;
    bool isTogLeft;
    bool isTogGroup;

    #endregion

    
    private void OnGUI()
    {
        //窗口中的控件相关绘制 逻辑处理相关的内容
        //EditorGUI相关的控件 同样还是需要在OnGUI当中进行实现 才能被显示出来

        #region Lesson05_EditorGUI_文本层级标签颜色

        //文本控件
        EditorGUILayout.LabelField("文本标题", "测试内容");
        EditorGUILayout.LabelField("文本内容");

        //层级标签控件
        // layer = EditorGUILayout.LayerField(layer);//这样可能别人看不明白是选择什么的。建议加个字符串提示
        layer = EditorGUILayout.LayerField("层级选择", layer);
        // tag = EditorGUILayout.TagField(tag);//这样可能别人看不明白是选择什么的。建议加个字符串提示
        tag = EditorGUILayout.TagField("标签选择", tag);

        //颜色获取控件
        color = EditorGUILayout.ColorField(new GUIContent("自定义颜色获取"),
            color, true, true, true);

        #endregion

        #region Lesson06_EditorGUI_枚举整数选择按下按钮

        //枚举选择
        type = (E_TestType)EditorGUILayout.EnumPopup("枚举选择", type);

        type2 = (E_TestType)EditorGUILayout.EnumFlagsField("枚举多选", type2);

        //整数选择控件
        //返回值 其实是整数数组当中的某一个值
        num = EditorGUILayout.IntPopup("整数单选框", num, strs, ints);
        EditorGUILayout.LabelField(num.ToString()); //显示返回的值是什么

        //按下就响应的按钮
        if (EditorGUILayout.DropdownButton(new GUIContent("按钮上文字"), FocusType.Passive))
            Debug.Log("按下就响应");

        #endregion

        #region Lesson07_EditorGUI_对象关联各类型输入

        //对象关联
        gameObj = EditorGUILayout.ObjectField("关联资源对象", gameObj, typeof(GameObject), false) as GameObject;


        //各类型输入
        i = EditorGUILayout.IntField("Int输入框", i);
        EditorGUILayout.LabelField(i.ToString());
        l = EditorGUILayout.LongField("long输入框", l);
        f = EditorGUILayout.FloatField("Float 输入:", f);
        d = EditorGUILayout.DoubleField("double 输入:", d);

        str = EditorGUILayout.TextField("Text输入:", str);
        vec2 = EditorGUILayout.Vector2Field("Vec2输入: ", vec2);
        vec3 = EditorGUILayout.Vector3Field("Vec3输入: ", vec3);
        vec4 = EditorGUILayout.Vector4Field("Vec4输入: ", vec4);

        rect = EditorGUILayout.RectField("rect输入: ", rect);
        bounds = EditorGUILayout.BoundsField("Bounds输入: ", bounds);
        boundsInt = EditorGUILayout.BoundsIntField("Bounds输入: ", boundsInt);

        //注意:EditorGUILayout中还有一些Delayed开头的输入控件
        //     他们和普通输入控件最主要的区别是:在用户按 Enter 键或将焦点从字段移开之前,返回值不会更改
        i2 = EditorGUILayout.DelayedIntField("Int输入框", i2);
        EditorGUILayout.LabelField(i2.ToString());

        #endregion

        #region Lesson08_EditorGUI_折叠折叠组

        //折叠
        isHide = EditorGUILayout.Foldout(isHide, "折叠控件", false);
        //第二个参数为true代表点击整体都能展开收起折叠 为false只能点击前面箭头展开收起折叠

        if (isHide)
        {
            EditorGUILayout.LabelField("折叠文本内容");
        }


        //折叠组
        isHideGroup = EditorGUILayout.BeginFoldoutHeaderGroup(isHideGroup, "折叠组控件");
        //和折叠的主要区别是折叠组会高亮 且 折叠组需要结束组

        if (isHideGroup)
        {
            EditorGUILayout.LabelField("折叠组文本内容");
        }

        EditorGUILayout.EndFoldoutHeaderGroup();

        #endregion
        
        #region Lesson09_EditorGUI_开关开关组
        
        //开关组控件
        isTogGroup = EditorGUILayout.BeginToggleGroup("开关组控件", isTogGroup);

        
        //开关控件
        isTog = EditorGUILayout.Toggle("开关控件", isTog);
        isTogLeft = EditorGUILayout.ToggleLeft("左侧开关", isTogLeft);
        
        
        EditorGUILayout.EndToggleGroup();
        
        #endregion
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏