Unity 自定义属性面板字段名称

效果

💃想要更炫的效果可以参考官方的文档。

PropertyDrawer自定义Inspector面板显示

https://docs.unity3d.com/ScriptReference/PropertyDrawer.html

image

代码

新建 FieldNameAttribute.cs文件

using UnityEngine;

namespace Editor
{
    /// <summary>
    /// 字段名称标签
    /// 自定义 inspector 字段名称
    /// </summary>
    public class FieldNameAttribute : PropertyAttribute
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; private set; }
        /// <summary>
        /// 字段名称
        /// </summary>
        /// <param name="name">名称</param>
        public FieldNameAttribute(string name)
        {
            Name = name;
        }
    }
}

新建FieldNameAttributeDrawer.cs文件

using UnityEditor;
using UnityEngine;

namespace Editor
{
    /// <summary>
    /// 字段名称属性抽屉
    /// </summary>
    [CustomPropertyDrawer(typeof(FieldNameAttribute))]
    public class FieldNameAttributeDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.PropertyField(position, property, new GUIContent((attribute as FieldNameAttribute).Name));
        }
    }
}

引用

为方便引用,在Editor文件夹中,新建名为EditorAssembly Definition 文件。

image

在根目录下新建名为FarewellAssembly Definition 文件。

image

使用

引用Editor命名空间,在对应的字段上添加标签

using Editor;
using UnityEngine;
namespace Player
{ 
    public class ActorController : MonoBehaviour
    { 
        [FieldName("移动速度")]
        public float moveSpeed = 1.3f;
 
    }
}
posted @   林一怂儿  阅读(731)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示