toLua笔记

基本

cs 复制代码
LuaState luaState=new LuaState();
luaState.Start();
luaState.DoString("xxx");
luaState.DoFile("yyy.lua");
luaState.Require("zzz");//不要加.lua后缀
luaState.CheckTop();//检查解析器栈顶为空
luaState.Dispose();
luaState=null;

toLua生成报错

toLua生成代码是在Source/Generate生成的。

Wrap脚本关于ReadOnlySpan的报错

解决方法:

Unity使用ToLua插件报错 ReadOnlySpan<> & Span<> 解决方法_readonlyspan unity-CSDN博客 为防止上面文章失踪,这里把方法再抄一遍。

在ToLuaExport.cs的IsObsolete()函数开头加入以下代码:

cs 复制代码
if (mb is MethodBase method){
            ParameterInfo[] parameters = method.GetParameters();
            foreach (ParameterInfo parameter in parameters){
                Type parameterType = parameter.ParameterType;
                if (parameterType.IsGenericType){
                    Type genericTypeDefinition = parameterType.GetGenericTypeDefinition();
                    if (genericTypeDefinition == typeof(System.Span<>) || genericTypeDefinition == typeof(System.ReadOnlySpan<>))
                        return true;
                }
            }
        }

然后Clear wrap files,重新生成。

DelegateFactory脚本关于Application.MemoryUsageChangedCallback的报错

看报错信息,UnityEngine.Application.MemoryUsageChangedCallback的构造函数需要传入的是一个带in关键字的UnityEngine.ApplicationMemoryUsageChange,就把这的ref改成in。

下面两种情况是先构造一个他定义的UnityEngine_Application_MemoryUsageChangedCallback_Event类。上面已经知道UnityEngine.Application.MemoryUsageChangedCallback的构造函数想要一个带关键字in的输入参数,就把这个UnityEngine_Application_MemoryUsageChangedCallback_Event里的Call和CallWithSelf的输入参数关键字改成in。

cs 复制代码
		if(!flag)
		{
			UnityEngine_Application_MemoryUsageChangedCallback_Event target = new UnityEngine_Application_MemoryUsageChangedCallback_Event(func);
			UnityEngine.Application.MemoryUsageChangedCallback d = target.Call;
			target.method = d.Method;
			return d;
		}
		else
		{
			UnityEngine_Application_MemoryUsageChangedCallback_Event target = new UnityEngine_Application_MemoryUsageChangedCallback_Event(func, self);
			UnityEngine.Application.MemoryUsageChangedCallback d = target.CallWithSelf;
			target.method = d.Method;
			return d;
		}

new LuaState()报错:找不到tolua.dll

DllNotFoundException: tolua assembly

去报错的地方看:

然后在Assets里搜索tolua.dll,发现在Plugin里,把它复制到LuaDLL.cs的文件夹,也就是ToLua/Core。

ToLua自定义解析器报错:找不到文件或模块

原因:自定义Lua文件路径时需要把那一大堆ToLua自带的脚本拷到打算放Lua脚本的文件夹:这里Main是自己写的脚本,其他都是自带脚本。

在自己重写的ReadFile()里打印fileName,发现Lua文件夹里的脚本好像都被加载了一遍:

toLua自定义解析器

继承LuaFileUtils类,重写ReadFile()。使用时先new自己写的解析器类。

cs 复制代码
public class MyToLuaLoader:LuaFileUtils{
    public override byte[] ReadFile(string fileName){
        if(!fileName.EndsWith(".lua")){
            fileName+=".lua";
        }
        byte[] buffer=null;
        //从AB包加载
        string[] strings=fileName.Split('/');
        Debug.Log("从AB包加载"+fileName);
        TextAsset luaCode=MyABManager.Instance.
        LoadRes<TextAsset>("learntolua",strings[strings.Length-1]);
        if(luaCode){
            buffer=luaCode.bytes;
            Resources.UnloadAsset(luaCode);
        }
        if(buffer==null){
            Debug.Log("未找到"+fileName);
            //从Resourcs加载
            string path="Lua/"+fileName;
            TextAsset textAsset=Resources.Load<TextAsset>(path);
            Debug.Log("从Resources加载"+fileName);
            if(textAsset != null){
                buffer=textAsset.bytes;
                Resources.UnloadAsset(textAsset);
            }
        }
        
        return buffer;
    }
}

打AB包报错

解决方法:

自定义委托注册

在CustomSettings这里增加自定义的委托:

cs 复制代码
public static DelegateType[] customDelegateList = 
    {        
        _DT(typeof(Action)),                
        _DT(typeof(UnityEngine.Events.UnityAction)),
        _DT(typeof(System.Predicate<int>)),
        _DT(typeof(System.Action<int>)),
        _DT(typeof(System.Comparison<int>)),
        _DT(typeof(System.Func<int, int>)),
        _DT(typeof(MultiReturn)),
        _DT(typeof(MultiParam)),
    };

然后重新生成:

相关推荐
玖玥拾6 小时前
Lua 基础语法(六)xLua Lua 调用 C# 交互
开发语言·unity·c#·lua
滨哥GPT14 小时前
Codex改完代码为什么保存后页面还是不生效?热更新、文件监听与开发服务器排查
前端开发·热更新·开发调试·codex·hmr
玖玥拾1 天前
Lua 基础语法(五)Unity xLua基础配置与 C# 访问 Lua
开发语言·unity·c#·lua
玖玥拾2 天前
Lua 基础语法(二)
开发语言·unity·lua
玖玥拾2 天前
Lua 基础语法(三) 元表 metatable、元方法、模拟面向对象
开发语言·unity·lua
盟道科技3 天前
Redis+Lua 实现滑动窗口限流:从固定窗口踩坑到生产可用的完整方案
redis·junit·lua
Rain的Java大神之路6 天前
高并发下的热点账户余额扣减:Redis+Lua脚本实现无锁记账
java·spring boot·redis·后端·spring cloud·缓存·lua
qq_322762758 天前
一个接口从能用到稳定,中间差的到底是什么
服务器·开发语言·lua·接口·fastapi·请求
呆呆敲代码的小Y10 天前
【游戏开发】Lua 与 C# 交互原理解析
c#·lua·交互·热更新·lua与c#交互·游戏热更新
呆呆敲代码的小Y10 天前
Lua 上值(UpValue)
开发语言·junit·lua·lua上值·upvalue