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 小时前
Lua语法入门-条件控制、函数
开发语言·junit·lua
gregmankiw5 小时前
Nemotron架构(Mamba3+Transformer+Moe)
android·深度学习·transformer
xianjian09127 小时前
MySQL 的 INSERT(插入数据)详解
android·数据库·mysql
欧简墨8 小时前
kotlin Android Extensions插件迁移到viewbinding总结
android·trae
货拉拉技术8 小时前
优雅解决Android app后台悬浮窗权限问题
android
用户69371750013849 小时前
Android 手机终于能当电脑用了
android·前端
第二层皮-合肥9 小时前
基于C#的工业测试控制软件-总体框架
开发语言·c#
用户5172231574809 小时前
android资源类型与布局资源详细介绍
android
优选资源分享10 小时前
GKD v1.11.6 | 安卓开屏广告跳过工具 可用版
android
robotx10 小时前
安卓zygote启动相关
android