Unity access XLua problem summary
一、This type must add to CSharpCallLua
As encountered when updating XLua in Unity
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>
Add the following configuration in XLua ExampleConfig.cs, and then re-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'
error line code
closeButton:GetComponent("Button").onClick:AddListener(self:OnClose(true))
Change getting component by "Button" string to get component by typeof(Button). Modify it as follows:
closeButton:GetComponent(typeof(Button)).onClick:AddListener(self:OnClose(true))