lua脚本 打包与读取
来源:互联网 发布:农业科技网络书屋app 编辑:程序博客网 时间:2024/06/16 05:39
一、打包
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 format15 /// 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.lua43 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
- lua脚本 打包与读取
- C++读取lua脚本
- Redis与Lua脚本
- Redis 与 Lua 脚本
- Redis 与 Lua 脚本
- nginx用lua脚本读取redis(脚本篇)
- 打包脚本使用与分析
- nginx用lua脚本读取redis(安装篇)
- Lua与C——执行Lua脚本(二)
- redis管道、事物与lua脚本
- redis与lua脚本的结合使用
- Unity3d之AssetBundle打包与读取
- [Unity基础]打包与读取AssetBundle
- Unity3d之AssetBundle打包与读取
- Unity3d之AssetBundle打包与读取
- Unity3d之AssetBundle打包与读取
- lua脚本
- LUA脚本
- 一碗毒鸡汤,让你活的更通透
- CSS3D--球体
- js中window.location.search的用法和作用。
- 关于js动态生成元素,注册(绑定)的事件失效的解决(on)
- 【物联网】 Yeelink开发平台注册
- lua脚本 打包与读取
- 参数估计
- android imageview 图片不能正常显示 或不能自适应的解决方法
- 合并排序解析
- Beautiful Soup的使用
- linux基本命令2--文件处理
- volatile原理
- CodeForces
- 事件循环机制