Untiy之Android平台读写遇到的坑

来源:互联网 发布:mac没有host 编辑:程序博客网 时间:2024/05/21 10:28

关注公众号,获取更多干货。

二维码


最近在做一个在Android平台频繁读写的一个项目,加载音乐的时候需要用WWW加载返回的w.audioClip,而读写xml的时候则需要用IO流读写。

于是我发现。。。Android平台路径的坑可真是不小啊

首先在Unity平台先看一下Windows系统下,各个文件夹的路径:

WindowsPath

这里我把Unity里能打印的四个自带里文件夹路径都打印出来了,而其实,我常用的只有streamingAssetsPath和persistentDataPath

streamingAssetsPath是可以跟随Unity打包存在的文件夹,WWW和IO都可以访问这个文件夹下的文件,但是这个文件夹是只读文件夹,不支持修改文件和写入等操作。

persistentDataPath是可读写的文件夹,但是这个文件夹是在APK安装成功后创建的,所以无法将文件在打包前放入此文件夹。

那么接下来我们看看Android平台的这些文件夹路径都是什么样的。

AndroidPath

下面开始分享我的经验

一、 如果用WWW去加载文件:

  1. 在Windows端访问streamingAssetsPath文件夹则需要在前面加上”file:///”协议,而Android端则不用加file协议,因为Android端的streamingAssetsPath路径Unity会自动为他添加jar:file://。

  2. 同样,如果是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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288

本文永久链接:http://blog.csdn.net/Aries_H/article/details/61201880

工程文件下载链接:http://download.csdn.net/detail/aries_h/9777143

只是个人经验分享,如有不对的地方,还望大家指正谢谢。 
如果有帮助到您,请关注一下公众号,谢谢。

1 0
原创粉丝点击