Unity ToLua 之 C#调用Lua(二)
一.Lua解析器管理器
public class LuaMgr : SingletonAutoMono< LuaMgr>
{
private LuaState _luaState;
public LuaState LuaState => _luaState;
public void Init ( )
{
new ToLuaCustomLoader ( ) ;
_luaState = new LuaState ( ) ;
_luaState. Start ( ) ;
}
public void DoString ( string str)
{
_luaState?. DoString ( str) ;
}
public void Require ( string str)
{
_luaState. Require ( str) ;
}
private void OnDestroy ( )
{
if ( _luaState == null ) return ;
_luaState. CheckTop ( ) ;
_luaState. Dispose ( ) ;
_luaState = null ;
}
}
二.Lua全局变量的获取
testInt = 1
testFloat = 2.2
testBool = true
testString = "zzs"
public class Lesson04_Global : MonoBehaviour
{
private void Start ( )
{
LuaMgr. GetInstance ( ) . Init ( ) ;
LuaMgr. GetInstance ( ) . Require ( "Main" ) ;
var globalState = LuaMgr. GetInstance ( ) . LuaState;
Debug. Log ( globalState[ "testInt" ] ) ;
Debug. Log ( globalState[ "testFloat" ] ) ;
Debug. Log ( globalState[ "testBool" ] ) ;
Debug. Log ( globalState[ "testString" ] ) ;
globalState[ "testInt" ] = 999 ;
Debug. Log ( globalState[ "testInt" ] ) ;
}
}
三.Lua全局函数的获取
1.无参无返回
function TestFunc1 ( )
print ( "无参无返回" )
end
public class Lesson05_Function : MonoBehaviour
{
private void Start ( )
{
LuaMgr. GetInstance ( ) . Init ( ) ;
LuaMgr. GetInstance ( ) . Require ( "Main" ) ;
var globalState = LuaMgr. GetInstance ( ) . LuaState;
var func = globalState[ "TestFunc1" ] as LuaFunction ;
func. Call ( ) ;
func. Dispose ( ) ;
func = globalState. GetFunction ( "TestFunc1" ) ;
func. Call ( ) ;
func. Dispose ( ) ;
func = globalState. GetFunction ( "TestFunc1" ) ;
Action act = func. ToDelegate < Action> ( ) ;
act?. Invoke ( ) ;
func. Dispose ( ) ;
}
}
2.有参有返回
function TestFunc2 ( num)
return num + 100
end
var func2 = globalState. GetFunction ( "TestFunc2" ) ;
func2. BeginPCall ( ) ;
func2. Push ( 9 ) ;
func2. PCall ( ) ;
var num = ( int ) func2. CheckNumber ( ) ;
Debug. Log ( num) ;
Debug. Log ( func2. Invoke < int , int > ( 99 ) ) ;
var funcDelegate = func2. ToDelegate < Func< int , int > > ( ) ;
Debug. Log ( funcDelegate. Invoke ( 999 ) ) ;
Debug. Log ( globalState. Invoke < int , int > ( "TestFunc2" , 9999 , true ) ) ;
3.多返回值
注意使用自定义委托需要在CustomSettings中添加对应的委托类型 需要重新点击生成委托 也可以使用ref进行多返回值的接收
function TestFunc3 ( num)
return 1 , 2.2 , false , "zzs" , num
end
var func3 = globalState. GetFunction ( "TestFunc3" ) ;
func3. BeginPCall ( ) ;
func3. Push ( "wy" ) ;
func3. PCall ( ) ;
var a1 = ( int ) func3. CheckNumber ( ) ;
var a2 = ( float ) func3. CheckNumber ( ) ;
var a3 = func3. CheckBoolean ( ) ;
var a4 = func3. CheckString ( ) ;
var a5 = func3. CheckString ( ) ;
func3. EndPCall ( ) ;
Debug. Log ( a1 + "." + a2 + "." + a3 + "." + a4 + "." + a5) ;
var customOut = globalState. GetFunction ( "TestFunc3" ) . ToDelegate < CustomOut> ( ) ;
customOut. Invoke ( 99 , out var b1, out var b2, out var b3, out var b4, out var b5) ;
Debug. Log ( b1 + "." + b2 + "." + b3 + "." + b4 + "." + b5) ;
4.变长参数
function TestFunc4 ( num, ... )
local arr = { ... }
for i, v in pairs ( arr) do
print ( i, v)
end
end
var func4 = globalState. GetFunction ( "TestFunc4" ) ;
var del = func4. ToDelegate < CustomLength> ( ) ;
del. Invoke ( 100 , "zzs" , 99.9 , false ) ;
func4. Call < int , string , float , bool > ( 100 , "zzs" , 99.9f , false ) ;