系统:Windows 10
引擎:Unity 2017.2.1f1
插件:tolua
(1)下载tolua。
tolua 的 github 资源库查看, https://github.com/topameng/tolua
下载tolua-master.zip,https://github.com/topameng/tolua/archive/master.zip
(2)tolua-master压缩后,把其中的Assets目录内容复制到工程目录的Assets目录里面。
(3)做一个简单实例
(3-1)场景布局如下:
(3-2)加一个LuaHelper.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LuaHelper {
public static void PrintA(string strMsg)
{
GameObject go = GameObject.Find ("TextA");
Text txt = go.GetComponent<Text>();
txt.text = strMsg;
}
}
(3-3)把刚创建的类LuaHelper加载到Assets\Editor\Custom\CustomSettings.cs
//在这里添加你要导出注册到lua的类型列表
public static BindType[] customTypeList =
{
// 省略默认代码...
_GT(typeof(LuaHelper)),
};
(3-4)先用菜单Lua/Clear wrap files,清空生成wrap文件,再用菜单Lua/Generate Al,生成全部wrap文件。
(3-5)把Lua/Main.lua的Main函数内容改为
--主入口函数。从这里开始lua逻辑
function Main()
print("logic start")
require("MuyTest")
MuyTest:Init()
end
(3-6)添加MuyTest.lua文件到Lua目录下,代码内容为:
MuyTest = {}
function MuyTest:Init()
print("MuyTest")
LuaHelper.PrintA("aaaaaaaaaaa");
end
(3-7)Resources方式打包的话,参考官方例子TestCustomLoader。
目录:[工程目录]\Assets\ToLua\Examples\13_CustomLoader
点击菜单Lua/Copy Lua files to Resources,会把Lua文件复制到Resources目录。
(3-8)Bundle方式打包的话,参考官方例子TestABLoader。
目录:[工程目录]\Assets\ToLua\Examples\18_Bundle
先点击菜单Lua/Copy Lua files to Resources,再点击菜单Lua/Build bundle files not jit,会把Lua文件复制到temp/Lua目录。
(4)打包效果:
(4-1)Resources方式:
(4-2)Bundle方式:(Bundle方式尚未能用Lua调用CSharp接口,所以没有aaaaa,研究中…)
以上简单回顾。
参考资料:
Unity3D中tolua的“安装部署和使用“教程
http://www.cnblogs.com/rekcah1986/p/6846425.html
toLua简单使用
https://blog.csdn.net/u012941657/article/details/51671854
用好Lua+Unity,让性能飞起来——Lua与C#交互篇
http://www.cnblogs.com/zwywilliam/p/5999924.html
http://www.cnblogs.com/zwywilliam/p/5999980.html
安卓报错LuaException: cannot open tolua.lua: No such file or directory
https://github.com/topameng/tolua/issues/75