参数的输入输出属性(out,ref)
-
1、Lua脚本
local DrivenClass = CS.DrivenClass
local testobj = DrivenClass()
--复杂方法调用
local ret, p2, p3, csfunc = testobj:ComplexFunc({x=3, y = 'john'}, 100, function()
print('i am lua callback')
end)
print('ComplexFunc ret:', ret, p2, p3, csfunc)
csfunc()
-
2、C#脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
[LuaCallCSharp]
public class BaseClass
{
}
public struct Param1//结构体参数
{
public int x;
public string y;
}
[LuaCallCSharp]
public class DrivenClass : BaseClass
{
//复杂函数,参数的输入输出属性(out,ref)
public double ComplexFunc(Param1 p1, ref int p2, out string p3, Action luafunc, out Action csfunc)
{
Debug.Log("P1 = {x=" + p1.x + ",y=" + p1.y + "},p2 = " + p2);
luafunc();
p2 = p2 * p1.x;
p3 = "hello " + p1.y;
csfunc = () =>
{
Debug.Log("csharp callback invoked!");
};
return 1.23;
}
}
public class _005_LuaCallCSharp : MonoBehaviour
{
private LuaEnv env;
void Start ()
{
env = new LuaEnv();
env.DoString("require 'LuaCallCSharp'");
}
private void Update()
{
if(env!=null)
{
env.Tick();
}
}
private void OnDestroy()
{
env.Dispose();
}
}
运行结果:
img.jpg
注意:
1、Lua调用侧的参数处理规则:C#的普通参数算一个输入形参,ref修饰的算一个输入形参,out不算,然后从左往右对应lua 调用侧的实参列表;
2、Lua调用侧的返回值处理规则:C#函数的返回值(如果有的话)算一个返回值,out算一个返回值,ref算一个返回值,然后从左往右对应lua的多返回值。