zl程序教程

您现在的位置是:首页 >  其它

当前栏目

Unity Puerts 过滤器

Unity 过滤器
2023-09-27 14:22:11 时间

可以看看这个链接 官方提供的黑名单
https://github.com/chexiongsheng/puerts_unity_demo/blob/master/projects/1_Start_Template/Assets/Samples/Editor/01_WrapperGenerate/PuertsFilter.cs

[Filter]
static bool FilterMethods(System.Reflection.MemberInfo mb)
{    
	if (mb.DeclaringType == typeof(GameUtils))
	{
	    return true;
	}
	return false;
}

如果返回true 就不会生成Wrapper里的代码了

[Filter]
static Puerts.Editor.Generator.BindingMode FilterMethods2(System.Reflection.MemberInfo memberInfo)
{
	return Puerts.Editor.Generator.BindingMode.DontBinding; // 不生成StaticWrapper,且JS调用时获取对应字段会得到undefined。     
    return Puerts.Editor.Generator.BindingMode.FastBinding; // 等同于前面return false的情况
}
internal static BindingMode getBindingMode(MemberInfo mbi) 
{
    BindingMode strictestMode = BindingMode.FastBinding;
    if (filters != null && filters.Count > 0)
    {
        foreach (var filter in filters)
        {
            BindingMode mode = BindingMode.FastBinding;
            if (filter.ReturnType == typeof(bool))
            {
                if ((bool)filter.Invoke(null, new object[] { mbi })) 
                {
                    mode = BindingMode.LazyBinding;
                }
            }
            else if (filter.ReturnType == typeof(BindingMode))
            {
                mode = (BindingMode)filter.Invoke(null, new object[] { mbi });
            }
            strictestMode = strictestMode < mode ? mode : strictestMode;
        }
    }
    return strictestMode;
}

看源码就会发现
默认是 BindingMode.FastBinding 也就是说返回 false 就是 FastBinding
如果返回值是 bool 值 并且 返回 true 那么就会变成 BindingMode.LazyBinding
设计俩接口 是为了区分功能
不过你可以看到 他是for循环调用的
后面的 Filter 会覆盖前面的结果 要注意这一点

有的项目,库特别大,一进游戏初始化一些内容的时候特别慢,查出来可能是这个导致的。
可以试试改成LazyBinding 模式。