Unity 导出 Anroid 之文本文件读取问题
来源:互联网 发布:薛之谦林俊杰知乎 编辑:程序博客网 时间:2024/06/05 22:32
老样子,先给出用到的参考文章
参考1:Unity Assets目录下的特殊文件夹名称(作用和是否会被打包到build中)
http://blog.csdn.net/ycl295644/article/details/47683603
参考2:unity3d项目文件目录发布后,对应的ios/android应用目录
http://blog.csdn.net/w88193363/article/details/41288449
目的:游戏最初的一些配置信息,希望从文件中去读取,那么又希望不被修改,那么从“”参考1“”来看,我作出的选择是 StreamingAssets(自动被build包含,只读)
参考2中所列目录有一些小问题,我自己获取的路径如下
PC端输出文件:
Person : internet, 32Person : ttyo, 77[{"name":"internet","age":32},{"name":"ttyo","age":77}]data path :G:/PYM/Demos/PYM_Git/PYM_GIT/Assetsstream path :G:/PYM/Demos/PYM_Git/PYM_GIT/Assets/StreamingAssetscache path :C:/Users/ADMINI~1/AppData/Local/Temp/DefaultCompany/PYM_GITpst path :C:/Users/Administrator/AppData/LocalLow/DefaultCompany/PYM_GIT
ANDROID端输出文件:
Person : internet, 32Person : ttyo, 77[{"name":"internet","age":32},{"name":"ttyo","age":77}]data path :/data/app/com.DAV.PYM-1/base.apkstream path :jar:file:///data/app/com.DAV.PYM-1/base.apk!/assetscache path :/storage/emulated/0/Android/data/com.DAV.PYM/cachepst path :/storage/emulated/0/Android/data/com.DAV.PYM/files
说明:
data path = dataPath
stream path = StreamingAssetsPath
cache path = temporaryCachePath
pst path = PersistentDataPath
ANDROID端 StreamingAssetsPath 路径存在 jar:file:///
而PC端 StreamingAssetsPath 则不存在,
存在协议的部分需要使用 WWW 读取文件,PC端如果使用 WWW 读取文件,只需要增加 file:/// 协议即可
string readFileTemp = Application.streamingAssetsPath + "/" + readFile;
#if UNITY_ANDROID
//readFileTemp = readFileTemp;
#elif UNITY_STANDALONE
readFileTemp = "file:///" + readFileTemp;
#endif
这里一定要注意!!!如果没有添加 file:/// 协议,会报 unsupported protocal
WWW cfgFileWWW = new WWW(readFileTemp); yield return cfgFileWWW; if (null == cfgFileWWW || null != cfgFileWWW.error) { Debug.LogFormat("www download file error : {0}", cfgFileWWW.error); yield break; } dataString = cfgFileWWW.text; // 使用这种方式来获取文本文件内容使用 WWW 时,注意需要该函数返回值写为 IEnumerator (迭代器,这里不展开)
最后,附完整代码
class Person { public Person(string name, int age) { this.name = name; this.age = age; } public string name = "default"; public int age = 0; public override string ToString() { return "Person : " + name + ", " + age.ToString(); } } IEnumerator Start() { string output = ""; // 准备输出的字符串 string dataString = ""; //从文件中读取的内容 string readFileTemp = Application.streamingAssetsPath + "/" + readFile; string writeFileTemp = Application.persistentDataPath + "/" + writeFile; try { wfs = new FileStream(writeFileTemp, FileMode.Create); wt = new StreamWriter(wfs); } catch { Debug.Log("write file open error."); yield break; } if (null == wfs || null == wt) { Debug.Log("write file null."); yield break; }#if UNITY_ANDROID //readFileTemp = readFileTemp;#elif UNITY_STANDALONE readFileTemp = "file:///" + readFileTemp;#endif WWW cfgFileWWW = new WWW(readFileTemp); yield return cfgFileWWW; if (null == cfgFileWWW || null != cfgFileWWW.error) { Debug.LogFormat("www download file error : {0}", cfgFileWWW.error); yield break; } else { Debug.Log("www download file success."); } dataString = cfgFileWWW.text; // 使用这种方式来获取文本文件内容 Person[] persons = JsonConvert.DeserializeObject<Person[]>(dataString); foreach (var p in persons) { output += p.ToString() + '\n'; } output += JsonConvert.SerializeObject(persons) + '\n'; wt.Write(output); wt.WriteLine("data path :" + Application.dataPath); wt.WriteLine("stream path :" + Application.streamingAssetsPath); wt.WriteLine("cache path :" + Application.temporaryCachePath); wt.WriteLine("pst path :" + Application.persistentDataPath); wt.Flush(); if (null != rfs) rfs.Close(); if (null != rd) rd.Close(); if (null != wfs) wfs.Close(); if (null != wt) wt.Close(); }
这里也是测试下 JSON.NET 能不能在 ANDROID 下如愿工作,答案是可以的(大神勿喷,我才入门)
- Unity 导出 Anroid 之文本文件读取问题
- Unity发布WebGl逐行读取本地文本文件
- NET之C#读取文本文件
- 文本文件中读取中文乱码的问题
- java 读取文本文件的乱码问题???
- Java读取文本文件中文乱码问题
- 【Q&A】getline读取文本文件的问题
- Java读取文本文件中文乱码问题
- Java读取文本文件中乱码问题
- 字符流读取文本文件的编码问题
- Java读取文本文件中文乱码问题
- Java读取文本文件中文乱码问题
- 关于java读取文本文件乱码的问题
- Java读取文本文件中文乱码问题
- Java读取文本文件中文乱码问题
- Java读取文本文件中文乱码问题
- java编程之从服务器读取文本文件
- unity导出IOS资源问题
- PHP jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图等
- RFID学习笔记(持续更新)
- iPhone X搭载3D感测功能,VCSEL是个什么鬼?
- 阿里Json解析
- Oauth 验证用户授权登录 几点总结
- Unity 导出 Anroid 之文本文件读取问题
- 【知识发现】基于用户的协同过滤推荐算法python实现
- Eclipse使用SVN,停用自动保存密码功能。删除eclipse自动保存的SVN信息
- 数据正倒显示
- 最大连续和(分治法)
- 大四求职历程(一)
- AD系列软件不小心关闭了左边菜单这么恢复
- 【Linux-运维】df和du的命令区分
- 负载均衡随机跳转子机地址,某服务器未打包(版本),导致自动批次总访问失败异常