uLua学习笔记(一):uLua安装及上手

uLua下载:http://www.ulua.org/

VS2012/2013的用于编写Lua的插件:https://babelua.codeplex.com/http://unknownworlds.com/decoda/

在下载了uLua_vX.XX.zip后解压得到一个XXX.unitypackage文件,将该文件导入到我们的工程中即可使用uLua了。

我们先来一个最简单的示例:

 1 using UnityEngine;
 2 using System.Collections;
 3 using LuaInterface;
 4 
 5 public class LuaTest : MonoBehaviour
 6 {
 7     void Start ()
 8     {
 9         LuaState luaState = new LuaState();
10         luaState.DoString("print('hello world 世界')");
11     }
12 }

将该脚本绑定到场景中的一个GameObject之上就可以看到效果了。

下面我们看一个加载外部lua文件的例子:

我们新建一个Resources目录,在目录里创建一个名为Test.lua.txt的文件,输入lua代码:

1 print("This is a script from a file 世界")

保存为UTF-8格式,注意Unity的TextAsset不支持lua的后缀名,所以后缀名要修改为txt。

修改上面的示例为下面的代码即可:

 1 using UnityEngine;
 2 using System.Collections;
 3 using LuaInterface;
 4 
 5 public class LuaLoadFileTest : MonoBehaviour
 6 {
 7     void Start ()
 8     {
 9         TextAsset luaString = Resources.Load<TextAsset>("Test.lua");
10 
11         LuaState luaState = new LuaState();
12         luaState.DoString(luaString.text);
13     }
14 }

注意Load的文件是不带后缀名的。

下面的例子使用uLua创建一个GameObject:

 1 using UnityEngine;
 2 using System.Collections;
 3 using LuaInterface;
 4 
 5 public class LuaTest : MonoBehaviour
 6 {
 7     private string lua = @"
 8             --加载模块
 9             luanet.load_assembly('UnityEngine')
10             luanet.load_assembly('Assembly-CSharp')
11 
12             --导入 Unity3D 的类
13             Util = luanet.import_type('Util')
14             GameObject = luanet.import_type('UnityEngine.GameObject')
15 
16             --创建一个新的 GameObject 对象
17             local newGameObj = GameObject('NewObj')
18             --添加粒子组件
19             Util.AddComponent(newGameObj, 'UnityEngine', 'ParticleSystem')
20         ";
21 
22     void Start ()
23     {
24         LuaState luaState = new LuaState();
25         LuaScriptMgr._translator = luaState.GetTranslator();
26         luaState.DoString(lua);
27     }
28 }

下面的例子是设置和获取Lua中的变量:

 1 using UnityEngine;
 2 using System.Collections;
 3 using LuaInterface;
 4 
 5 public class LuaTest : MonoBehaviour
 6 {
 7     private string lua = @"
 8             --加载模块
 9             luanet.load_assembly('UnityEngine')
10             luanet.load_assembly('Assembly-CSharp')
11 
12             --导入 Unity3D 的类
13             Util = luanet.import_type('Util')
14             GameObject = luanet.import_type('UnityEngine.GameObject')
15 
16             --创建表
17             particles = {}
18 
19             --循环
20             for i = 1, Objs2Spawn, 1 do
21                 --创建 GameObject 对象
22                 local newGameObj = GameObject('NewObj' .. tostring(i))
23                 --添加组件
24                 local ps = Util.AddComponent(newGameObj, 'UnityEngine', 'ParticleSystem')
25                 --暂停粒子播放
26                 ps:Stop()
27 
28                 --加入表
29                 table.insert(particles, ps)
30             end
31 
32             --定义一个数据
33             var2read = 42
34         ";
35 
36     void Start ()
37     {
38         LuaState luaState = new LuaState();
39         LuaScriptMgr._translator = luaState.GetTranslator();
40 
41         //赋值 Lua 中的数据
42         luaState["Objs2Spawn"] = 5;
43 
44         luaState.DoString(lua);
45 
46         //读取 Lua 中的数据
47         print("Read from lua: " + luaState["var2read"].ToString());
48 
49         //获取 LuaTable 对象
50         LuaTable particles = (LuaTable)luaState["particles"];
51 
52         //遍历播放粒子
53         foreach(ParticleSystem ps in particles.Values)
54         {
55             ps.Play();
56         }
57     }
58 }

下面看看Unity如何调用uLua中的函数:

 1 using UnityEngine;
 2 using System.Collections;
 3 using LuaInterface;
 4 
 5 public class LuaTest : MonoBehaviour
 6 {
 7     private string lua = @"
 8             --定义一个函数
 9             function luaFunc(message)
10                 print(message)
11                 return 42
12             end
13         ";
14 
15     void Start ()
16     {
17         LuaState luaState = new LuaState();
18         LuaScriptMgr._translator = luaState.GetTranslator();
19         
20         //运行脚本确保函数已经创建
21         luaState.DoString(lua);
22 
23         //获取函数
24         LuaFunction func = luaState.GetFunction("luaFunc");
25 
26         //调用函数
27         object[] result = func.Call("I called a lua function!");
28 
29         //获取结果
30         print(result[0]);
31     }
32 }

下面我们看看lua和U3D之间是如何相互传递数组:

 1 using UnityEngine;
 2 using System.Collections;
 3 using LuaInterface;
 4 using System;
 5 
 6 public class LuaTest : MonoBehaviour
 7 {
 8     private string lua = @"
 9         --定义一个函数
10         function luaFunc(objs, len)
11             for i = 0, len - 1 do
12                 print(objs[i])
13             end
14             --返回一个列表
15             local table1 = {'111', '222', '333'}
16             return table1
17         end
18     ";
19 
20     string[] objs = { "aaa", "bbb", "ccc" };
21 
22     void Start ()
23     {
24         LuaScriptMgr luaMgr = new LuaScriptMgr();
25         LuaState luaState = luaMgr.lua;
26         luaState.DoString(lua);
27 
28         //调用lua的函数获取返回值
29         LuaFunction f = luaState.GetFunction("luaFunc");
30         object[] rs = f.Call(objs, objs.Length);
31 
32         //输出lua的返回值
33         LuaTable table = rs[0] as LuaTable;
34         foreach (DictionaryEntry de in table)
35         {
36             Debug.Log(de.Value);
37         }
38     }
39 }

接下来我们看看Lua里的协程:

 1 using UnityEngine;
 2 using System.Collections;
 3 using LuaInterface;
 4 
 5 public class LuaCoroutines : MonoBehaviour {
 6 
 7     private string script = @"
 8             --导入程序集
 9             luanet.load_assembly('UnityEngine')
10             --导入类型
11             local Time = luanet.import_type('UnityEngine.Time')
12 
13             --使运行暂停指定的时间, 每帧调用
14             function waitSeconds(t)
15                 --获得结束时间
16                 local timeStamp = Time.time + t
17                 --时间没到就 yield 中断
18                 while Time.time < timeStamp do
19                     coroutine.yield()
20                 end
21             end
22 
23             --外部调用的方法
24             function myFunc()
25                 print('Coroutine started')
26                 local i = 0
27                 for i = 0, 10, 1 do
28                     print(i)
29                     waitSeconds(1)
30                 end
31                 print('Coroutine ended')
32             end
33         ";
34 
35     private LuaThread co = null;
36 
37     void Start () {
38         LuaState l = new LuaState();
39         LuaScriptMgr._translator = l.GetTranslator();
40 
41         //创建函数
42         l.DoString(script);
43 
44         //获取函数
45         LuaFunction f = l.GetFunction("myFunc");
46 
47         //创建协同程序
48         co = new LuaThread(l, f);
49 
50         //启动协同程序
51         co.Start();
52     }
53     
54     void Update () {
55         if( !co.IsDead() )
56         {
57             //协同程序需要每帧进行调用
58             co.Resume();
59         }
60         else
61         {
62             print("Coroutine has exited.");
63 
64             //清除协同程序
65             co = null;
66         }
67     }
68 }
原文地址:https://www.cnblogs.com/hammerc/p/4484493.html