基本
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()函數開頭加入以下代碼:
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。
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自己寫的解析器類。
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這里增加自定義的委托:
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)),};
然后重新生成: