xLua Lua访问C#注意事项(七)

  • 调用成员方法
    注意:调用成员方法,第一个参数需要传该对象,建议用冒号语法
Lua 复制代码
loacl camera =  CS.UnityEngine.GameObject.Find("Main Camera")
--冒号语法
camera:GetComponent("Camera")
--点语法
camera.GetComponent(camera,"Camrea")
  • xlua支持子类访问父类的静态属性、静态方法
  • xlua支持子类实例访问父类的成员属性、成员方法
  • lua支持多个返回值,C#只有一个返回值,但是C#支持传递多个out、ref参数,在C#中可以使用ref和out来接受lua的多返回值
Lua 复制代码
--[Lua--]
function luaAction(a,b,c)
	return a,a+b,c
end

--[C#]
[CSharpCallLua]
delegate int CSAction(int a, int b, ref int resa, out int resb);


  var testAction = luaEnv.Global.Get<CSAction>("luaAction");
        int resa = 100;
        int resb;
        int value= testAction (45,67,ref resa, out resb);
        print(value);
        print(resa);
        print(resb);
        testAction = null;
  • xlua支持方法重载,直接通过不同的参数类型访问
Lua 复制代码
test:TestAction()
test:TestAciton(1)
test:TestAction("a")
  • xlua调用C#方法时,如果C#方法的形参中带有默认值,不传值时,会按默认值传递

  • 扩展方法,C#中定义了扩展方法,lua中可以直接使用

  • lua不支持泛型方法,建议在C#中封装后使用

  • xlua调用C#的枚举,需要在C#定义枚举时,加上[CSharpCallLua]特性

    //C#
    [CSharpCallLua]
    public enum TestEnum
    {
    One,
    Two,
    Three
    }
    --lua
    CS.TestEnum.One

  • xlua调用C#委托与调用C#的方法没有区别

    //C#
    [CSharpCallLua]
    public delegate void TestDelegate();

    public TestDelegate testDelegate;

    luaEnv = new LuaEnv();
    luaEnv.DoString("require 'TestLua'");//使用内置loder加载lua源文件
    testDelegate = luaEnv.Global.Get<TestDelegate>("luaAction");
    testDelegate?.Invoke();

    --lua
    function luaAction()
    print("委托")
    end

  • xlua调用C#event并添加事件

    //C#
    [CSharpCallLua]
    public delegate void TestEvent();

    public event TestEvent testEvent;

    luaEnv = new LuaEnv();
    luaEnv.DoString("require 'TestLua'");//使用内置loder加载lua源文件
    testEvent?.Invoke();
    luaEnv.Dispose();

    --lua
    function luaAction()
    print("事件")
    end

    local GameObject = CS.UnityEngine.GameObject
    local obj = GameObject.Find("GameObject")
    local manager = obj:GetComponent("Manager")
    manager:testEvent('+',luaAction)

  • xlua获取C#类型

    typeof(CS.UnityEngine.GameObject)

相关推荐
儿歌八万首4 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
E_ICEBLUE5 小时前
PPT 批量转图片:在 Web 预览中实现翻页效果(C#/VB.NET)
c#·powerpoint·svg
JQLvopkk6 小时前
C# 轻量级工业温湿度监控系统(含数据库与源码)
开发语言·数据库·c#
消失的旧时光-19437 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
Jinkxs7 小时前
Gradle - 与Groovy/Kotlin DSL对比 构建脚本语言选择指南
android·开发语言·kotlin
&有梦想的咸鱼&7 小时前
Kotlin委托机制的底层实现深度解析(74)
android·开发语言·kotlin
LDORntKQH7 小时前
基于深度强化学习的混合动力汽车能量管理策略 1.利用DQN算法控制电池和发动机发电机组的功率分配 2
android
冬奇Lab7 小时前
Android 15 ServiceManager与Binder服务注册深度解析
android·源码·源码阅读
2501_916008899 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
wxin_VXbishe9 小时前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php