16.Lua调用C#的ref和out函数

16.Lua调用CSharp-ref和out函数


16.1 知识点

准备工作

启动Lua脚本

LuaManager.Instance.Init();
LuaManager.Instance.DoLuaFile("Lesson16_Lua调用CSharp_ref和out函数");

定义有ref和out函数的类

public class Lesson16_Test
{
    public int RefFunction(int a, ref int b, ref int c, int d)
    {
        int temp = b;
        b = c;
        c = temp;
        return a + d;
    }

    public int OutFunction(int a, out int b, out int c, int d)
    {
        b = a;
        c = d;
        return a + d;
    }

    public int RefAndOutFunction(int a, out int b, ref int c)
    {
        b = a * 100;
        c = a * 666;
        return a;
    }
}

在Lua脚本实例化

Lesson16_Test = CS.Lesson16_Test
local test = Lesson16_Test()

ref函数

  • 有ref修饰的参数的函数 在Lua脚本调用时 会以多返回值的形式返回给lua变量
  • 如果 ref 函数存在返回值,在 Lua 脚本调用时返回的第一个值是 ref 函数的返回值,其他多出来的返回值即为传入的 ref 的返回值,从左到右一一对应。
  • ref 修饰的参数需要传入一个默认值占位置。
local a, b, c = test:RefFunction(1, 666, 999, 1) -- 正常传参
print(a) -- 2 函数返回值,因为 1 + 1
print(b) -- 999 第一个 ref 参数 b 的返回值,函数内 b 和 c 交换值
print(c) -- 666 第二个 ref 参数 c 的返回值,函数内 b 和 c 交换值

local a, b, c = test:RefFunction(1) -- 忽略一些参数,会默认用 0 补位
print(a) -- 1 函数返回值,因为 1 + 0
print(b) -- 0 第一个 ref 参数 b 的返回值,函数内 b 和 c 交换值仍然为 0
print(c) -- 0 第二个 ref 参数 c 的返回值,函数内 b 和 c 交换值仍然为 0

out函数

  • 有out修饰的参数的函数 在Lua脚本调用时 会以多返回值的形式返回给lua变量
  • 如果 out 函数存在返回值,在 Lua 脚本调用时返回的第一个值是 out 函数的返回值,其他多出来的返回值即为传入的 out 的返回值,从左到右一一对应。
  • out 修饰的参数不需要传占位置的值,传入的参数相当于直接无视 out 修饰的参数再一一对应。
local a, b, c = test:OutFunction(100, 200) -- 100 对应参数 a,200 对应参数 d
print(a) -- 300 函数返回值,因为 100 + 200
print(b) -- 100 第一个 out 参数 b 的返回值,函数内 b 被赋值第一个参数
print(c) -- 200 第二个 out 参数 c 的返回值,函数内 c 被赋值第四个参数,由于 out 参数忽略实际上赋值的是 200

ref和out函数

  • 当 ref 和 out 同时修饰函数参数时,ref 需要传入占位值,out 不需要传占位值。
  • 第一个返回值是函数的返回值,之后的返回值从左到右依次对应 ref 或者 out。
local a, b, c = test:RefAndOutFunction(10, 1) -- 10 对应参数 a,1 对应参数
print(a) -- 10 函数内直接返回参数 a
print(b) -- 1000 out 参数拿 a 的值乘了 100
print(c) -- 6600 ref 参数拿 a 的值乘了 666

16.2 知识点代码

Lesson16_Lua调用CSharp_ref和out函数.lua

print('我是Lua脚本 Lesson16_Lua调用CSharp_ref和out函数')

Lesson16_Test = CS.Lesson16_Test
local test = Lesson16_Test()

print("**********知识点一 ref函数************")
--有ref修饰的参数的函数 在Lua脚本调用时 会以多返回值的形式返回给lua变量
--如果ref函数存在返回值 那么在Lua脚本调用时返回的第一个值是ref函数的返回值
--其他多出来的返回值 就是传入的ref的返回值 从左到右一一对应
--ref修饰的参数 需要传入一个默认值 占位置

local a,b,c = test:RefFunction(1, 666, 999, 1)--正常传参
print(a)--2 函数返回值 因为1+1
print(b)--999 第一个ref参数b的返回值 函数内b和c交换值
print(c)--666 第二个ref参数c的返回值 函数内b和c交换值

local a,b,c = test:RefFunction(1)--忽略一些参数 会默认用0补位
print(a)--1 函数返回值 因为1+0
print(b)--0 第一个ref参数b的返回值 函数内b和c交换值仍然为0
print(c)--0 第二个ref参数c的返回值 函数内b和c交换值仍然为0


print("**********知识点二 out函数************")
--有out修饰的参数的函数 在Lua脚本调用时 会以多返回值的形式返回给lua变量
--如果out函数存在返回值 那么在Lua脚本调用时返回的第一个值是out函数的返回值
--其他多出来的返回值 就是传入的out的返回值 从左到右一一对应
--out修饰的参数 不需要传占位置的值 传入的参数相当于直接无视out修饰的参数再一一对应
local a,b,c = test:OutFunction(100,200)--100对应参数a 200对应参数d
print(a)--300 函数返回值 因为100+200
print(b)--100 第一个out参数b的返回值 函数内b被赋值第一个参数
print(c)--200 第二个out参数c的返回值 函数内c被赋值第四个参数 由于out参数忽略实际上赋值的是200


print("**********知识点三 ref和out函数************")
--ref和out同时修饰函数参数时 ref需传入占位值 out不用传占位值
--第一个返回值是函数的返回值  之后的返回值从左到右依次对应ref或者out
local a,b,c = test:RefAndOutFunction(10,1)--10对应参数 1对应参数c
print(a)--10 函数内直接返回参数a
print(b)--1000 out参数拿a的值乘了100
print(c)--6600 ref参数拿a的值乘了666

Lesson16_Lua调用CSharp_ref和out函数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson16_Lua调用CSharp_ref和out函数 : MonoBehaviour
{
    void Start()
    {
        LuaManager.Instance.Init();
        LuaManager.Instance.DoLuaFile("Lesson16_Lua调用CSharp_ref和out函数");
    }
}

public class Lesson16_Test
{
    public int RefFunction(int a, ref int b, ref int c, int d)
    {
        int temp = b;
        b = c;
        c = temp;
        return a + d;
    }

    public int OutFunction(int a, out int b, out int c, int d)
    {
        b = a;
        c = d;
        return a + d;
    }

    public int RefAndOutFunction(int a, out int b, ref int c)
    {
        b = a * 100;
        c = a * 666;
        return a;
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏