Fork me on GitHub

xlua tips

1. LuaCallCSharp

  a. 标签(该方式方便,但在il2cpp下会增加不少的代码量,不建议使用

[LuaCallCSharp]
public class A
{
}

  b. 静态列表(有时我们无法直接给一个类型打标签,比如系统api,没源码的库,或者实例化的泛化类型,这时你可以在一个静态类里声明一个静态字段,该字段的类型除BlackListAdditionalProperties之外只要实现了IEnumerable<Type>就可以了(这两个例外后面具体会说),然后为这字段加上标签:    

[LuaCallCSharp]
public static List<Type> mymodule_lua_call_cs_list = new List<Type>()
{
    typeof(GameObject),
    typeof(Dictionary<string, int>),
};

  这个字段需要放到一个静态类里头,建议放到Editor目录。

  c. 动态列表  (声明一个静态属性,打上相应的标签即可)

[LuaCallCSharp]
public static List<Type> LuaCallCSharp
{
    get
    {
        return new List<Type>()
        {
              typeof(WaitForSeconds),
              typeof(WWW)
         };
     }
}

  or

[Hotfix]
    public static List<Type> by_property
    {
        get
        {
            return (from type in Assembly.GetExecutingAssembly().GetTypes()
                    where type.Namespace == "XXXX"
                    select type).ToList();
        }
}

  Getter是代码,你可以实现很多效果,比如按名字空间配置,按程序集配置等等。

这个属性需要放到一个静态类里头,建议放到Editor目录。

2. XLua.ReflectionUse

  反射访问除了性能不佳之外,在il2cpp下还有可能因为代码剪裁而导致无法访问,可以通过ReflectionUse标签来避免。一个C#类型类型加了这个配置,xLua会生成link.xml阻止il2cpp的代码剪裁。

3. ExtensionAttribute

4. XLua.CSharpCallLua

  如果希望把一个lua函数适配到一个C# delegate(一类是C#侧各种回调:UI事件,delegate参数,比如List<T>:ForEach;另外一类场景是通过LuaTableGet函数指明一个lua函数绑定到一个delegate)。或者把一个lua table适配到一个C#   interface,该delegate或者interface需要加上该配置。

5. XLua.GCOptimize

 

 

 

 

 

 

posted on 2020-03-31 09:58  pengyingh  阅读(367)  评论(0)    收藏  举报

导航