UnityEditor(编辑器扩展) 学习笔记 (一)常用属性 Attribute

一、Header

      用来给属性添加标题文字:
public class Test : MonoBehaviour
{
    [Header("用户名:")]
    public string UserName;
}

在这里插入图片描述
二、Tooltip

 显示属性标签的提示文字:
public class Test : MonoBehaviour
{
   [Tooltip("用户地址")]
   public string Address;
}

三、Space

 空间占位,两个属性快之间的间隔距离:
public class Test : MonoBehaviour
{
    [Tooltip("用户地址")]
    public string Address;
    [Space(50)]
    public int Age;
}

在这里插入图片描述
四、Range

给属性值添加一个区域滑块
public class Test : MonoBehaviour
{
    [Range(0, 1)]
    public float speed = 0.5f;
}

在这里插入图片描述
五、Multiline

多行文本
public class Test : MonoBehaviour
{
    [Multiline(5)]
    public string testStr;
}

在这里插入图片描述

六、SerializeField

序列化域,可以将私有变量序列化,让其可以显示在Inspector面板中
public class Test : MonoBehaviour
{
    [SerializeField]
    private string text;
}

七、NonSerialized

与SerializeField相对应NonSerialized不可被序列化,且不能在Inspector面板中显示
public class Test : MonoBehaviour
{
    [SerializeField]
    private string text;

    [NonSerialized]
    public int number;
}

在这里插入图片描述
八、HideInInspector

 隐藏属性在面板上的显示,切勿和NonSerialized混淆,HideInInspector只是隐藏属性在面板上的显示和序列化无关
public class Test : MonoBehaviour
{
    [HideInInspector]
    public int number;
}

在这里插入图片描述
九、TextArea

文本区域,区别于Multiline,TextArea超出固定行数后会出现滚动条
public class Test : MonoBehaviour
{
    [Multiline(5)]
    public string text1;

    // 两个参数分别是最小和最大行数,超出最大行数会出现滚动条
    [TextArea(1,5)]   
    public string text2;
}

在这里插入图片描述
十、HelpURL

帮助文档链接地址
[HelpURL("https://www.baidu.com/")]
public class Test : MonoBehaviour
{

}

在这里插入图片描述
点击图中红色标记的帮助文档按钮,会跳转到指定的"https://www.baidu.com/"链接地址

十一、AddComponentMenu

AddComponentMenu 属性允许用在Component菜单下面为脚本创建快捷添加菜单
using UnityEngine;

[AddComponentMenu("Add Test Script")]
public class Test : MonoBehaviour
{

}

在这里插入图片描述
十二、RequireComponent

自动添加你定义的组件(如果脚本已经挂在在物体上,在此时添加RequireComponent特性,需要重新挂在脚本才刷新)
用此特性添加的组件不能单独卸载,要卸载添加的组件,必须先卸载带有此特性的脚本
using UnityEngine;

[AddComponentMenu("Add Test Script")]
[RequireComponent(typeof(BoxCollider))]
public class Test : MonoBehaviour
{

}

在这里插入图片描述
十三、ContextMenu

  ContextMenu特性允许用户为脚本添加一个命令,用户可以通过在脚本上右键,或者点击后面的小齿轮在下拉菜单中
  找到自定义的命令方法,且是在非运行状态下执行该函数;
using UnityEngine;

public class Test : MonoBehaviour
{
    public string txt = "Hello...";

    [ContextMenu("SayHello")]
    void SayHello()
    {
        Debug.Log("输出文字:" + txt);
    }
}

在这里插入图片描述
十四、ContextMenuItem

ContextMenuItem和ContextMenu功能相似,只是ContextMenuItem作用于变量属性
using UnityEngine;

public class Test : MonoBehaviour
{
    // 第一个参数是在面板上显示的调用名称
    // 第二个参数是调用的方法
    [ContextMenuItem("SayHello", "SayHello")]
    public string txt = "Hello...";

    void SayHello()
    {
        Debug.Log("输出文字:" + txt);
    }
}

十五、DisallowMultipleComponent

DisallowMultipleComponent特性使得该脚本组件不能被重复添加
using UnityEngine;

[DisallowMultipleComponent]
public class Test : MonoBehaviour
{
    
}

在这里插入图片描述
十六、CreateAssetMenu

快速的创建ScriptableObject派生类的实例,并存储成以“.asset"结尾的文件
using UnityEngine;

[CreateAssetMenu(fileName = "UserInfo",menuName ="Create UserInfo")]
public class Test : ScriptableObject
{
    public string userName;
    public int age;
}

在这里插入图片描述

### Unity 编辑器扩展:自定义属性绘制 在 Unity 中,通过扩展编辑器功能可以实现自定义属性的绘制。这通常需要使用 `Editor` 类和相关的 API 来创建自定义检视面板或修改现有组件的显示方式[^1]。 以下是个完整的教程,展示如何在 Unity 编辑器扩展属性的功能和用法: #### 1. 创建自定义编辑器脚本 为了扩展特定类型的组件,可以使用 `[CustomEditor]` 属性来关联目标组件类与自定义编辑器类。例如,假设有个名为 `MyCustomComponent` 的组件,可以通过以下代码为其创建个自定义编辑器: ```csharp using UnityEditor; using UnityEngine; [CustomEditor(typeof(MyCustomComponent))] public class MyCustomComponentEditor : Editor { public override void OnInspectorGUI() { DrawDefaultInspector(); // 绘制默认的属性字段 // 自定义检视面板的内容 if (GUILayout.Button("Perform Custom Action")) { // 执行自定义操作 Debug.Log("Custom action performed!"); } } } ``` 上述代码会在 `MyCustomComponent` 的检视面板中添加个按钮,点击后会输出日志信息。 #### 2. 使用 `PropertyDrawer` 自定义属性绘制 如果需要为特定属性类型提供自定义绘制逻辑,可以使用 `PropertyDrawer` 类。以下是个示例,展示如何为带有 `[MyCustomAttribute]` 的字段创建自定义绘制逻辑: ```csharp using UnityEditor; using UnityEngine; // 定义个自定义属性 public class MyCustomAttribute : PropertyAttribute { } // 创建个 PropertyDrawer 来处理该属性 [CustomPropertyDrawer(typeof(MyCustomAttribute))] public class MyCustomPropertyDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // 设置标签文本 label = new GUIContent("Custom Label"); // 根据属性类型绘制不同的控件 switch (property.propertyType) { case SerializedPropertyType.Integer: property.intValue = EditorGUI.IntField(position, label, property.intValue); break; case SerializedPropertyType.Float: property.floatValue = EditorGUI.FloatField(position, label, property.floatValue); break; case SerializedPropertyType.String: property.stringValue = EditorGUI.TextField(position, label, property.stringValue); break; default: EditorGUI.PropertyField(position, property, label, true); break; } } } ``` 上述代码会为所有标记了 `[MyCustomAttribute]` 的字段提供自定义绘制逻辑[^3]。 #### 3. 使用 `EditorWindow` 创建独立窗口 虽然问题主要涉及属性扩展,但了解 `EditorWindow` 的使用也有助于理解编辑器扩展的整体机制。以下是个简单的示例,展示如何创建个独立的编辑器窗口: ```csharp using UnityEditor; using UnityEngine; public class MyCustomWindow : EditorWindow { [MenuItem("Window/My Custom Window")] public static void ShowWindow() { GetWindow<MyCustomWindow>("My Custom Window"); } private void OnGUI() { GUILayout.Label("This is a custom editor window!", EditorStyles.boldLabel); if (GUILayout.Button("Click Me")) { Debug.Log("Button clicked!"); } } } ``` 此代码会在菜单栏中添加个选项,用于打开个包含按钮的自定义窗口[^3]。 #### 4. 使用 `UIBuilder` 和 `VisualElement` 对于更现代的 UI 开发,Unity 提供了 `UIBuilder` 和 `VisualElement`,允许开发者以声明式的方式构建编辑器界面。以下是个简单的示例: ```csharp using UnityEditor.UIElements; using UnityEngine.UIElements; public class StatusBar : VisualElement { public new class UxmlFactory : UxmlFactory<StatusBar> { } public StatusBar() { var label = new Label("Hello, World!"); Add(label); } } ``` 此代码定义了个简单的 `StatusBar` 元素,并可通过 UXML 文件实例化[^4]。 --- ### 总结 通过使用 `Editor`、`PropertyDrawer` 和 `EditorWindow` 等类,开发者可以在 Unity 编辑器中实现高度定制化的功能。这些工具不仅限于修改现有组件的显示方式,还可以用于创建全新的编辑器界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值