您的位置:首页 > 编程语言 > Lua

C#如何访问Lua中的属性(1)

2016-08-18 17:26 232 查看
参考   http://www.myexception.cn/c-sharp/1881698.html

C#如何访问Lua中的属性

1)
.C#如何访问LUA中的属性?

2)
.C#如何访问LUA中的函数?

3)
.C#如何访问LUA中的表?
第一个文件  test0.txt 文件
放在


lua语言

--[[
@author:涛涛
@des:测试C#访问LUA的一些东东
@date:2016-8-17
--]]

config={name="taotao", age=24,qq="55555555"}

Name="ahui";
Age=24;
isBoy=true;

function PrintFromLua(a)
print("打印信息。我来Lua...cs传的参数的值", a);
return 200;
end;
print("test0.lua执行完毕..");
Main.cs文件     随便挂在一个对象上测试
/*
C#如何访问Lua中的属性
1) .C#如何访问LUA中的属性?
2) .C#如何访问LUA中的函数?
3) .C#如何访问LUA中的表?
*/

using UnityEngine;
using System.Collections;
//
using LuaInterface;

public class Main : MonoBehaviour {

private static Main instance;
public string ss;
public TextAsset tt;

// Use this for initialization
void Start () {
instance = this;
}
// Update is called once per frame
void Update()
{

}

//C#调用LUA
void testCSharp_GoLua()
{
LuaState lua = new LuaState();
lua.DoString("print'hello world'");
}

//C#调用LUAFile
void testCSharp_GoLuaFile()
{
LuaState lua = new LuaState();
//lua.DoFileFromAge(this, "Test0.lua");
//TextAsset file = (TextAsset)Resources.Load("test0", typeof(TextAsset));
//if (file == null)
//{
// Debug.Log("sdfds");
//}
//else {
// Debug.Log("加载成功");
//}
lua.DoFile("test0");
}

void testCSharp_GoLuaInfor()
{
LuaState lua = new LuaState();
lua.DoFile("test0");
// 访问LUA中的表
LuaTable configTable = lua.GetTable("config");
Debug.Log("name:" + configTable["name"]);
Debug.Log("age:" + configTable["age"]);
Debug.Log("qq:" + configTable["qq"]);
// 访问Lua 中的基础属性
Debug.Log("Name:" + lua.GetString("Name"));
Debug.Log("Age" + lua.GetNumber("Age"));
Debug.Log("isBoy" + lua["isBoy"]);
//访问Lua中的函数
LuaFunction luaFun = lua.GetFunction("PrintFromLua");
if (luaFun != null)
{
System.Object[] obResult = luaFun.Call(100);
Debug.Log("obResult" + obResult[0]);
}
}

void OnGUI()
{
if (GUILayout.Button("第一节.C#调用LUA"))
{
testCSharp_GoLua();
}

if (GUILayout.Button("第二节.C#调用LUA File"))
{
testCSharp_GoLuaFile();
}
if (GUILayout.Button("第三节.C#调用LUA 信息"))
{
testCSharp_GoLuaInfor();
}
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: