对于lua的简单调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| using System.Collections; using System.Collections.Generic; using UnityEngine; using XLua;
public class CsCallLua : MonoBehaviour { void Start() { LuaEnv env = new LuaEnv(); env.DoString("print('Hello World')","CsCallLua"); env.DoString("require('Main')"); env.Tick(); env.Dispose(); }
void Update() { } }
|
文件加载重定向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using XLua;
public class CsCallLua2 : MonoBehaviour { void Start() { LuaEnv env = new LuaEnv();
env.AddLoader(MyCusomLoader); env.DoString("require('Main')"); }
void Update() { }
private byte[] MyCusomLoader(ref string filePath) { string path = Application.dataPath + "/Lua/" + filePath + ".lua"; Debug.Log(path); if (File.Exists(path)) { return File.ReadAllBytes(path); } else { Debug.Log("重定向失败,文件名为"+ filePath); } return null; } }
|
解析器管理器
luamgr:构建一个唯一的lua解析器luaenv提供很多外部使用的方法操作lua相关的内容
ab包加载lua脚本重定向
注意:平时不用,只有测试热更新或者最终打包时才会使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using XLua;
public class LuaMgr : BaseManager<LuaMgr> { private LuaEnv luaEnv;
public LuaTable Global { get { return luaEnv.Global; } }
public void Init() { if (luaEnv != null) return; luaEnv = new LuaEnv(); luaEnv.AddLoader(MyCustomLoader); luaEnv.AddLoader(MyCustomABLoader); }
private byte[] MyCustomLoader(ref string filePath) { string path = Application.dataPath + "/Lua/" + filePath + ".lua";
if (File.Exists(path)) { return File.ReadAllBytes(path); } else { Debug.Log("MyCustomLoader重定向失败,文件名为" + filePath); }
return null; }
private byte[] MyCustomABLoader(ref string filePath) {
TextAsset lua = ABMgr.instance.LoadRes<TextAsset>("lua", filePath + ".lua"); if (lua != null) return lua.bytes; else Debug.Log("MyCustomABLoader重定向失败,文件名为:" + filePath);
return null; }
public void DoLuaFile(string fileName) { string str = string.Format("require('{0}')", fileName); DoString(str); }
public void DoString(string str) { if(luaEnv == null) { Debug.Log("解析器为初始化"); return; } luaEnv.DoString(str); }
public void Tick() { if (luaEnv == null) { Debug.Log("解析器为初始化"); return; } luaEnv.Tick(); }
public void Dispose() { if (luaEnv == null) { Debug.Log("解析器为初始化"); return; } luaEnv.Dispose(); luaEnv = null; } }
|