用ScriptableObject打包数据(以csv文件为例)
来源:互联网 发布:ubuntu 16.04 安装后 编辑:程序博客网 时间:2024/05/22 09:41
原文:http://blog.csdn.net/xv_ly15/article/details/9330617
简介
开发人员一般通过BuildPipeline函数去打包文件,然后通过WWW去下载
但是BuildPipeline打包的对象类型是有限制的,像GameObject,TextAsset这些文件是可以直接打包的,但是,如果要读取一些Unity不支持的类型,我们就需要用到ScriptableObject了。这里用打包Csv表作为例子示例一下
实现
1. 编辑ScriptObject对象
首先,csv表的内容就是一段字符串,又或者说是一段bytes,看程序需求,我这里把csv表的内容当成一段bytes读取,吧
所以,csv表的ScriptableObject可以这样写
- using UnityEngine;
- using System.Collections;
- public class soCsv : ScriptableObject
- {
- public string fileName;
- public byte[] content;
- }
2. 打包文件
有了Csv的ScriptableObject后,就可以开始写打包函数了
简单说一下流程:
1.获取选中的csv表对象
2.赋值到刚声明的csv表对应的ScriptableObject
3.用该ScriptableObject生成Asset
4.读取生成的Asset后打包(生成与读取asset的过程有点别扭,但只有这样获得的Object对象,才能打包成功...)
- using UnityEngine;
- using System.Collections;
- using UnityEditor;
- using System.IO;
- using System.Collections.Generic;
- public class BuildTest : MonoBehaviour
- {
- [MenuItem("Assets/Build Csv")]
- static void BuildCsv()
- {
- string applicationPath = Application.dataPath;
- string saveDir = applicationPath + "/Resources_out/";
- string savaPath = saveDir + "csv.unity3d";
- //获取选中的对象
- /*关于SelectionMode参数,目前我只用过Assets,DeepAssets,其他的暂且不说
- Assets:获取当前选中的单一对象
- DeepAssets:获取当前选中的对象,如果选中的文件夹,则会获取到文件夹的子对象
- *
- *
- Unfiltered : Return the whole selection.
- TopLevel : Only return the topmost selected transform. A selected child of another selected transform will be filtered out.
- Deep: Return the selection and all child transforms of the selection.
- ExcludePrefab : Excludes any prefabs from the selection.
- Editable :Excludes any objects which shall not be modified.
- Assets :Only return objects that are assets in the Asset directory.
- DeepAssets : If the selection contains folders, also include all assets and subfolders within that folder in the file hierarchy.
- *
- */
- Object[] selections = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- List<Object> outs = new List<Object>();
- for (int i=0,max=selections.Length; i<max; i++)
- {
- Object obj = selections[i];
- //asset path : 相对Asset目录
- string fileAssetPath = AssetDatabase.GetAssetPath(obj);
- //判断后缀
- if (fileAssetPath.Substring(fileAssetPath.LastIndexOf('.') + 1) != "csv")
- continue;
- //
- string fileWholePath = applicationPath + "/" + fileAssetPath.Substring(fileAssetPath.IndexOf("/"));
- //
- soCsv csv = ScriptableObject.CreateInstance<soCsv>();
- csv.fileName = obj.name;
- csv.content = File.ReadAllBytes(fileWholePath);
- //这一个存与读的步骤是否必要的?
- string assetPathTemp = "Assets/Resources_Local/Temp/" + obj.name + ".asset";
- AssetDatabase.CreateAsset(csv, assetPathTemp);
- Object outObj = AssetDatabase.LoadAssetAtPath(assetPathTemp, typeof(soCsv));
- //
- Debug.Log("package : " + outObj.name);
- outs.Add(outObj);
- }
- //
- Object[] outObjs = outs.ToArray();
- if (BuildPipeline.BuildAssetBundle(null, outs.ToArray(), savaPath))
- Debug.Log("build " + savaPath + " success,length = " + outObjs.Length);
- else
- Debug.LogWarning("build " + savaPath + " failed");
- }
- }
3. 下载并解析内容
- using UnityEngine;
- using System.Collections;
- using System.IO;
- using LumenWorks.Framework.IO.Csv;
- public class Test : MonoBehaviour {
- WWW w;
- string path ;
- void Start () {
- //File协议主要用于访问本地计算机中的文件,所以这里还需要加file:///
- path = "file:///" + Application.dataPath + "/Resources_out/csv.unity3d";
- w = new WWW(path);
- }
- // Update is called once per frame
- void Update () {
- if (w != null)
- {
- if (w.isDone)
- {
- Debug.Log("download " + path);
- Debug.Log(w.assetBundle.mainAsset);
- /*loadall的参数如果不指定类型,很可能会读取到额外的内容
- * 像我测试的时候,就发现一直多出了一个名字叫soCsv的对象
- * 最好还是指定一下类型
- * */
- Object[] objs = w.assetBundle.LoadAll(typeof(soCsv));
- Debug.Log("count : " + objs.Length);
- for (int i = 0, max = objs.Length; i < max; i++)
- {
- Object obj = objs[i];
- soCsv csv= obj as soCsv;
- //用string表做一个例子吧
- if (csv.fileName != "string")
- {
- continue;
- }
- //
- MemoryStream ms = new MemoryStream(csv.content);
- if (ms == null)
- {
- Debug.LogWarning("转换csv失败");
- continue;
- }
- StreamReader sr = new StreamReader(ms);
- TextReader tr = sr as TextReader;
- if (tr == null)
- {
- Debug.LogWarning("text reader is null");
- continue;
- }
- CsvReader cr = new CsvReader(tr, true);
- if (cr == null)
- {
- Debug.LogWarning("CsvReader is null");
- continue;
- }
- //将字段头输出
- string[] headers = cr.GetFieldHeaders();
- foreach (string szHeader in headers)
- Debug.Log(szHeader);
- }
- w = null;
- }
- }
- }
- }
0 0
- 用ScriptableObject打包数据(以csv文件为例)
- [Unity3D]用ScriptableObject打包数据
- Java读取本地文件,然后把数据存到mysql.(以csv文件为例)
- Unity ScriptableObject 自动导出 .csv文件
- sqlldr导入数据(以PostgreSql>>>Oracle为例)copy call to '/tmp/call.csv' DELIMITER ',' NULL '' CSV
- 导入以csv为后缀的文件
- 将数据导出为csv文件(需要javacsv.jar)
- 以时间为文件名打包文件
- 抓取数据为json文件,读入csv
- Ubuntu 解析以逗号“,”分割数据的csv文件
- Android Studio多渠道打包(以友盟为例)
- PHP把数组写进csv,和从csv文件以数组的形式读取数据
- 抓取网页信息并获取生成xml文件(以网页彩票数据为例)
- 用Advanced Installer8.7.1实现简单打包,以打包OCX安装包为例
- PHP批量导入和导出数据为CSV文件 (自适应版)
- oracle数据库的表数据导出为csv文件
- 从ldap xmpp拿数据保存为csv文件
- php数据转换为html table或者csv文件
- 兔子--SDK,ADT,AVD,IDE,ADB
- 分享基于phantomjs的web截图
- 网络采集软件核心技术剖析系列(2)---如何使用C#语言获得任意站点博文的正文及标题
- Heritrix在Eclipse下的配置方法
- CSS学习笔记一
- 用ScriptableObject打包数据(以csv文件为例)
- 网页背景图固定不动,不跟随滚动条滚动
- cmd命令卸载ie9、10、11
- Sqoop导入关系数据库到Hive
- 从两道经典试题谈C/C++中联合体(union)的使用
- C#日期只显示年月日
- nRF51822 ADC PWM
- 不要再翻译计算机单词了
- 浏览器兼容 IE6、IE7、IE8、Firefox、Opera CSS hack区分