第一章编辑器开发基础第一节绘制编辑器元素_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);
    }



}

效果图

相关推荐
魔士于安3 天前
unity 丑陋哥布林 哥布林有humannoid 动画没 带动画
unity·游戏引擎·材质·贴图·模型
weixin_464078073 天前
ppt转为draw.io方法
编辑器
小贺儿开发4 天前
Unity 家居视频遥控(细节优化)
科技·unity·程序员·udp·视频·工具·通信
淡海水4 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
小小数媒成员4 天前
UGUI 性能优化(详细,全,深入)
开发语言·unity·游戏引擎
林川~015 天前
Unity新输入系统使用笔记
unity·input system
小贺儿开发5 天前
Unity 结合百度AI开放平台 手写文字识别
人工智能·科技·学习·unity·云服务·文字识别·手写文字
一木 之林5 天前
手搓家庭服务器:二手装机、Docker部署、端口映射与frp内网穿透
服务器·人工智能·git·编辑器
林川~016 天前
Unity 万能物理检测工具:射线检测 / 范围检测 / 层级过滤 / 编辑器可视化(可直接拿去用)
游戏·unity·c#·射线检测·通用工具
yi碗汤园6 天前
MQTT消息队列遥测传输协议
网络·网络协议·unity