Unity3D Odin Inspector Attribute回调的实现原理

Posted 暗光之痕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D Odin Inspector Attribute回调的实现原理相关的知识,希望对你有一定的参考价值。

环境:Unity2021.1.14 Odin3.0.4 语言:C#

面向:Odin进阶开发人员

问题

    在Odin Attribute的使用过程中,比如OnValueChanged,通常会传入一个字符串指定回调函数。

    通过字符串指定回调函数的方式是否能通过Odin的API主动调用?带着这样的疑问,我开始了探索。

情况

    使用OnValueChanged的Attribute可以指定回调函数,在age值改变的时候会触发OnAgeChanged:

public class Test1 : MonoBehaviour

	[OnValueChanged("OnAgeChanged")]
	public int age = 18;

	public void OnAgeChanged()
	
		Debug.LogError("我的年龄:" + age);
	

    我们可以看到回调函数指定是通过字符串形式传递的。

原理

    OnValueChangedAttributeDrawer中使用字符串进行创建。

    创建ActionResolver,ActionResolver.DoAction调用回调函数:

this.onChangeAction = ActionResolver.Get(this.Property, this.Attribute.Action);
if (this.Attribute.InvokeOnInitialize && !this.onChangeAction.HasError)

    this.onChangeAction.DoActionForAllSelectionIndices();

    ActionResolverCreator进行创建,在InitResolver时,通过内部保存的Resolvers数组逐个尝试创建ResolvedAction:

var array = ActionResolverCreators;
ResolvedAction action = null;
for (int i = 0; i < array.Length; i++)

    var resolverCreator = array[i].ResolverCreator;
    if (resolverCreator == null) break;
    action = resolverCreator.TryCreateAction(ref resolver.Context);
    if (action != null)
        break;

    3种Odin默认提供的字符串解析:

MethodPropertyActionResolverCreator

MethodReferenceActionResolverCreator

ExpressionActionResolverCreator

(需要自定义字符串解析可以通过打RegisterDefaultActionResolver标签)

    我们先看ExpressionActionResolverCreator类。

    ExpressionUtility.ParseExpression解析字符串,需要字符串以@开头:

var method = ExpressionUtility.ParseExpression(expression, isStatic, context.ParentType, parameterTypes, parameterNames, out compileError, true);
var parameterValues = new object[parameterCount + (isStatic ? 0 : 1)];
GetExpressionLambda(method, isStatic, context.ParentType.IsValueType, parameterValues);

    返回的函数最外层使用lambda表达式进行包裹,并将创建好的Delegate传入,最终使用Delegate. DynamicInvoke进行调用。

    Emitter.EmitMethod构建出方法,根据ASTParser. Parse()所解析出来的Token:

Tokenizer.SetExpressionString(expression);
Emitter.EmitMethod("$Expression(" + expression + ")_" + Guid.NewGuid().ToString(), Parser.Parse(), context, delegateType);

    MethodReferenceActionResolverCreator,通过Type反射出对应的MethodInfo进行调用:

MethodInfo method = GetCompatibleMethod(contextType, memberName, flags, ref context.NamedValues, ref argSetup, out errorMessage);
GetMethodInvoker(method as MethodInfo, argSetup, context.ParentType.IsValueType);

方法

    最简单的方式是创建ActionResolver直接进行解析,这也是AttributeDrawer调用的方式:

var action = ActionResolver.Get(PropertyTree.Create(this).RootProperty, "OnAgeChanged");
action.DoActionForAllSelectionIndices();

    这里底层使用的是Type进行的反射,我们可以传递@this.OnAgeChanged()主动调用表达式的方式:

var action = ActionResolver.Get(PropertyTree.Create(this).RootProperty, "@this.OnAgeChanged()");
action.DoActionForAllSelectionIndices();

    使用ExpressionUtility直接解析表达式,并调用:

var action = ExpressionUtility.ParseExpression("this.OnAgeChanged()", false, typeof(Test1), null, null, out var compileError, true);
if (!string.IsNullOrEmpty(compileError))

	Debug.LogError(compileError);

action.DynamicInvoke(this);

    以上的方法解析表达式大概会耗费0.0180秒的时间,反射方式耗费0.0010秒的时间,可以看到解析表达式是比较耗时的,所以在真实开发的时候推荐初始化时就先解析并缓存起来。

以上是关于Unity3D Odin Inspector Attribute回调的实现原理的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D Odin Inspector Attribute回调的实现原理

Unity3D Odin Inspector Attribute回调的实现原理

很好用的Unity编辑器扩展工具 Odin Inspector教程

Unity3D Editor Undo回退效果实现3 Odin相关

Unity3D Editor Undo回退效果实现3 Odin相关

在 Unity3d 中,如何将浮动窗口(例如光照贴图)添加到现有的一组选项卡(例如:Inspector)?