https://blog.csdn.net/fjjaylz/article/details/86578489
using LuaInterface;
using UnityEngine;
public class LuaAccess : MonoBehaviour
{
string luaFile = "LuaAccess";
LuaState state;
void Start()
{
state = new LuaState();
state.Start();
//使用文件调用Lua
//手动添加一个lua文件搜索地址
string sceneFile = Application.dataPath + "/LuaStudy";
state.AddSearchPath(sceneFile);
state.Require(luaFile);//载入文件
//获取Lua变量
Debug.Log("获取文件中变量:" + state["num"]);
state["num"] = 10;
Debug.Log("设置文件中变量为:" + state["num"]);
//调用Lua方法
LuaFunction luaFunc = state.GetFunction("Count");
luaFunc.Call();
Debug.Log("C#调用LuaFunc,函数返回值:" + state["num"]);
Debug.Log("C#直接调用Count方法。");
state.Call("Count", false);
//对方法传入参数
LuaFunction valueFunc = state.GetFunction("InputValue");
valueFunc.BeginPCall();
valueFunc.Push("--push方法从C#中传入参数--");
valueFunc.PCall();
valueFunc.EndPCall();
valueFunc.Call("--直接Call方法从C#传入参数--");
//获取LuaTable
LuaTable table = state.GetTable("mytable");
table.Call("tableFunc");
LuaFunction tableFunc = table.GetLuaFunction("tableFunc");
Debug.Log("C#调用table中的func");
tableFunc.Call();
Debug.Log("获取table中的num值:"+table["num"]);
//通过下标直接获取
for (int i = 0; i < table.Length; i++)
{
Debug.Log("获取table的值:" + table[i]);
}
//转换成LuaDictTable
LuaDictTable dicTable = table.ToDictTable();
foreach (var item in dicTable)
{
Debug.LogFormat("遍历table:{0}--{1}", item.Key, item.Value);
}
state.Dispose();
}
————————————————
版权声明:本文为CSDN博主「达也酱」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fjjaylz/article/details/86578489