lua脚本 打包与读取

一、打包

  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  

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值