项目记录04--客户端unity,服务端erlang--打包数据csv

来源:互联网 发布:mac桌面提醒便签 编辑:程序博客网 时间:2024/06/06 18:56

开始前必须做好准备,就是决定数据究竟用什么类型好。xml,json,csv,tex.....最终决定使用csv,阅读性肯定没有其它好,好处就是比较小,打出包比较小。

先写个打包工具:

核心代码:

[MenuItem("GameTool/BuildAssets/Build_CSV")]
public static void BuildCSV()
{
        //Path 先读取-->序列号ScriptableObject 将些不能打包的比如string等转化 -->生成asset AssetDataBase -->打包AssetBundle
        string csvReadPath = applicationPath + "/DataTable/CSV/";  //读取的CSV目录
        string assetPathTemp = "Assets/Local/temp/";      //临时生成的createAsset二进制文件目录
        string saveAssetBundleName = "csv.assetbundle";            //生成AssetBundle名称
        string savePath = saveDir + saveAssetBundleName;           //生成保存的目录

    #region 固定目录打包
        //拿到文件
        List<Object> outs = new List<Object>();
        string[] allFilesPath = Directory.GetFiles(csvReadPath);
        for (int i = 0, max = allFilesPath.Length; i < max; i++ )
        {
            string filesPath = allFilesPath[i];
            //过滤文件.csv
            if (filesPath.Substring(filesPath.LastIndexOf(".") + 1) != "csv")
                continue;
            string fileName = filesPath.Substring(filesPath.LastIndexOf("/") + 1).Split('.')[0];


            //序列化
            soCSV csv = ScriptableObject.CreateInstance<soCSV>();
            csv.fileName = fileName;
            csv.content = File.ReadAllBytes(filesPath);


            //生成AssetDatabase临时中间文件二进制
            string TempPath = assetPathTemp + fileName + ".asset";
            AssetDatabase.CreateAsset(csv, TempPath);


            //取出来丢进outs里面等待打包
            Object outTemp =  AssetDatabase.LoadAssetAtPath(TempPath,typeof(soCSV));
            outs.Add(outTemp);
        }
        
    #endregion


        if (BuildPipeline.BuildAssetBundle(null, outs.ToArray(), savePath, BuildAssetBundleOptions.IgnoreTypeTreeChanges, PlatformUtil.Build_Target))
            EditorUtility.DisplayDialog("ok", "build " + savePath + " success,length = " + outs.ToString().Length, " ok");
        else
           Debug.LogWarning("build" + savePath + " failed");
 
  AssetDatabase.Refresh ();
}

0 0
原创粉丝点击