LuaConst下的luaDir------------自己写的lua文件夹
toluaDir---------------tolua文件夹,tolua下lua文件夹不能移动。
两个地址一定要写对
在editor模式下,LuaState会自动把这两个地址进行AddSearchPath,但是在android机下,因为lua文件不会打进resource下,放进streamingpath下也不行,
因为android会使用file.exist来寻找lua文件,全都复制到persistentDataPath不知也为何还是找不到,只能尝试打bundle包。是不是真机下lua必须打包成bundle?
LuaFileUtils里有个beZip需要设置为true。
并且需要在运行lua文件前主动将所需要的文件全都预先加再进去。
public void AddBundle(string bundleName) {
string url = Application.streamingAssetsPath+"/"+ GM.sysPlatform+"/"+ bundleName.ToLower();
Debug.Log (url);
// if (File.Exists(url)) {
AssetBundle bundle = AssetBundle.LoadFromFile(url);
if (bundle != null)
{
bundleName = bundleName.Replace("lua/", "").Replace(".unity3d", "");
Debug.Log (bundleName);
base.AddSearchBundle(bundleName.ToLower(), bundle);
}
// }
}
unity5后的打包方式是一个文件一个assetbundle,如果lua文件也照此来,那么预先加载lua文件的代码就会变得很繁琐,只能选择将多个lua文件打包成一个bundle,就是像unity5以前的打包方式,LuaFramework_NGUI选择的就是这种方式,一个文件夹打包成一个bundle,根目录下的汇集打成lua.unity3d
public static void packLua(){
string luaPath = Application.dataPath + "/StreamingAssets/"+buildTarget+"/"+"lua/";
//----------复制Lua文件----------------
if (!Directory.Exists(luaPath)) {
Directory.CreateDirectory(luaPath);
}
string[] luaPaths = { Application.dataPath + "/ModuleRes/lua/",
Application.dataPath + "/Tolua/Lua/" };
string streamDir = Application.dataPath + "/lua/" ;
CopyLuaBytesFiles(LuaConst.luaDir, streamDir);
CopyLuaBytesFiles(LuaConst.toluaDir, streamDir);
AssetDatabase.Refresh();
string[] dirs = Directory.GetDirectories(streamDir, "*", SearchOption.AllDirectories);
for (int i = 0; i < dirs.Length; i++) {
string str = dirs[i].Remove(0, streamDir.Length);
BuildLuaBundle(str);
}
BuildLuaBundle(null);
Directory.Delete(streamDir, true);
AssetDatabase.Refresh();
string resPath = Application.dataPath + "/StreamingAssets/"+buildTarget;
///----------------------创建文件列表-----------------------
string newFilePath = resPath + "/files.txt";
if (File.Exists(newFilePath)) File.Delete(newFilePath);
paths.Clear(); files.Clear();
Recursive(luaPath);
FileStream fs = new FileStream(newFilePath, FileMode.CreateNew);
StreamWriter sw = new StreamWriter(fs);
for (int i = 0; i < files.Count; i++) {
string file = files[i];
string ext = Path.GetExtension(file);
if (file.EndsWith(".meta") || file.Contains(".DS_Store")) continue;
string md5 = myspace.LuaUtil.md5file(file);
string value = file.Replace(resPath, string.Empty);
sw.WriteLine(value + "|" + md5);
}
sw.Close(); fs.Close();
}
static void BuildLuaBundle(string dir) {
BuildAssetBundleOptions options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets |
BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.UncompressedAssetBundle;
string path = "Assets/lua/" + dir;
string[] files = Directory.GetFiles(path, "*.lua.bytes");
List
if (files.Length > 0) {
string output = Application.streamingAssetsPath +"/"+ buildTarget+ "/lua/" + bundleName;
if (File.Exists(output)) {
File.Delete(output);
}
BuildPipeline.BuildAssetBundle(null, list.ToArray(), output, options, EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh();
}
}
public static void Recursive(string path) {
string[] names = Directory.GetFiles(path);
string[] dirs = Directory.GetDirectories(path);
foreach (string filename in names) {
string ext = Path.GetExtension(filename);
if (ext.Equals(".meta")) continue;
files.Add(filename.Replace('\\', '/'));
}
foreach (string dir in dirs) {
paths.Add(dir.Replace('\\', '/'));
Recursive(dir);
}
}
static void CopyLuaBytesFiles(string sourceDir, string destDir, bool appendext = true) {
if (!Directory.Exists(sourceDir)) {
return;
}
string[] files = Directory.GetFiles(sourceDir, "*.lua", SearchOption.AllDirectories);
int len = sourceDir.Length;
if (sourceDir[len - 1] == '/' || sourceDir[len - 1] == '\\') {
--len;
}
for (int i = 0; i < files.Length; i++) {
string str = files[i].Remove(0, len);
string dest = destDir + str;
if (appendext) dest += ".bytes";
string dir = Path.GetDirectoryName(dest);
Directory.CreateDirectory(dir);
File.Copy(files[i], dest, true);
}
}
只有这样一来,一个项目中有两种打包方式。
////////////////////////////----------------------------利用Manifest中的列表找到所有lua文件
还是按unity5的打包方式,将所有lua文件复制到你要打包的目录下,记得改后缀名,然后在启动时遍历Manifest
AssetBundle newmanifestBundle=AssetBundle.LoadFromFile(outputPath);
AssetBundleManifest manifest=(AssetBundleManifest)newmanifestBundle.LoadAsset("AssetBundleManifest");
string[] abs =manifest.GetAllAssetBundles();
获得所有带有lua的bundle一一加载,AddBundle。