lua-路径加载lua文件-函数返回值,访问lua文件中的变量

lua文件如下:

print("ddhdhh")

function login(username,pswd)

if username =="ms" and pswd=="ms" then

print('用户存在')

return true;

else

print('用户不存在')

return false

end

end

user = {id = "010101",LastLoginTime= "2016-5"}

version = "12.11"

c#代码调用上述lua文件的内容:

using UnityEngine;

using System.Collections;

using LuaInterface;

public class MyScriptsFromFile : MonoBehaviour {

LuaState lua;

string serchPath;

// Use this for initialization

void Start () {

serchPath = Application.dataPath + "/0My/02-ScriptsFromFile";//lua文件的路径(不包括文件名)

lua = new LuaState();

lua.Start();

//添加搜索路径

lua.AddSearchPath(serchPath);

// lua.Require("02"); //用require和dofile都行,注意参数的区别

lua.DoFile("02.lua");

LuaFunction func = lua.GetFunction("login");//调用login函数

func.BeginPCall();

func.Push("ms");

func.Push("mss");

func.PCall();

//取函数返回值,布尔类型

bool isOk = func.CheckBoolean();

func.EndPCall();

if (isOk)

{

//访问lua变量 非table类型变量

print(lua["version"]);

//访问lua变量 table类型变量

LuaTable user = lua.GetTable("user");//获取usertable表

print(user["id"]);//获取表中的id变量

}

else

{

print("用户不存在");

}

lua.CheckTop();

lua.Dispose();

}

// Update is called once per frame

void Update () {

}

}