lua调用c#
lua调用c#的对象
所有c#对象都放在CS中
1 | CS.System;CS.System.IO;CS.UnityEngine.GameObject(); |
lua调用c#静态变量
基本脚本结构
【luai脚本】
1 2 3 4 5 | -- 全局变量 s1 = "this is lua" --引用unity方法 local GameObject = CS.UnityEngine.GameObject local UI = CS.UnityEngine.UI; |
【c#脚本】
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 | using UnityEngine; using System.Collections; using System.Collections.Generic; using XLua; using System; using System.IO; namespace XLuaTest1 { public class LuaTest : MonoBehaviour { private LuaEnv luaenv; void Start () { // 启动lua虚拟机 luaenv = new LuaEnv(); // luaenv.AddLoader(LuaLoader); // 读取Resources下的脚本 TextAsset luaScript = Resources.Load<TextAsset>("lua.lua") as TextAsset; // 运行脚本 luaenv.DoString(luaScript.text, ""); //获取lua全局属性1 string mStr = luaenv.Global.Get<string>("s1"); Debug.Log("lua globle string:" + mStr); } void Update () { // 每帧gc if (luaenv != null) { luaenv.Tick(); } } void Destroy() { //释放lua if (luaenv != null) { luaenv.Dispose(); } } //脚本读取器 private byte[] LuaLoader(ref string filename) { TextAsset text = Resources.Load("Lua/" + filename + ".lua") as TextAsset; return text.bytes; } } } |
调用类
【lua脚本】
引用 CS脚本 -> XluaTest1命名空间 -> mClass类
1 2 | local mClass = CS.XLuaTest1.myClass; local testObj = mClass() |
【C#脚本】
1 2 3 4 5 6 | namespace XLuaTest1 { public class myClass { } } |
调用属性、方法
调用属性、静态属性、静态方法用".“连接。调用方法用”:"连接。
【lua脚本】
1 2 3 4 5 6 7 8 9 10 | -- 引用 CS脚本 -> XluaTest1命名空间 -> mClass类 local mClass = CS.XLuaTest1.myClass; -- 实例化一个类,不需要new关键字 local testObj = mClass() -- 调用变量、方法 print("num",testObj.num) testObj:Func() -- 调用变量、方法, 原始类名 + . + 变量或方法 print("staticNum",mClass.staticNum) mClass.staticFunc() |
【C#脚本】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | namespace XLuaTest1 { // [LuaCallCSharp] public class myClass { public static int staticNum = 111; public static void staticFunc() { Debug.Log("静态值 = " + staticNum); } public int num = 222; public void Func() { Debug.Log("变量值 = " + num); } } } |
调用枚举
【lua脚本】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | -- 实例化类, cs > 命名空间 > 类 local myNum = CS.XLuaTest1.myNum() -- 引用枚举 local enumNum = CS.XLuaTest1.enumNum -- 调用c#方法,并传入枚举,返回枚举值 local enumValue = myNum:SwitchNum(enumNum.one) print("enumValue",enumValue) -- 根据枚举的value,返回key myNum:SwitchNum(enumNum.__CastFrom(0)) -- 根据枚举的key,返回值 myNum:SwitchNum(enumNum.__CastFrom("one")) -- 方法 直接传入枚举的值 myNum:SwitchNum(0) -- 方法 直接传入枚举的key myNum:SwitchNum("one"); |
【C#脚本】
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 | namespace XLuaTest1 { // 创建枚举 [LuaCallCSharp] public enum enumNum { one, two } [LuaCallCSharp] // 使用枚举的方法 class myNum { public enumNum SwitchNum(enumNum num) { switch (num) { case enumNum.one: Debug.Log("one"); break; case enumNum.two: Debug.Log("two"); break; } return num; } } } |
调用Action
【lua脚本】
1 2 3 | local mClass = CS.XLuaTest1.myClass; local testObj = mClass() testObj.act(); |
【C#脚本】
1 2 3 4 5 6 7 8 9 | namespace XLuaTest1 { [LuaCallCSharp] public class myClass { public Action act = ()=>{ Debug.Log("c# Action"); }; } |
调用委托
【lua脚本】
1 2 3 4 5 6 7 8 9 10 11 12 | // 自定义方法 function lua_act(arg) print("lua_act:", arg) end // 实例化类 local testObj = CS.XLuaTest1.mClass() // 引用委托 local act = testObj.act1 // 将c#脚本中的函数与lua脚本中的函数 交给c#的委托 act = act + lua_act + testObj.act2 // 运行委托 act("lua call c#") |
【C#脚本】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | namespace XLuaTest1 { [LuaCallCSharp] public class mClass { // 定义一个委托 public delegate void mDelegate(string arg); // 委托函数 public mDelegate act1 = (string s)=>{ Debug.Log("act1:" + s); }; // 委托函数 public mDelegate act2 = (string s)=>{ Debug.Log("act2:" + s); }; } } |
调用事件
【lua脚本】
1 2 3 4 5 6 7 8 9 10 11 12 13 | -- 自定义方法 function lua_act(arg) print("lua_act:", arg) end -- --实例化类 local testObj = CS.XLuaTest1.mClass() -- 将lua函数 绑定 c#事件 testObj:mEvent("+", lua_act) -- 将c#函数 绑定 c#事件 testObj:mEvent("+", testObj.act1) testObj:mEvent("+", testObj.act2) -- 通过事先定义的接口 运行c#中的事件 testObj:doEvent("lua call c#") |
【C#脚本】
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 | namespace XLuaTest1 { [LuaCallCSharp] public class mClass { // 此处 必须使用该关键字 CSharpCallLua [CSharpCallLua] // 定义委托 public delegate void mDelegate(string arg); // 定义关键字 public event mDelegate mEvent; // c#方法1 public mDelegate act1 = (string s)=>{ Debug.Log("act1:" + s); }; // c#方法2 public mDelegate act2 = (string s)=>{ Debug.Log("act2:" + s); }; // 运行 public void doEvent(string s){ Debug.Log("event:" + s); mEvent("do event"); } } } |
泛型
使用扩展的办法处理lua不支持的泛型需求,或其他需求
【lua脚本】
1 2 3 4 | -- --实例化类 local testObj = CS.XLuaTest1.Extended() -- 传入目标对象,返回c#进行相应处理 testObj.AddComponentRigidbody(gameobject) |
【C#脚本】
1 2 3 4 5 6 7 8 9 10 | namespace XLuaTest1 { [LuaCallCSharp] static class Extended{ public static Rigidbody AddComponentRigidbody(GameObject gameobject) { return gameobject.AddComponent<Rigidbody>(); } } } |
静态列表
使用扩展的办法处理lua不支持的泛型需求,或其他需求
【lua脚本】
1 2 3 4 | -- --实例化类 local testObj = CS.XLuaTest1.luaCallCSharpList("") -- 传入目标对象,返回c#进行相应处理 testObj.AddComponentRigidbody(gameobject) |
【C#脚本】
1 2 3 4 5 6 7 8 | namespace XLuaTest1 { [LuaCallCSharp] public static List<Type> luaCallCSharpList = new List<Type>() { typeof(GameObject), }; } |
调用unity对象
创建unity引用
1 2 | local GameObject = CS.UnityEngine.GameObject local UI = CS.UnityEngine.UI; |
创建unity对象
1 | local go = GameObject("Cube") |
修改unity对象属性
1 2 3 4 | --修改 go.name = "myCube" --访问 print("GameObject1:", go.name) |
调用unity对象的方法
1 2 | go:SetActive(false) go.SetActive(go, false) |
读取unity静态属性
1 2 3 4 5 | --访问 local Time = CS.UnityEngine.Time print("Time.deltaTime:", Time.deltaTime) --修改 Time.timeScale = 0.5 |
获取unity场景中的物体
1 2 | local mainCamera = GameObject.Find("Main Camera") print("mainCamera:", mainCamera.name) |
获取unity物体的组件
1 2 3 4 5 6 7 | --Camera组件 local compCamera = mainCamera:GetComponent("Camera") print("compCamera:",compCamera.fieldOfView) --Transform组件 local compTransform = mainCamera:GetComponent("Transform") -- 调用组件下的方法 print("compTransform:",compTransform:GetChild(0).name) |