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



}

效果图

相关推荐
笨#小孩1 天前
中间件解析漏洞与编辑器漏洞:Web安全绕过的“捷径”
web安全·中间件·编辑器
玖玥拾2 天前
Unity3D RPG 入门项目(四)背包拖拽、物品悬浮提示、项目开发思维
3d·unity·游戏引擎
xcLeigh2 天前
Unity基础:Start与Update方法——Unity脚本生命周期初探
java·unity·游戏引擎·教程
郝学胜-神的一滴2 天前
[简化版 GAMES 104] 现代游戏引擎 05:游戏引擎世界构建核心机制深度解析
c++·程序人生·unity·游戏引擎·计算机图形学·opengl
LONGZETECH3 天前
无人机实训高成本痛点解法:虚拟仿真实现 70% 耗材损耗下降
大数据·算法·unity·架构·无人机
五仁烧饼3 天前
Unity Addressables 静默资源策略
游戏·unity·addressables
ue星空4 天前
【Godot】更换编辑器外观主题
编辑器·游戏引擎·godot
没事学点编程小知识4 天前
免费的在线EPUB编辑器:E-Ink
编辑器
fanfan_hongyun4 天前
unity 与cad 坐标系映射
unity·游戏引擎
神码编程4 天前
【Unity】 HTFramework框架(七十)MultiChoiceDropdown可多选的下拉菜单
unity·游戏引擎·ugui·dropdown·下拉菜单