您的位置:首页 > 移动开发 > Unity3D

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

2015-11-23 14:34 405 查看
开始前必须做好准备,就是决定数据究竟用什么类型好。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 ();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: