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



}

效果图

相关推荐
在路上看风景26 分钟前
15. 纹理尺寸是4的倍数
unity
AT~3 小时前
unity 使用Socket和protobuf实现网络连接
unity·游戏引擎
有梦想的鱼4 小时前
vscode letax编译中英文论文(傻瓜式、一分钟)
ide·vscode·编辑器
iCora9 小时前
vim入门
linux·编辑器·vim
怣疯knight9 小时前
Cocos creator判断节点是否能用的方法
unity·cocos2d
tealcwu9 小时前
Google Play的Keystore不可用时的解决方法
unity
呼呼突突10 小时前
Unity使用TouchSocket的RPC
unity·rpc·游戏引擎
qq 180809511 天前
从零构建一个多目标多传感器融合跟踪器
unity
平行云1 天前
实时云渲染支持在网页上运行UE5开发的3A大作Lyra项目
unity·云原生·ue5·webgl·虚拟现实·实时云渲染·像素流送
鹏飞于天1 天前
Shader compiler initialization error: Failed to read D3DCompiler DLL file
unity