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



}

效果图

相关推荐
weixin_424294674 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
山峰哥4 天前
吃透 SQL 优化:告别慢查询,解锁数据库高性能
服务器·数据库·sql·oracle·性能优化·编辑器
HoFunGames4 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
holeer4 天前
【V1.0】Typora 中的 HTML 支持|软件文档自翻译
前端·编辑器·html·typora·web·markdown·文档
硬汉嵌入式4 天前
Vim 9.2版本正式发布
编辑器·vim
wy3258643644 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs4 天前
着色器multi_compile笔记
unity·着色器
ECHO飞跃 0124 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
Unity游戏资源学习屋4 天前
【Unity UI资源包】GUI Pro - Casual Game 专为休闲手游打造的专业级UI资源包
ui·unity
冰凌糕4 天前
Unity3D Shader 顶点法线外扩实现描边效果
unity