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;
 
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM