找回密码
 立即注册
查看: 265|回复: 0

Unity Xlua 之 Lua调用C#(四)

[复制链接]
发表于 2023-4-9 11:39 | 显示全部楼层 |阅读模式
Unity Xlua 之 Lua调用C#(四)

一.Lua和系统类,系统委托调用

    系统自定义的类或者委托,当我们需要调用时要加[LuaCallCSharp]时,不能直接加入
  1. publicstaticclassLesson10{[CSharpCallLua]privatestatic List<Type> allCSharpCallLuaList =newList<Type>(){typeof(UnityAction<float>)};[LuaCallCSharp]privatestatic List<Type> allLuaCallCSharpList =newList<Type>(){typeof(GameObject),typeof(Rigidbody),};}
复制代码
  1. GameObject = CS.UnityEngine.GameObject
  2. local sliderObj = GameObject.Find("Slider")local sliderComponent = sliderObj:GetComponent(typeof(CS.UnityEngine.UI.Slider))
  3. sliderComponent.onValueChanged:AddListener(function(f)print(f)end)
复制代码
二.Lua调用携程

    测试后发现需要在unity中添加标签,携程才能正常调用


  1. util =require("xlua.util")
  2. GameObject = CS.UnityEngine.GameObject
  3. WaitForSeconds = CS.UnityEngine.WaitForSeconds
  4. local go =GameObject("Coroutine")local mono = go:AddComponent(typeof(CS.TestMono))
  5. func =function()local a =0whiletruedoprint(a)
  6.         a = a +1
  7.         coroutine.yield(WaitForSeconds(0.5))if a >=10then
  8.             mono:StopCoroutine(b)endendend
  9. b = mono:StartCoroutine(util.cs_generator(func))
复制代码
三.Lua调用泛型方法

    Lua本身只支持,有参有类约束的泛型方法,其余泛型方法都不支持除非使用Xlua为其提供的方法,但是不建议使用有局限性如果打包为Mono方式的话可以,但是打包为il2cpp方式有局限性(具体参考下图)
  1. publicclassLesson12{publicclassTestFather{}publicclassTestChild:TestFather, ITest
  2.     {}publicinterfaceITest{}publicvoidTestFun1<T>(T t1,T t2)where T : TestFather
  3.     {
  4.         Debug.Log("有参有约束");}publicvoidTestFun2<T>(T t1){
  5.         Debug.Log("有参无约束");}publicvoidTestFun3<T>()where T : TestFather
  6.     {
  7.         Debug.Log("无参有约束");}publicvoidTestFun4<T>(T t1)where T : ITest
  8.     {
  9.         Debug.Log("有参有接口约束");}}
复制代码
  1. Lesson12 = CS.Lesson12
  2. TestFather = Lesson12.TestFather
  3. TestChild = Lesson12.TestChild
  4. local l12 =Lesson12()local testFather =TestFather()local testChild =TestChild()
  5. l12:TestFun1(testFather,testChild)
  6. l12:TestFun1(testChild,testFather)--l12:TestFun2(testFather)--l12:TestFun2(testChild)local testFun2 = xlua.get_generic_method(CS.Lesson12,"TestFun2")local testFun2_R =testFun2(CS.System.Int32)testFun2_R(l12,123)local testFun3 = xlua.get_generic_method(CS.Lesson12,"TestFun3")local testFun3_R =testFun3(TestFather)testFun3_R(l12)local testFun4 = xlua.get_generic_method(CS.Lesson12,"TestFun4")local testFun4_R =testFun4(TestChild)testFun4_R(l12,testChild)
复制代码
  1. print("*********Lua调用C# 泛型函数相关知识点***********")local obj = CS.Lesson12()local child = CS.Lesson12.TestChild()local father = CS.Lesson12.TestFather()--支持有约束有参数的泛型函数
  2. obj:TestFun1(child, father)
  3. obj:TestFun1(father, child)--lua中不支持 没有约束的泛型函数--obj:TestFun2(child)--lua中不支持 有约束 但是没有参数的泛型函数--obj:TestFun3()--lua中不支持 非class的约束--obj:TestFun4(child)--有一定的使用限制--Mono打包 这种方式支持使用--il2cpp打包  如果泛型参数是引用类型才可以使用--il2cpp打包  如果泛型参数是值类型,除非C#那边已经调用过了 同类型的泛型参数 lua中才能够被使用--补充知识 让上面 不支持使用的泛型函数 变得能用--得到通用函数  --设置泛型类型再使用--xlua.get_generic_method(类, "函数名")local testFun2 = xlua.get_generic_method(CS.Lesson12,"TestFun2")local testFun2_R =testFun2(CS.System.Int32)--调用--成员方法  第一个参数 传调用函数的对象--静态方法 不用传testFun2_R(obj,1)
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-22 19:18 , Processed in 0.126682 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表