xLua中Lua调用C#
1.前提
这里使用的是XLua框架,需要提前配置xlua,设置加载器路径;
可以参考之前的Blog:《xlua入门基础》;
//调用段,所有的lua代码都写在LuaCallCSharp.lua文件中
public class LuaCallCSharp1 : MonoBehaviour
{
void Start()
{
XluaEnv.I.DoString("LuaCallCSharp");
}
private void OnDestroy()
{
XluaEnv.I.Free();
}
}
2.调用C#类
静态类
public static class TestStatic
{
public static void ShowName(string name, int id)
{
Debug.Log($"name:{name},id:{id}");
}
}
--静态类
CS.TestStatic.ShowName("littlePerilla",1012);
动态类
public class NPC
{
public string name;
public int attack;
public NPC(string name,int attack)
{
this.name = name;
this.attack = attack;
}
public int Hp { get; set; }
public void Attack()
{
Debug.Log($"attack:{attack},Hp:{Hp}");
}
}
--类对象
local hero = CS.NPC("Angel",100)
hero.Hp = 110
hero:Attack()
调用Unity官方Api
--创建物体
local go = CS.UnityEngine.GameObject("LuaObj ")
--添加组件
go:AddComponent(typeof(CS.UnityEngine.BoxCollider))
Lua不支持泛型,所有用到泛型的地方需要把每种可能的重载都写一遍;
调用父类和子类
public class Father
{
public string name = "father";
public virtual void Say()
{
Debug.Log($"{name}:我在被调用");
}
}
public class Child :Father
{
public string name = "child";
public override void Say()
{
Debug.Log($"{name}:我在被调用");
}
}
local father = CS.Father()
father:Say()
local child = CS.Child()
child:Say()
类拓展方法
拓展类必须为静态类,类必须加特性[LuaCallCSharp];《C#类拓展方法》;
会受到xlua版本和unity版本影响,导致调用失败,xlua官方推荐版本是2017(太过时了);
public class TestExtension
{
public string Test1()
{
return "test";
}
}
[LuaCallCSharp]
public static class MyExtension
{
public static void Test2(this TestExtension obj)
{
Debug.Log("ExtensionFunc:"+ obj.Test1());
}
}
local testEx = CS.TestExtension()
print(testEx:Test1())
testEx:Test2()
3.调用C#结构体
结构体和类相似,都有构造方法;
public struct TestStruct
{
public int id;
public string name;
public void Output()
{
Debug.Log(id);
Debug.Log(name);
}
}
--结构体
local teststrut = CS.TestStruct()
teststrut.id = 12
teststrut.name = "littlePerilla"
teststrut:Output()
4.调用C#枚举
枚举使用的userdate自定义数据类型;
public enum State
{
idle = 0,
walk,
run,
attack
}
--枚举使用的userdate自定义数据类型
local state = CS.State.idle
print(state)
--转换获得枚举
print(CS.State.__CastFrom(1))
print(CS.State.__CastFrom("run"))
5.调用C#中委托
静态委托赋值调用必须释放;
动态委托不必要,但是最好也释放;
调用委托前先做为空判定;
Lua中没有+=或-=方法,只能通过a = a+b来实现多播;
public delegate void DelegateLua();
public class TestDelegate
{
public static DelegateLua deStatic;
public DelegateLua deDynamic;
public static void Func()
{
Debug.Log("静态委托");
}
public void Func2()
{
Debug.Log("动态委托");
}
}
--静态委托赋值调用必须释放
CS.TestDelegate.deStatic = CS.TestDelegate.Func
CS.TestDelegate.deStatic()
CS.TestDelegate.deStatic = nil
local func = function ()
-- body
print("lua函数替换委托")
end
--lua函数赋值委托
CS.TestDelegate.deStatic = func
--多播委托,确定deStatic不为空,lua没有+=和-=
if(CS.TestDelegate.deStatic ~= nil)then
CS.TestDelegate.deStatic = CS.TestDelegate.deStatic + func
else
CS.TestDelegate.deStatic = func
end
--调用前判定不为空
if(CS.TestDelegate.deStatic ~= nil)then
CS.TestDelegate.deStatic()
end
CS.TestDelegate.deStatic = nil
--动态委托
local test = CS.TestDelegate()
local func1 = function()
print("动态委托调用")
end
test.deDynamic = func1
test.deDynamic()
test.deDynamic = nil
6.调用C#事件
事件的调用不能直接复制,必须使用(“+”,function);
事件回调完成也需要释放;
public delegate void EventLua();
public class TestEvent
{
public event EventLua luaEvent1;
public static event EventLua luaEvent2;
public static void Func()
{
Debug.Log("静态事件");
}
public static void CallEvent2()
{
if (luaEvent2 != null)
luaEvent2();
}
public void CallEvent1()
{
if (luaEvent1 != null)
luaEvent1();
}
}
--静态事件
CS.TestEvent.luaEvent2("+",CS.TestEvent.Func)
CS.TestEvent.CallEvent2()
CS.TestEvent.luaEvent2("-",CS.TestEvent.Func)
--动态事件
local test = CS.TestEvent()
local func = function ()
print("动态事件")
end
test:luaEvent1("+",func)
test:CallEvent1()
test:luaEvent1("-",func)