第一章编辑器开发基础第一节绘制编辑器元素_4输入字段(4/7)

输入字段组

​​用途​​:数据输入和编辑

​​所属类​​:EditorGUILayout

​​调用位置​​:OnInspectorGUI()

​​类型支持​​:

方法 类型 说明
TextField string 普通文本
FloatField float 浮点
IntField int 整数
LongField long 大整数
PasswordField string 密码掩码
Vector3Field Vector3 三维向量

使用规范​​:

csharp 复制代码
stringValue = EditorGUILayout.TextField("字段名", stringValue);
floatValue = EditorGUILayout.FloatField("浮点数字段", floatValue);

// 其他类型类似

​​特殊功能​​:

向量字段自带分量展开

密码字段自动显示为*

数值类型自动验证输入格式

下面是具体例子和效果图

csharp 复制代码
using UnityEngine;
using UnityEditor;

/// <summary>
/// 创建自定义编辑器,作用于Example组件
/// </summary>
[CustomEditor(typeof(Example))]
public class ExampleEditor : Editor
{

    private string stringValue = "Hello world"; // 字符串字段
    private float floatValue = 10f;      // 浮点数字段
    private int intValue = 100;          // 整型字段
    private long longValue = 100;        // 长整型字段
    private string passwordValue = "123456"; // 密码字段


    // ================= 核心方法 - 绘制编辑器界面 =================
    public override void OnInspectorGUI()
    {
        // 1. 先绘制默认Inspector内容
        base.OnInspectorGUI();

        // 2. 依次绘制各种自定义控件

        InputField();  // 输入控件组 
    }



    // ================= 4. 输入字段控件组 =================
    private void InputField()
    {
        // 文本输入框
        stringValue = EditorGUILayout.TextField("String Value", stringValue);
        // 浮点数输入框
        floatValue = EditorGUILayout.FloatField("FloatValue", floatValue);
        // 整数输入框
        intValue = EditorGUILayout.IntField("IntValue", intValue);
        // 长整数输入框
        longValue = EditorGUILayout.LongField("LongValue", longValue);
        // 密码输入框(显示为*)
        passwordValue = EditorGUILayout.PasswordField("PasswordValue", passwordValue);
        // 2D向量字段
        vector2Value = EditorGUILayout.Vector2Field("Vector2Value", vector2Value);
        // 3D向量字段
        vector3Value = EditorGUILayout.Vector3Field("Vector3Value", vector3Value);
        // 4D向量字段
        vector4Value = EditorGUILayout.Vector4Field("Vector4Value", vector4Value);
    }



}

效果图

相关推荐
ellis19703 小时前
Unity插件SafeArea Helper适配异形屏详解
unity
nnsix4 小时前
Unity Physics.Raycast的 QueryTriggerInteraction枚举作用
unity·游戏引擎
地狱为王5 小时前
Cesium for Unity叠加行政区划线
unity·gis·cesium
esmap11 小时前
技术深度解析:ESMap引擎VS主流数字孪生竞品
人工智能·物联网·3d·编辑器·智慧城市·webgl
claider13 小时前
Vim User Manual 阅读笔记 usr_22.txt Finding the file to edit 多文件编辑浏览
笔记·编辑器·vim
小贺儿开发13 小时前
Unity3D 八大菜系连连看
游戏·unity·互动·传统文化
在路上看风景14 小时前
25. 屏幕像素和纹理像素不匹配
unity
ۓ明哲ڪ15 小时前
Unity功能——创建新脚本时自动添加自定义头注释
unity·游戏引擎
熬夜敲代码的小N15 小时前
Unity大场景卡顿“急救包”:从诊断到落地的全栈优化方案
java·unity·游戏引擎
qq_3975623115 小时前
使用vscode , 开发keil单片机工程 . (为了使用ai助手)
ide·vscode·编辑器