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

Lua_ uLua_C#中使用Lua_016

2018-03-08 15:10 316 查看

 
前期工作,导入ulua_v1.25.unitypackage,然后在工程中引入LuaInterface命名空间。

方法一:

using LuaInterface;
// 常见一个Lua解释器
LuaState lua = new LuaState();

//创建全局变量 "num" and "str"
lua["num"] = 2;
lua["str"] = "a string";
//创建空表
lua.NewTable("table_one");
LuaTable tb = lua.GetTable("table_one");
tb["a"]=1000;
print(tb["a"]);

// Read global variables "num" and "str"
double num = (double)lua["num"];
string str = (string)lua["str"];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

方法二:

DoString 和DoFile方法可以执行Lua脚本,方法返回一个脚本执行后返回的数组
// Execute a Lua script file
lua.DoFile("script.lua");

// Execute chunks of Lua code
lua.DoString("num=2");

lua.DoString("str=’a string’");

// Lua code returning values
object[] retVals = lua.DoString("return num,str");
1
2
3
4
5
6
7
8
9
10
DoString
using UnityEngine;
using System.Collections;
using LuaInterface;

public class HelloWorld : MonoBehaviour {

// Use this for initialization
void Start () {
LuaState l = new LuaState();
string str = "print('hello world 世界')";
l.DoString(str);
}

}

using UnityEngine;
using System.Collections;
using LuaInterface;

public class ScriptsFromFile_01 : MonoBehaviour
{

public TextAsset scriptFile;

// Use this for initialization
void Start()
{
LuaState l = new LuaState();
l.DoString(scriptFile.text);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
DoFile
using UnityEngine;
using System.Collections;
using LuaInterface;

public class ScriptsFromFile_02 : MonoBehaviour
{
// Use this for initialization
void Start()
{
//只是展示如何加载文件。不是推荐这么做
LuaState l = new LuaState();
string path = Application.dataPath + "/uLua/luaScript.lua";
l.DoFile(path);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
参考代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;

public class Test : MonoBehaviour {

// Use this for initialization
void Start()
{
//创建一个Lua的解释器
LuaState lua = new LuaState();
lua["num"] = 34;
Debug.Log(lua["num"]);
//可以直接执行lua的代码
lua.DoString("num=2");
Debug.Log(lua["num"]);
lua.DoString("str='a string'");
Debug.Log(lua["str"]);

object[] values = lua.DoString("return num,str");
foreach (var item in values)
{
Debug.Log(item);
}
//下面这句代码默认的路径是  Application.dataPath +\uLua\Lua + LuaTest.lua
//Util.LuaPath("LuaTest.lua");
//在这里不能直接使用Lua.
string path = Application.dataPath + "/LuaTest.lua";
//执行lua的文件
object[] objs = lua.DoFile(path);

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

在Lua中调用C#静态方法

编写脚本如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;

public class Test : MonoBehaviour {

// Use this for initialization
void Start()
{
string lua01 = @"Test.MethodTest01()";
LuaScriptMgr luaMgr = new LuaScriptMgr();
luaMgr.Start();
LuaState luastate = luaMgr.lua;
luastate.DoString(lua01);
}

public static void MethodTest01()
{
Debug.Log("Hahahaha");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
然后下图中点击Clear LuaBinder file + wrap Files 


然后在uLua/Editor目录下面找到WrapFile文件,在binds静态数组中添加_GT(typeof(Test)),最后在上图中选择第二步Gen Lua Wrap Files,运行Unity即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: