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("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)

相关推荐
码农coding1 小时前
android12 状态栏图标的加载流程
android
hai_android1 小时前
Kotlin 协程作用域源码剖析
android·kotlin
xintaiideas2 小时前
.NET 项目的多方式启动指南 [特殊字符]
c#
Yeyu2 小时前
Android 画中画(PiP)小窗播放:怎么做、以及它到底是怎么工作的
android
千里马学框架2 小时前
安卓系统性能优化高级实战开发专题--开机,app冷启动优化
android·智能手机·性能优化·framework·性能·开机优化·app冷启动优化
律宏阔2 小时前
Android 手机通过 adblib 使用 ADB Wi-Fi 控制 Android 9 开发板
android
Z5998178413 小时前
c#软件开发学习笔记--WPF(MVVM框架)
笔记·学习·c#
律宏阔3 小时前
Android App 里实现开机动画替换
android
消失的旧时光-19433 小时前
Android 系统层扫盲 05:Android 开机后发生了什么?从 Bootloader 到 Launcher
android·zygote·fork·aosp·cow
hunterandroid4 小时前
Android 线上卡顿治理:从布局层级到主线程负载的全链路排查
android