|
一、Lua 文件加载
1. 执行字符串
using UnityEngine;using XLua;public class ByString : MonoBehaviour { XLua.LuaEnv luaEnv ; // Use this for initialization void Start () { luaEnv = new XLua.LuaEnv(); //从Unity调用Lua // luaEnv.DoString("print('HelloWorld')"); //从unity调用Lua,Lua调用C# luaEnv.DoString("CS.UnityEngine.Debug.Log('HelloWorld')"); } // Update is called once per frame void Update () { if (luaEnv!=null) { // 清楚 Lua 未手动释放的 LuaBase 对象 luaEnv.Tick(); } } private void OnDestroy() { luaEnv.Dispose(); }}2. 加载 Lua 文件
using System.Collections;using System.Collections.Generic;using UnityEngine;public class HelloWorldByFile : MonoBehaviour { XLua.LuaEnv luaEnv = null; void Start () { TextAsset ta = Resources.Load<TextAsset>("HelloWorld.lua"); luaEnv = new XLua.LuaEnv(); luaEnv.DoString(ta.bytes);//或者用下面的方法 // luaEnv.DoString("require 'HelloWorld'"); //requite 会Load 找到 HelloWorld.Lua.Txt require实际上是调一个个的loader去加载,有一个成功就不再往下尝试,全失败则报文件找不到。 } // Update is called once per frame void Update () { } private void OnDestroy() { luaEnv.Dispose(); }}其中 Lua 文件代码为:
print("Hello Lua")print("Hello Lua")print("Hello Lua")print("Hello Lua")a=2b=3print(a+b)需要注意的是因为 Resource 只支持有限的后缀,放 Resources 下 lua 文件得加上 txt 后缀,如:HelloWorld.lua.txt。
3. 自定义 Loader
using System.Collections;using System.Collections.Generic;using UnityEngine;public class CreateLoad : MonoBehaviour { public delegate byte[] CustomLoader(ref string filepath); // public void LuaEnv.AddLoader(CustomLoader loader) XLua.LuaEnv luaEnv; private byte[] MyLoader(ref string filePath) { print(filePath); string s = " a=5 b=10 print(a+b)"; return System.Text.Encoding.UTF8.GetBytes(s); } void Start () { luaEnv = new XLua.LuaEnv(); luaEnv.AddLoader(MyLoader); luaEnv.AddLoader(MyLoader); luaEnv.DoString("require 'xxx'"); } private void OnDestroy() { luaEnv.Dispose(); }}自定义loader路径
using System.Collections;using System.Collections.Generic;using UnityEngine;using System.IO;public class CreateLoad : MonoBehaviour { public delegate byte[] CustomLoader(ref string filepath); // public void LuaEnv.AddLoader(CustomLoader loader) XLua.LuaEnv luaEnv; void Start () { luaEnv = new XLua.LuaEnv(); luaEnv.AddLoader(MyLoader); luaEnv.DoString("require 'Test007'"); } private void OnDestroy() { luaEnv.Dispose(); } private byte[] MyLoader(ref string filePath) { string absPath = Application.streamingAssetsPath + "/" + filePath + ".lua.txt"; string s= File.ReadAllText(absPath); return System.Text.Encoding.UTF8.GetBytes(s); }}
C#访问Lua
方法1:通过Class类来方位lua中的函数变量方法和表
CSharpCallLua.lua.text
a=100 str= "Hi" isDie=false person={name="zain",age=100,eat=function(self,a,b) print("吃面条")print(a+b)end} function Test(a ,b) print("test") print(a+b)c=20z=ax=breturn z,x,cend using System.Collections;using System.Collections.Generic;using UnityEngine;using XLua;public class CSharpCallLua : MonoBehaviour { XLua.LuaEnv luaEnv = null; // Use this for initialization void Start () { luaEnv = new XLua.LuaEnv(); luaEnv.DoString("require 'CSharpCallLua'"); int a= luaEnv.Global.Get<int>("a"); //获取到lua里面的全局变量 print(a); Preson p = luaEnv.Global.Get<Preson>("person"); print(p.name + "---" + p.age); IPreson p2 = luaEnv.Global.Get<IPreson>("person"); print(p2.age + "--" + p2.name); p2.eat(12, 12); //运行lua中得function 方法1 使用delegate Test test = luaEnv.Global.Get<Test>("Test"); int x = 0; int c = 0; int z = test(12, 12, out x, out c); print(z); print(x); print(c); // //运行lua中得function 方法2 映射到luafunction LuaFunction fun = luaEnv.Global.Get<LuaFunction>("Test"); object [] o = fun.Call(12, 34); foreach (var item in o) { print(item); } } [CSharpCallLua] delegate int Test(int a,int b ,out int x,out int c); public void Eat() { } private void OnDestroy() { luaEnv.Dispose(); } public class Preson { public string name; public int age; } [ CSharpCallLua] interface IPreson { string name { get; set; } int age { get; set; } void eat(int a,int b); }}方法2:通过Interface可以改变lua中 table表中的参数
lua CSharpCallLua.lua.txt 里面声明
preson ={ name="Zain",age=100 ,eat=function(thisself,a,b)print(a+b)print("eat吃东西")end}
CSharp脚本 CSharpCallLua脚本为
using System.Collections;using System.Collections.Generic;using System.IO;using UnityEngine;using XLua;public class CSharpCallLua: MonoBehaviour { public delegate byte[] CustomLoader(ref string filepath); // public void LuaEnv.AddLoader(CustomLoader loader) XLua.LuaEnv luaEnv; void Start() { luaEnv = new XLua.LuaEnv(); luaEnv.DoString("require 'CSharpCallLua'"); IPorson p2= luaEnv.Global.Get<IPorson>("preson"); print(p2.name + "--" + p2.age); p2.eat(2,4); p2.age = 200; luaEnv.DoString("print(preson.age)"); } private void OnDestroy() { luaEnv.Dispose(); } [CSharpCallLua] interface IPorson { string name { get; set; } int age { get; set; } void eat(int a, int b); } private byte[] MyLoader(ref string filePath) { string absPath = Application.streamingAssetsPath + "/" + filePath + ".lua.txt"; string s = File.ReadAllText(absPath); return System.Text.Encoding.UTF8.GetBytes(s); }} |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|