在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
最近要学习热更新,搜了下,选择了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
|
请发表评论