C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
using System.Text;
using UnityEngine.UI;
public class TestXLua : MonoBehaviour
{
LuaEnv lua;
void Start()
{
lua = new LuaEnv();
//加载Lua
lua.AddLoader(LoadLua);
lua.DoString("require 'TestLua'");
}
byte[] LoadLua(ref string file)
{
Debug.Log(file);
string slua = File.ReadAllText(Application.dataPath + "/Lua/" + file + ".lua");
byte[] bytes = new UTF8Encoding().GetBytes(slua);
return bytes;
}
void OnDestroy()
{
lua.Dispose();
}
}
可变参数
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player
{
public bool PFunction(int a,float b, params string[]srtArr)
{
Debug.Log("PFunction:" + a);
Debug.Log("PFunction:" + b);
foreach (string item in srtArr)
{
Debug.Log(item);
}
return true;
}
}
Lua
player = CS.Player()
bool = player:PFunction(2,3.6,"aa","bb","cc")
print(bool)
结构体参数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct TheStruct
{
public int i;
public string s;
}
public class Player
{
public void PFunction(TheStruct theStruct)
{
Debug.Log(theStruct.i);
Debug.Log(theStruct.s);
}
}
player = CS.Player()
structTable = {i = 1,s = "a"}
player:PFunction(structTable)
接口参数
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
[CSharpCallLua]
public interface TheInterface
{
int i { get; set; }
string s { get; set; }
void Play();
}
public class Player
{
public void PFunction(TheInterface theInterface)
{
Debug.Log(theInterface.i);
Debug.Log(theInterface.s);
theInterface.Play();
}
}
Lua
player = CS.Player()
TheInterface = {
i = 1,
s = "a",
Play = function ()
print("PLAY")
end
}
player:PFunction(TheInterface)
委托参数
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
[CSharpCallLua]
public delegate void TheDelegate(int i);
public class Player
{
public void PFunction(TheDelegate theInterface)
{
Debug.Log("DELEGATE");
theInterface.Invoke(26);
}
}
Lua
player = CS.Player()
Play = function (i)
print("PLAY"..i)
end
player:PFunction(Play)