Unity接入XLua问题汇总

一、This type must add to CSharpCallLua

如Unity中更新XLua时遇到

LuaException: c# exception:This type must add to CSharpCallLua: UnityEngine.Events.UnityAction<float>

LuaException: c# exception:System.InvalidCastException: This type must add to CSharpCallLua: System.Action<bool>

在XLua ExampleConfig.cs中添加如下配置,然后重新 Generate Code 即可

//C#静态调用Lua的配置(包括事件的原型),仅可以配delegate,interface
    [CSharpCallLua]
    public static List<Type> CSharpCallLua = new List<Type>() {
            typeof(Action),
            typeof(Func<double, double, double>),
            typeof(Action<string>),
            typeof(Action<double>),
            typeof(Action<bool>),
            typeof(Action<float>),
            typeof(UnityEngine.Events.UnityAction),
        };

二、Type cannot be null in method ‘GetComponent’

LuaException: c# exception:Type cannot be null.,stack:  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <00000000000000000000000000000000>:0 
      at XLua.OverloadMethodWrap.Call (System.IntPtr L) [0x00000] in <00000000000000000000000000000000>:0 
      at XLua.MethodWrap.Call (System.IntPtr L) [0x00000] in <00000000000000000000000000000000>:0 
      at XLua.StaticLuaCallbacks.FixCSFunction (System.IntPtr L) [0x00000] in <00000000000000000000000000000000>:0 
    stack traceback:
    	[C]: in method 'GetComponent'

出错行代码

closeButton:GetComponent("Button").onClick:AddListener(self:OnClose(true))

将通过 “Button” 字符串获取组件改为 通过 typeof(Button) 获取组件。修改为如下:

closeButton:GetComponent(typeof(Button)).onClick:AddListener(self:OnClose(true))
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值