Untiy之Android平台读写遇到的坑
来源:互联网 发布:淘宝商品详情页怎么做 编辑:程序博客网 时间:2024/04/30 12:42
关注公众号,获取更多干货。
最近在做一个在Android平台频繁读写的一个项目,加载音乐的时候需要用WWW加载返回的w.audioClip,而读写xml的时候则需要用IO流读写。
于是我发现。。。Android平台路径的坑可真是不小啊
首先在Unity平台先看一下Windows系统下,各个文件夹的路径:
这里我把Unity里能打印的四个自带里文件夹路径都打印出来了,而其实,我常用的只有streamingAssetsPath和persistentDataPath
streamingAssetsPath是可以跟随Unity打包存在的文件夹,WWW和IO都可以访问这个文件夹下的文件,但是这个文件夹是只读文件夹,不支持修改文件和写入等操作。
persistentDataPath是可读写的文件夹,但是这个文件夹是在APK安装成功后创建的,所以无法将文件在打包前放入此文件夹。
那么接下来我们看看Android平台的这些文件夹路径都是什么样的。
下面开始分享我的经验
一、 如果用WWW去加载文件:
在Windows端访问streamingAssetsPath文件夹则需要在前面加上”file:///”协议,而Android端则不用加file协议,因为Android端的streamingAssetsPath路径Unity会自动为他添加jar:file://。
同样,如果是persistentDataPath文件夹,在Windows端一样需要加”file:///”协议 ,而Android端的persistentDataPath文件夹Unity则没有像streamingAssetsPath一样,添加了jar:file://,所以,我们要在此路径前自己添一下file协议(PS:Android端的file协议需要在file前添加”jar:”)。
二、 如果用IO流去读写文件:
不管是Windows端还是Android端都是不需要file协议的,但是streamingAssetsPath在Android端不支持IO流访问,Windows端直接使用streamingAssetsPath即可。
而persistentDataPath路径不管是Windows端还是Android端,都可以直接使用,不需要添加file协议。
光看文字未免有些不爽,我自己也写了一个小Demo供大家参考。
以下是代码与工程文件。
using System.IO;using UnityEngine;using System.Collections;using UnityEngine.UI;public class Aries_UIManager : MonoBehaviour{ public Text myConsole; /// <summary> /// 显示各个文件夹路径 /// </summary> public void showPath() { log("dataPath: ======> " + Application.dataPath); log("persistentDataPath: ======> " + Application.persistentDataPath); log("streamingAssetsPath: ======> " + Application.streamingAssetsPath); log("temporaryCachePath: ======> " + Application.temporaryCachePath); } /// <summary> /// 将StreamingAssets里的文件复制到PersistentDataPath下 /// </summary> public void copyFileToPersistentDataPath() { if (Application.platform == RuntimePlatform.Android) //如果是Android平台 { StartCoroutine(copyFile_Android("NowYouSeeMe.mp3")); StartCoroutine(copyFile_Android("Test.txt")); } else if (Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平台 { File.Copy(Application.streamingAssetsPath + "/NowYouSeeMe.mp3", Application.persistentDataPath + "/NowYouSeeMe.mp3",true); log("CopyMusic Success!" + "\n" + "Path: ======> " + Application.persistentDataPath + "/NowYouSeeMe.mp3"); File.Copy(Application.streamingAssetsPath + "/Test.txt", Application.persistentDataPath + "/Test.txt",true); log("CopyTxt Success!" + "\n" + "Path: ======> " + Application.persistentDataPath + "/Test.txt"); } } /// <summary> /// 从StreamingAssets加载背景音乐 /// </summary> public void LoadMusicByStreamingAssets() { StartCoroutine(LoadMusicByStreamingAssets_IEnumerator()); } /// <summary> /// 从PersistentDataPath加载背景音乐 /// </summary> public void LoadMusicByPersistentDataPath() { StartCoroutine(LoadMusicByPersistentDataPath_IEnumerator()); } /// <summary> /// 从StreamingAssets加载文本文件 /// </summary> public void LoadTxtByStreamingAssets() { StartCoroutine(LoadTxtByStreamingAssets_Ienumerator()); } /// <summary> /// 从PersistentDataPath加载文本文件 /// </summary> public void LoadTxtByPersistentDataPath() { FileInfo fi = new FileInfo(Application.persistentDataPath + "/Test.txt"); //判断文件是否存在 if (fi.Exists) { log("File Exists! Began To Read."); log("File Path : ======> " + Application.persistentDataPath + "/Test.txt"); StreamReader sr = fi.OpenText(); string data = sr.ReadToEnd(); log("Txt Content : ======> " + data); } else { log("<color=red>File Does Not Exist</color>"); } } //=============================================================================== IEnumerator LoadMusicByStreamingAssets_IEnumerator() { string filePath=""; if (Application.platform == RuntimePlatform.Android) //如果是Android平台 { filePath = Application.streamingAssetsPath + "/NowYouSeeMe.mp3"; } else if(Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平台 { filePath = "file:///" + Application.streamingAssetsPath + "/NowYouSeeMe.mp3"; } WWW w=new WWW(filePath); yield return w; if (w.error == null) { //获取Unity音源文件 AudioClip ac = w.audioClip; //赋值到Unity播放器 GetComponent<AudioSource>().clip = ac; //播放 GetComponent<AudioSource>().Play(); log("PlayMusic By StreamingAssetsPath..."); } else { log("Error : ======> " + w.error); } } IEnumerator LoadMusicByPersistentDataPath_IEnumerator() { string filePath = ""; if (Application.platform == RuntimePlatform.Android) //如果是Android平台 { filePath = "file://" + Application.persistentDataPath + "/NowYouSeeMe.mp3"; } else if (Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平台 { filePath = "file:///" + Application.persistentDataPath + "/NowYouSeeMe.mp3"; } WWW w = new WWW(filePath); yield return w; if (w.error == null) { //获取Unity音源文件 AudioClip ac = w.audioClip; //赋值到Unity播放器 GetComponent<AudioSource>().clip = ac; //播放 GetComponent<AudioSource>().Play(); log("PlayMusic By PersistentDataPath..."); } else { log("Error : ======> " + w.error); } } /// <summary> /// WWW加载Txt /// </summary> /// <returns></returns> IEnumerator LoadTxtByStreamingAssets_Ienumerator() { string filePath = ""; if (Application.platform == RuntimePlatform.Android) //如果是Android平台 { filePath = Application.streamingAssetsPath + "/Test.txt"; } else if (Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平台 { filePath = "file:///" + Application.streamingAssetsPath + "/Test.txt"; } WWW w = new WWW(filePath); yield return w; if (w.error == null) { log("File Exists! Began To Read."); log("File Path : ======> " + Application.streamingAssetsPath + "/Test.txt"); string data = System.Text.Encoding.UTF8.GetString(w.bytes); log("Txt Content : ======> " + data); } else { log("Error : ======> " + w.error); } } /// <summary> /// 安卓端复制文件 /// </summary> /// <param name="fileName">文件路径</param> /// <returns></returns> IEnumerator copyFile_Android(string fileName) { WWW w=new WWW(Application.streamingAssetsPath + "/" + fileName); yield return w; if (w.error == null) { FileInfo fi=new FileInfo(Application.persistentDataPath + "/" +fileName); //判断文件是否存在 if (!fi.Exists) { FileStream fs = fi.OpenWrite(); fs.Write(w.bytes,0,w.bytes.Length); fs.Flush(); fs.Close(); fs.Dispose(); log("CopyTxt Success!" + "\n" + "Path: ======> " + Application.persistentDataPath + fileName); } } else { log("Error : ======> " + w.error); } } /// <summary> /// 向我的控制台打印内容 /// </summary> /// <param name="content">内容</param> void log(string content) { myConsole.text += "\n" + content; }}
本文永久链接:http://blog.csdn.net/Aries_H/article/details/61201880
工程文件下载链接:http://download.csdn.net/detail/aries_h/9777143
只是个人经验分享,如有不对的地方,还望大家指正谢谢。
如果有帮助到您,请关注一下公众号,谢谢。
- Untiy之Android平台读写遇到的坑
- Untiy之Android平台读写遇到的坑
- Android Studio mac 虚拟机无法读写遇到的坑
- android 平台搭建中遇到的问题
- untiy 对象之SetActive
- Untiy - android打包的assetbundle加载时材质问题
- Windows 平台之读写锁
- 在SAE开放云平台进行IO读写相关操作时遇到的问题
- Untiy
- 华为OJ平台遇到的坑
- Android读写操作之sdcard的读写操作
- untiy 脚本API之RectTransformUtility
- Unity 打包至Android 平台 编译不通过 所遇到过的坑
- ofbiz部署之平台部署安装及遇到的问题
- Android开发平台搭建遇到的ADT无法安装问题
- jsdk+Android开发平台搭建所遇到莫名的错误
- 读写INI遇到的问题
- 读写分离遇到的问题
- 【密码学】维吉尼亚密码加解密原理及其破解算法Java实现
- 欢迎使用CSDN-markdown编辑器
- 逻辑回归
- mysql5.7安装时2503 2502错误以及自定义安装目录
- $.ajax()方法详解
- Untiy之Android平台读写遇到的坑
- 程序出现数值每次从redis获取不一样
- 腾讯云系统镜像和使用镜像创建云主机
- Windows 7下Git SSH 创建Key的步骤(经个人实践win10环境下也可行)
- 大型高并发高负载web应用系统架构-数据库架构策略
- 初识LinkedHashMap源码
- 又搜集一批项目源码,同样改改就能用
- 如何将java web项目上线/部署到公网
- 实体类(VO,DO,DTO,PO)的划分