7.EditorGUI对象关联各类型输入

  1. 7.EditorGUI-对象关联各类型输入
    1. 7.1 知识点
      1. 对象关联控件
      2. 各类型输入控件
    2. 7.2 知识点代码
      1. Lesson07_EditorGUI_对象关联各类型输入
      2. MyEditorGUILearnWindow

7.EditorGUI-对象关联各类型输入


7.1 知识点

对象关联控件

对象变量 = EditorGUILayout.ObjectField(对象变量, typeof(对象类型), 是否允许关联场景上对象资源) as 对象类型;

GameObject gameObj;

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

各类型输入控件

int变量 = EditorGUILayout.IntField(“Int输入框”, int变量);
long变量 = EditorGUILayout.LongField(“long输入框”, long变量);
float变量 = EditorGUILayout.FloatField(“Float 输入:”, float变量);
double变量 = EditorGUILayout.DoubleField(“double 输入:”, double变量);

string变量 = EditorGUILayout.TextField(“Text输入:”, string变量);
vector2变量 = EditorGUILayout.Vector2Field(“Vec2输入: “, vector2变量);
vector3变量 = EditorGUILayout.Vector3Field(“Vec3输入: “, vector3变量);
vector4变量 = EditorGUILayout.Vector4Field(“Vec4输入: “, vector4变量);
rect变量 = EditorGUILayout.RectField(“rect输入: “, rect变量);
bounds变量 = EditorGUILayout.BoundsField(“Bounds输入: “, bounds变量);
boundsInt变量 = EditorGUILayout.BoundsIntField(“Bounds输入: “, boundsInt变量);

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

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

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

Rect rect;
Bounds bounds;
BoundsInt boundsInt;

private void OnGUI()
{
    //各类型输入
    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());
}


7.2 知识点代码

Lesson07_EditorGUI_对象关联各类型输入

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

public class Lesson07_EditorGUI_对象关联各类型输入 : MonoBehaviour
{
    void Start()
    {
        #region 知识点一 对象关联控件

        //对象变量 = EditorGUILayout.ObjectField(对象变量, typeof(对象类型), 是否允许关联场景上对象资源) as 对象类型;

        #endregion

        #region 知识点二 各类型输入控件

        //int变量 = EditorGUILayout.IntField("Int输入框", int变量);
        //long变量 = EditorGUILayout.LongField("long输入框", long变量);
        //float变量 = EditorGUILayout.FloatField("Float 输入:", float变量);
        //double变量 = EditorGUILayout.DoubleField("double 输入:", double变量);

        //string变量 = EditorGUILayout.TextField("Text输入:", string变量);
        //vector2变量 = EditorGUILayout.Vector2Field("Vec2输入: ", vector2变量);
        //vector3变量 = EditorGUILayout.Vector3Field("Vec3输入: ", vector3变量);
        //vector4变量 = EditorGUILayout.Vector4Field("Vec4输入: ", vector4变量);
        //rect变量 = EditorGUILayout.RectField("rect输入: ", rect变量);
        //bounds变量 = EditorGUILayout.BoundsField("Bounds输入: ", bounds变量);
        //boundsInt变量 = EditorGUILayout.BoundsIntField("Bounds输入: ", boundsInt变量);

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

        #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

    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
    }
}


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

×

喜欢就点赞,疼爱就打赏