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)

相关推荐
e***0969 小时前
【MySQL】MySQL库的操作
android·数据库·mysql
qq_589568109 小时前
android通过SharedPreferences保存共享数据之后,怎么打开设备文件查看保存的数据,并取出保存的数据
android
4***V2029 小时前
MySQL查询执行计划
android·mysql·adb
Tiger_shl9 小时前
SqlConnection、SqlCommand 和 SqlDataAdapter
开发语言·数据库·c#
在狂风暴雨中奔跑10 小时前
Android 16KB Page Size 适配实战流程:以重编译FFmpeg so库为例
android
月下的郁王子10 小时前
进阶学习 PHP 中的二进制和位运算
android·学习·php
BrianGriffin10 小时前
DcatAdmin框架小坑
android
yi碗汤园12 小时前
Visual Studio常用的快捷键
开发语言·ide·c#·编辑器·visual studio
Just_Paranoid12 小时前
【Settings】获取 SIM 卡信号强度 dBm 和 ASU
android·at·csq·dbm·asu
Q***f63513 小时前
Kotlin在Android性能优化中的工具
android·开发语言·kotlin