XLua教程之Lua调用C#

测试模板

cs 复制代码
public class Test
{
    public int num1;//实例变量
    public static int num2 ;//静态变量
   
    private string flag1;
    public string Flag1 => flag1; //实例属性
    
    private static string flag2;
    public static string Flag2 => flag2;//静态属性

    //委托
    [CSharpCallLua]
    public delegate int GetSumEventHandler(int a, int b);
    public GetSumEventHandler GetSumLogic;
    
    //构造函数
    public Test(int _num1,int _num2)
    {
        num1 = _num1;
        num2 = _num2;

        flag1 = "好好学习";
        flag2 = "天天向上";
    }
    
    //实例方法
    public void Say()
    {
        Debug.Log("Hello World!");
    }
    
    //静态方法
    public static void Say1()
    {
        Debug.Log("Hello World!");
    }

    //ref参数方法
    public void RefFunc(int a,ref int b,ref int c,int d)
    {
        b = a+d;
        c = a-d;
      
    }
    //out参数方法
    public string OutFunc(int a,out int b,out int c,int d)
    {
        b = a +d;
        c = a -d;
        return "Lua调用Out函数";
    }
    //ref参数+out参数方法
    public int RefAndOutFunc(int a,ref int b,out int c)
    {
        b = a * 3;
        c = a * 9;
        return 250;
    }
}

new C#对象

所有C#的类都放在CS字段中。如果需要经常访问的类,可以先用局部变量引用后访问,除了减少敲代码的时间,还能提高性能:

Lua 复制代码
local Class = CS.Test
local test = Class(111,222)

访问C#静态变量和实例变量

使用逗号访问

Lua 复制代码
test.num1--实例变量
test.num2--静态变量

访问C#静态属性和实例属性

使用逗号访问

Lua 复制代码
test.Flag1--实例属性
test.Flag2--静态属性

调用C#静态方法和实例方法

静态方法使用逗号调用

实例方法使用冒号调用(即将调用对象作为第一参数传入)

Lua 复制代码
local Class = CS.Test
local test = Class(111,222)
test:Say()--实例方法
Class.Say1()--静态方法

调用C#有out参数和ref参数的方法

1、如果C#函数有返回值,那么Lua中第一个返回值就是该函数的返回值。

2、在调用ref函数时,需要在ref参数处添加占位值,否则系统会自动补零。

3、在调用out函数时,不需要在out参数处添加占位值。

Lua 复制代码
--ref参数方法
local b,c = test:RefFunc(2,0,0,3)
print("调用ref函数:",b,c)

--out参数方法
local a,b,c = test:OutFunc(2,3)
print("调用out函数:",a,b,c)

--ref参数+out参数方法
local a,b,c = test:RefAndOutFunc(3)
print("调用ref和out的混合函数:",a,b,c)

调用C#委托

对于自定义委托需要添加CSharpCallLua标签,并重新生成代码

Lua 复制代码
local logic = function(a,b)
    local result = a+b;
    print("委托结果 加法:",result)
    return result
end

local logic1 = function(a,b)
    local result = a-b;
    print("委托结果 减法:",result)
    return result
end

--如果第一次向委托中添加函数,因为是nil,所以不能直接加,需要=
test.GetSumLogic = logic

--添加事件处理器
test.GetSumLogic = test.GetSumLogic + logic1
test.GetSumLogic(1,2)

--移除事件处理器
test.GetSumLogic = test.GetSumLogic - logic1
test.GetSumLogic(1,2)
相关推荐
落寞的电源1 小时前
Delegate = Object + MethodInfo
开发语言·数据库·c#
Ricky_Theseus3 小时前
Trie 字典树:前缀匹配利器
开发语言·c#
郝学胜-神的一滴3 小时前
[简化版 GAMES 101] 计算机图形学 16:纹理走样、Mipmap、三线性插值、各向异性过滤原理全解
unity·游戏引擎·godot·图形渲染·three.js·opengl·unreal
北域码匠17 小时前
嵌入式限幅滤波:工业信号降噪利器
c#·传感器采集·数据预处理·嵌入式算法·限幅滤波·数字滤波·数据降噪
吴梓穆1 天前
untiy TextMeshPro (TMP) text 操作 中文字符集 字体材质操作(添加纹理 添加描边 添加阴影)
unity
星空露珠1 天前
迷你世界UGc3.0脚本Wiki[剧情动画模块管理接口 Timeline]
开发语言·数据结构·算法·游戏·lua
想你依然心痛1 天前
嵌入式单元测试:Unity/CMock框架与硬件在环测试——测试驱动、桩函数
unity·单元测试·游戏引擎
xcLeigh1 天前
Unity基础:Game视图详解——游戏预览、分辨率模拟与性能显示
游戏·unity·游戏引擎·音频·视频·game·play模式
开开心心_Every1 天前
带OCR识别的电子发票打印工具
运维·自动化·ocr·电脑·powerpoint·音视频·lua
csdn_aspnet1 天前
C# 提取、截取或匹配字符串内包含指定字符的一些方法分享
c#·字符串·正则·分割·提取·匹配