最近要學習熱更新,搜了下,選擇了ulua這個插件,本人也是新人。對這個插件也是一知半解,不過幸好加了專門討論這一塊的群,這個群的技術氛圍還是很濃重的,特別是已經形成了一套自己的lua學習框架。最近周末就抽空研究了一下。
群號這里分享一下給大家 Unity3D&uLua技術交流群 341746602
開始這篇我希望你對熱更新有一定了解,並且對ulua有初步的嘗試。
一、打包
lua的后綴是不被支持打包進assertbundle的,所以我們一般把 .lua后綴 變為.lua.txt 或者 .lua.bytes 進行打包。
這里我就直接使用了框架的代碼
1 [MenuItem("Lua/Build Lua without jit", false, 2)] 2 public static void BuildLuaNoJit() 3 { 4 BuildAssetBundleOptions options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle; 5 6 /// 清零掉這個 目錄下的 * .bytes 文件 方便重新生成 7 string[] files = Directory.GetFiles("Assets/Lua/Out", "*.lua.bytes"); 8 9 for (int i = 0; i < files.Length; i++) 10 { 11 FileUtil.DeleteFileOrDirectory(files[i]); 12 } 13 14 /// convert files whth *.lua format to .bytes format 15 /// then move to Application.dataPath + "/Lua/Out/" 16 files = Directory.GetFiles(Application.dataPath + "/Lua/", "*.lua", SearchOption.TopDirectoryOnly); 17 18 for (int i = 0; i < files.Length; i++) 19 { 20 string fname = Path.GetFileName(files[i]); 21 FileUtil.CopyFileOrDirectory(files[i], Application.dataPath + "/Lua/Out/" + fname + ".bytes"); 22 } 23 24 AssetDatabase.Refresh(); 25 26 //use list to collect files whith *.bytes format 27 files = Directory.GetFiles("Assets/Lua/Out", "*.lua.bytes"); 28 List<Object> list = new List<Object>(); 29 for (int i = 0; i < files.Length; i++) 30 { 31 Object obj = AssetDatabase.LoadMainAssetAtPath(files[i]); 32 list.Add(obj); 33 } 34 35 ///buildpipeline with list 36 if (files.Length > 0) 37 { 38 string output = string.Format("{0}/Bundle/Lua.unity3d", Application.dataPath); 39 BuildPipeline.BuildAssetBundle(null, list.ToArray(), output, options, EditorUserBuildSettings.activeBuildTarget); 40 string output1 = string.Format("{0}/{1}/Lua.unity3d", Application.persistentDataPath, GetOS()); 41 FileUtil.DeleteFileOrDirectory(output1); 42 Directory.CreateDirectory(Path.GetDirectoryName(output1)); 43 FileUtil.CopyFileOrDirectory(output, output1); 44 AssetDatabase.Refresh(); 45 } 46 47 UnityEngine.Debug.Log("編譯lua文件結束"); 48 }
在框架里我新加了一個myTest.lua 文件
--region NewFile_1.lua --Author : admin --Date : 2015/1/18 --此文件由[BabeLua]插件自動生成 print("This is a script from a file") --endregion
然后運用腳本進行批處理,可以看到我已經打完完畢進 Lua 的 *.unity3d 包里了。

二、讀取
一般包內的初始資源從streamAssert中 加載 到 persistpath ,然后之后就從服務器更新 等等之類,超出本文的范圍。
我這里就演示下 使用 www 從streamAsserts 路徑讀取 這個 assetbundle。
至於什么要從www,這個適用於全平台
1 #if UNITY_EDITOR 2 string dbpath = "file://" + Application.streamingAssetsPath + "/" + dbfilename 3 //or string path = "file://" + Application.dataPath + "/StreamingAssets" + "/" + "dbfilename"; 4 #elif UNITY_IPHONE 5 string dbpath = "file://" + Application.streamingAssetsPath + "/" + "dbfilename"; 6 //or string path = "file://" + Application.dataPath + "/Raw" + "/" + dbfilename"; 7 #elif UNITY_ANDROID 8 string dbpath = Application.streamingAssetsPath + "/" + dbfilename; 9 //or string path = "jar:file://" + Application.dataPath + "!/assets/" + "/dbfilename"; 10 #endif
1 using UnityEngine; 2 using System.Collections; 3 using LuaInterface; 4 5 public class Test : MonoBehaviour 6 { 7 void OnGUI() 8 { 9 if (GUI.Button(new Rect(10, 10, 150, 100), "TestRead")) StartCoroutine(TestRead()); 10 11 if (GUI.Button(new Rect(10, 200, 150, 100), "TestRead2")) TestRead2(); 12 } 13 14 15 IEnumerator TestRead() 16 { 17 Debug.Log("TestRead"); 18 19 string path = "file://" + Application.streamingAssetsPath + "/Lua.unity3d"; 20 21 WWW www = new WWW(path); 22 23 yield return www; 24 25 Debug.Log("load finish"); 26 27 AssetBundle bundle = www.assetBundle; 28 29 TextAsset text = bundle.Load("myTest.lua") as TextAsset; 30 31 32 if(text != null){ 33 LuaState lu = new LuaState(); 34 lu.DoString(text.text); 35 } 36 else{ 37 Debug.LogError("text == null"); 38 } 39 } 40 41 42 public TextAsset tmp; //reference myTest.lua 43 44 void TestRead2() 45 { 46 Debug.Log("TestRead2"); 47 48 LuaState lu = new LuaState(); 49 50 lu.DoString(tmp.text); 51 } 52 }
如下結果,我們成功實現了從assertbundle讀取腳本

問題:
這里不知道為什么,你點擊TestRead 之后 再點一次會報個錯誤。
The asset bundle 'file://C:/Users/admin/Desktop/HotUpdate/CsToLua-master/CsToLua-master/tolua/Assets/StreamingAssets/Lua.unity3d' can't be loaded because another asset bundle with the same files are already loaded
原因如下:http://docs.unity3d.com/Manual/keepingtrackofloadedassetbundles.html

