zoukankan      html  css  js  c++  java
  • [Unity基础]移动平台下的文件读写

    From:http://blog.csdn.net/lyh916/article/details/52161633

    参考链接:

    http://www.cnblogs.com/murongxiaopifu/p/4199541.html?utm_source=tuicool#autoid-3-2-0

    http://zhaolongchn.blog.163.com/blog/static/1906585042013624115926451/

    http://forum.china.Unity3D.com/thread-1516-1-1.html


    在移动平台中,一般读取资源会通过下面这三个路径:

    1.Resources

    2.Application.streamingAssetsPath

    3.Application.persistentDataPath(同时这个也是可写的)


    重点说下下面这两个路径:

    1.Application.streamingAssetsPath(只读)

    需要手动建一个StreamingAssets文件夹。在打包时,Resources文件夹下的东西会被压缩和加密。而StreamingAssets文件夹中的内容则会原封不动的打入包中。

    一般在Resources下放预制,StreamingAssets下放二进制文件(csv、bin、txt、xml、json、AB包等)

    不能通过File类来读取这个路径,只能通过WWW类。这是因为在Android中,StreamingAssets的东西会被包含在.jar包中(类似于zip压缩文件)。


    2.Application.persistentDataPath(可读可写)

    安卓只有这个文件夹可以用File类来读写

    测试

    [csharp] view plain copy
     在CODE上查看代码片派生到我的代码片
    1. using UnityEngine;  
    2. using System.Collections;  
    3. using System.IO;  
    4. using UnityEngine.UI;  
    5. using System.Text;  
    6.   
    7. public class Test : MonoBehaviour {  
    8.   
    9.     public Text text0;  
    10.     public Text text1;  
    11.     public Text text2;  
    12.     public Text text3;  
    13.     private string path;  
    14.     private string content;  
    15.   
    16.     void Start ()   
    17.     {  
    18.         //显示不同平台下的路径信息  
    19.         text0.text = Application.dataPath + " " + Application.streamingAssetsPath + " " + Application.persistentDataPath;  
    20.   
    21.         //读取StreamingAssets下的文件  
    22.         if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)  
    23.         {  
    24.             path = "file://" + Application.streamingAssetsPath + "/Data/AA.bin";  
    25.         }  
    26.         else if (Application.platform == RuntimePlatform.Android)  
    27.         {  
    28.             path = Application.streamingAssetsPath + "/Data/AA.bin";  
    29.         }     
    30.         StartCoroutine(Load(path, (s) => { content += Application.platform + " " + s + " "; }));  
    31.   
    32.         //读取Resources下的文件  
    33.         text2.text = Resources.Load<TextAsset>("CC").text;  
    34.   
    35.         //读取与写入Application.persistentDataPath下的文件  
    36.         path = Application.persistentDataPath + "/BB.txt";  
    37.         File.WriteAllText(path, "保佑这个也能读取成功啊~~hello??", Encoding.UTF8);  
    38.         text3.text = File.ReadAllText(path, Encoding.UTF8);  
    39.     }  
    40.   
    41.     void Update()  
    42.     {  
    43.         if (!string.IsNullOrEmpty(content)) text1.text = content;  
    44.     }  
    45.   
    46.     IEnumerator Load(string url, System.Action<string> action)  
    47.     {  
    48.         WWW www = new WWW(url);  
    49.         yield return www;  
    50.         //Debug.Log(www.text);  
    51.         action(www.text);  
    52.     }  
    53.   
    54. }  





    Ps:

    1.如果读取的中文为乱码,则打开txt文件,另存为,选择编码为UTF-8即可。

    2.对于Application.dataPath路径的东西(不包括StreamingAssets和Resources),除非被引用,否则不会被打包。所以不建议把数据文件放在这个路径。具体的自行打包exe就知道了。


  • 相关阅读:
    BZOJ2530 : [Poi2011]Party
    BZOJ3998 : [TJOI2015]弦论
    BZOJ3941 : [Usaco2015 Feb]Fencing the Herd
    BZOJ3939 : [Usaco2015 Feb]Cow Hopscotch
    搬家啦~
    BZOJ3837 : [Pa2013]Filary
    使用Privoxy做智能代理切换
    放弃iOS4,拥抱iOS5
    让Xcode的 stack trace信息可读
    改进iOS客户端的升级提醒功能
  • 原文地址:https://www.cnblogs.com/caymanlu/p/6361695.html
Copyright © 2011-2022 走看看