💯 系列教程索引
📄(一)开始使用Odin Inspector
📄(二)使用 Attributes(特性)
📄(三)自定义编辑器窗口
📄(四)创建自定义 Drawers(抽屉)
📄(五)使用自定义特性处理器(Attribute Processors)
📄(六)Value解析器 和 Action解析器
Odin Inspector是Unity的一个插件,让您可以享受拥有强大,自定义和用户友好编辑器的所有工作流程优势,而无需编写任何自定义编辑器代码。
Odin包含许多功能,例如Static Inspector,Project Validation,Odin Editor Windows和开源的Odin Serializer,它允许您在需要多态对象结构时扩展Unity的序列化功能,或者希望在运行时序列化和反序列化数据。
💯 使用自定义特性处理器
特性处理器为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甚至在确定是否应该绘制成员时,会忽略诸如SerializeField
和OdinSerialize
之类的特性列表中的属性。这些特性将直接从被检查的成员中获取,完全绕过特性处理器系统。
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));
}
}
}
💯 Odin 下载地址
《Odin Inspector 系列教程》文章索引:
📄(一)开始使用Odin Inspector
📄(二)使用 Attributes(特性)
📄(三)自定义编辑器窗口
📄(四)创建自定义 Drawers(抽屉)
📄(五)使用自定义特性处理器(Attribute Processors)
📄(六)Value解析器 和 Action解析器
🍉🍉🍉 如果觉得这篇文对你有帮助的话,请点个赞👍、收藏⭐️下吧,非常感谢! 💕💕💕
【博主简介】:10年以上软件开发经验,精通C语言
、C++
、C#
、Java
等开发语言,开发过大型 Android 项目,现主要自主开发经营 休闲益智类小游戏。
【粉丝福利】:博主收藏了大量游戏开发资源和素材。这些资源经过博主多年整理沉淀,现筛选一批精品资源,分享给大家学习研究。
Unity打怪军团 广招天下勇士加入 Unity学习互助小组 需要进群的同学联系我,互3互推也请联系我…