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互推也请联系我…
联系我

Odin 将催化你的 Unity 工作流程,使它轻易地为你和整个团队构建功能强大并适用于高级用户的编辑器。 资源商店版本是针对过去 12 个月内收入或资金低于 20 万美元的实体或公司而设的。企业选项可以在这里获取。 ☄️ OdinInspector.com:了解 Odin Inspector 的一切。 ☄️ 手册:快速入门 ☄️ 支持:提交任何问题 ☄️ 路线图:下一步是什么? 在 2019 年 5 月 28 日之前购买了 Odin?从 https://odininspector.com/download 免费获得 Odin 源代码和验证器插件 Odin 能够完美部署到原有的工作流程中,无需费力的集成工作,让您能够序列化任何内容,并使用 80 多个全新检测器属性、无样板代码和更多功能来使用 Unity! 看看 2.1 版有什么更新! ☄️ 亮点 ☄️ - 轻松集成 - 纯编辑器模式 - Odin 编辑器窗口 - 输入验证 - 序列化任何对象 - 强大的列表 - 惊人的扩展性 - 调色板 - 字典 - 还有更多! · 轻松集成: Odin 很容易操作,并且不会打破你的已有工作流程。实际上你甚至不需要继承任何东西,也就是说你的现有编辑器将继续与 Odin 适用。 · 纯编辑器模式: 仅使用 Odin 的编辑器改进,完全禁用序列化。 · Odin 编辑器窗口: 您现在可以使用 Odin 来快速创建自定义的编辑器窗口,帮助组织您的项目和游戏数据。 · 输入验证: 通过允许您的开发者设置场景和输入验证来赋能您的整个团队,让 Unity使用对艺术家和开发者来说变得前所未有的容易。 · 序列化任何对象: Odin 使用我们评级很高的自定义序列化协议,让您既可以继承我们的 SerializedBehaviour、SerializedScriptableObject 等,也可以为您已有的类添加几行新代码,让可以序列化的任何内容都得到序列化。是的,甚至是多态类型! 在 2018.3 以上版本中,Odin 序列化的预制件由于嵌套的预制系统而被弃用。 · 功能强大的列表: 所有实现 Microsoft IList 接口的数组和列表均由我们强大的列表绘制器绘制; 拖放、插入和删除单个项目、多维数组、表、交叉列表,甚至是跨窗口的项目拖动、分页、嵌套列表绘制和更多! · 惊人的可扩展性: 强大而灵活的 API 让你轻易扩展和修改检查器的绘制方式。快速创建全新的属性组类型和自定义绘画器! • 更多内容! Odin 还添加了词典、自定义布局、资源列表、值下拉列表、嵌入式编辑器、调色板,甚至更多内容! ☄️平台支持 ☄️ 功能丰富且得到优化的 Odin 序列化器支持: - 桌面 - Android - iOS - WebGL - PlayStation - Xbox - Nintendo Switch - Oculus - 所有的 IL2CPP 平台 - UWP 仅得到 IL2CPP 后端的支持
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Unity打怪升级

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

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

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

打赏作者

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

抵扣说明:

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

余额充值