Odin Inspector教程 | (五)使用自定义特性处理器(Attribute Processors)

📂 Unity 开发资源汇总 | 插件 | 模型 | 源码

💓 欢迎访问 Unity 打怪升级大本营

💯 系列教程索引

📄(一)开始使用Odin Inspector
📄(二)使用 Attributes(特性)
📄(三)自定义编辑器窗口
📄(四)创建自定义 Drawers(抽屉)
📄(五)使用自定义特性处理器(Attribute Processors)
📄(六)Value解析器 和 Action解析器

Odin Inspector是Unity的一个插件,让您可以享受拥有强大,自定义和用户友好编辑器的所有工作流程优势,而无需编写任何自定义编辑器代码。

Odin包含许多功能,例如Static Inspector,Project Validation,Odin Editor Windows和开源的Odin Serializer,它允许您在需要多态对象结构时扩展Unity的序列化功能,或者希望在运行时序列化和反序列化数据。

在这里插入图片描述

Odin Inspector and Serializer 最新版 (0积分)免费下载

华丽的分割线


标题1

💯 使用自定义特性处理器

特性处理器为Odin绘制Inspector的方式提供了极大的自定义灵活性。您可以动态添加、编辑和删除特性,甚至完全改变Inspector的布局。

设想我们有一个名为MyProcessedClass的类和一个包含该类字段的MonoBehaviour

public class MyMonoBehaviour : MonoBehaviour
{
    public MyProcessedClass Processed;
}

[Serializable]
public class MyProcessedClass
{
    public ScaleMode Mode;
    public float Size;
}

现在我们将创建一个自定义的OdinAttributeProcessor。Odin提供了泛型和非泛型版本的OdinAttributeProcessor类,您可以根据需要选择最合适的版本。您可以期望在OdinDrawer类上找到与OdinDrawer类相同的泛型约束规则。

public class MyProcessedClassAttributeProcessor : OdinAttributeProcessor<MyProcessedClass>

首先,我们将重写ProcessSelfAttributes方法。Odin将为MyProcessedClass类型的任何字段或属性调用此方法。

在这里,我们将向属性的特性列表中添加两个特性。我们创建了AttributeListExtension类,并提供了一些实用方法来简化操作。

public override void ProcessSelfAttributes(InspectorProperty property, List<Attribute> attributes)
{
    attributes.Add(new InfoBoxAttribute("动态添加的特性!"));
    attributes.Add(new InlinePropertyAttribute());
}

让我们也重写并实现ProcessChildMemberAttributes方法。Odin将为MyProcessedClass属性的所有直接子属性调用此方法。

public override void ProcessChildMemberAttributes(
    InspectorProperty parentProperty,
    MemberInfo member,
    List<Attribute> attributes)
{
    // 这些特性将被添加到所有子元素上。
    attributes.Add(new HideLabelAttribute());
    attributes.Add(new BoxGroupAttribute("Box", showLabel: false));

    // 在这里我们分别向子属性添加特性。
    if (member.Name == "Mode")
    {
        attributes.Add(new EnumToggleButtonsAttribute());
    }
    {
        attributes.Add(new RangeAttribute(0, 5));
    }
}

值得一提的是,OdinAttributeProcessor仅适用于Inspector特性。此系统不会影响序列化,添加或删除序列化特性将不会产生任何效果。

Odin甚至在确定是否应该绘制成员时,会忽略诸如SerializeFieldOdinSerialize之类的特性列表中的属性。这些特性将直接从被检查的成员中获取,完全绕过特性处理器系统。


Attribute processor:

using UnityEngine;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector;
using System.Collections.Generic;
using System;
using System.Reflection;

public class MyProcessedClassAttributeProcessor : OdinAttributeProcessor<MyProcessedClass>
{
    public override void ProcessSelfAttributes(InspectorProperty property, List<Attribute> attributes)
    {
        attributes.Add(new InfoBoxAttribute("Dynamically added attributes!"));
        attributes.Add(new InlinePropertyAttribute());
    }

    public override void ProcessChildMemberAttributes(
        InspectorProperty parentProperty,
        MemberInfo member,
        List<Attribute> attributes)
    {
        // These attributes will be added to all of the child elements.
        attributes.Add(new HideLabelAttribute());
        attributes.Add(new BoxGroupAttribute("Box", showLabel: false));

        // Here we add attributes to child properties respectively.
        if (member.Name == "Mode")
        {
            attributes.Add(new EnumToggleButtonsAttribute());
        }
        else if (member.Name == "Size")
        {
            attributes.Add(new RangeAttribute(0, 5));
        }
    }
}

标题2

💯 Odin 下载地址

Odin Inspector and Serializer 最新版 (0积分)免费下载


《Odin Inspector 系列教程》文章索引:
📄(一)开始使用Odin Inspector
📄(二)使用 Attributes(特性)
📄(三)自定义编辑器窗口
📄(四)创建自定义 Drawers(抽屉)
📄(五)使用自定义特性处理器(Attribute Processors)
📄(六)Value解析器 和 Action解析器


TheEnd


跳跃

📂 Unity 开发资源汇总 | 插件 | 模型 | 源码

💓 欢迎访问 Unity 打怪升级大本营

🍉🍉🍉 如果觉得这篇文对你有帮助的话,请点个赞👍、收藏⭐️下吧,非常感谢! 💕💕💕
关注我

博主头像
【博主简介】:10年以上软件开发经验,精通 C语言C++C#Java 等开发语言,开发过大型 Android 项目,现主要自主开发经营 休闲益智类小游戏

【粉丝福利】:博主收藏了大量游戏开发资源和素材。这些资源经过博主多年整理沉淀,现筛选一批精品资源,分享给大家学习研究。

Unity打怪军团 广招天下勇士加入 Unity学习互助小组 需要进群的同学联系我,互3互推也请联系我…
联系我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity打怪升级

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值