UnityAPI---Application类(数据文件路径)

数据文件路径:总共有四个属性,分别为:dataPath、persistentDataPath、streamingAssetsPath、 temporaryCachePath
System.Environment.CurrentDirectory   获取到本地工程的绝对路径
Application.dataPath  Assets资源文件夹的绝对路径
Application.persistentDataPath  持久性的数据存储路径,在不同平台路径不同,但都存在,绝对路径
Application.streamingAssetsPath  Assets资源文件夹下StreamingAssets文件夹目录的绝对路径
Application.temporaryCachePath  游戏运行时的缓存目录,也是绝对路径
基本语法:public static string dataPath { get ;}
dataPaht是包含游戏数据文件夹的路径,权限为只读,返回的是一个相对路径,即对于不同的游戏平台返回的路径是不一样的。
Unity Editor: /Assets
Mac player: /Contents
iOS player: //Data
Android:/data/app/xxx.xxx.xxx.apk
注意:
Application.dataPath 返回的路径目录,在移动端是没有访问权限的(既不能访问此目录)。
基本语法:public static string persistentDataPath{ get ;}
persistentDataPaht返回的是一个持久化数据存储目录,权限为只读,在同一平台,不用的应用程序访问此属性返回值相同,但是不同的平台就不相同了。当应用程序发布到IOS和Android平台中,这个路径会指向一个公共的路径,而且应用每次更新时这里的数据不会被清除。
注意:
这个路径比较特殊,这个路径下是可读写。而且在IOS上就是应用程序的沙盒,但是在Android可以是程序的沙盒,也可以是sdcard。并且在Android打包的时候,ProjectSetting页面有一个选项Write Access,可以设置它的路径是沙盒还是sdcard。
该路径的特点:
  • 内容可读写,不过只能运行时才能写入或者读取。提前将数据存入这个路径是不可行的。
  • 无内容限制。你可以从StreamingAsset中读取二进制文件或者从AssetBundle读取文件来写入PersistentDataPath中。
  • 写下的文件,可以在电脑上查看。同样也可以清掉。
基本语法:public static string streamingAssetsPath{ get ;}
streamingAssetsPath返回的是流数据的缓存目录,返回路径为相对路径适合用来存储一些外部数据文件。
基本语法:public static string temporaryCachePath{ get ;}
temporaryCachePath返回一个临时数据缓存目录(只读),同一平台不用应用程序访问此属性的返回值相同,不同平台返回值不同。
将以上四个路径分别在Unity Editor和Android、IOS 平台下返回路劲的基本情况:
Android 平台
Application.dataPaht /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath /data/data/xxx.xxx.xxx/files
Application.temporaryCachePath /data/data/xxx.xxx.xxx/cache
IOS 平台
Application.dataPath:
Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath:
Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath:
Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath:
Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches
Unity Editor平台
Application.dataPaht Application(工程目录)/Assets
Application.streamingAssetsPath Application(工程目录)/Assets/StreamingAssets
Application.persistentDataPath 系统指定目录1/工程名
Application.temporaryCachePath 系统指定目录2/工程名
从中可以看出,dataPath 和 stremingAssetsPath是相对于程序的安装目录,是相对路径。非常适用于移植平台设置外部数据文件读取路径。而persistentDataPath和temporaryCachePath返回的程序所在平台的固定位置。适用于存储程序运行过程中一些数据。
实际用法:
 如果只是读取和创建文件或者文件夹,直接用上面的路径拼自己的文件夹就可以;如果是要访问apk或者app内的文件这里操作就有点特殊了,如下(这里以streamingAssetsPath为例):
安卓读:
正常读
filePath = Application.streamingAssetsPath + "/新建文件.txt";
读apk包内文件
filePath = "jar:file://" + Application.dataPath + "/!/assets" + "/新建文件.txt";
IOS读: 
正常读
filePath = "file://" +  Application.streamingAssetsPath + "/新建文件.txt";
读ipa包内文件
filePath = "file://" +  Application.dataPath + "/Raw" + "/新建文件.txt";
PC读:
filePath = "file://" + Application.streamingAssetsPath + "/新建文件.txt";
扩展知识:资源加载---Unity移动平台获取外部文件解析
关于资源加载问题要讲到Resources、StreamingAssets、AssetBundle这三个类。
Resources:
它作为一个Unity3D的保留文件夹出现的,也就是如果你新建的文件夹的名字叫Resources,那么里面的内容在打包时都会被无条件的打到发布包中。
其特点是:
  • 只读,即不能动态修改。所以想要动态更新的资源不要放在这里。
  • 会将文件夹内的资源打包集成到.asset文件里面。因此建议可以放一些Prefab,因为Prefab在打包时会自动过滤掉不需要的资源,有利于减小资源包的大小。
  • 主线程加载。
  • 资源读取使用Resources.Load()。
StreamingAssets:
要说到StreamingAssets,其实和Resources还是蛮像的。同样作为一个只读的Unity3D的保留文件夹出现。不过两者也有很大的区别,那就是Resources文件夹中的内容在打包时会被压缩和加密。而StreamingAsset文件夹中的内容则会原封不动的打入包中,因此StreamingAssets主要用来存放一些二进制文件。
下面也同样做一个简单的总结:
  • 只读不可写。
  • 主要用来存放二进制文件。
  • Android平台下一般是通过过WWW类来读取。
AssetBundle:
是把prefab或者二进制文件封装成AssetBundle文件(也是一种二进制)。但是也有硬伤,就是在移动端无法更新脚本。
下面简单的总结下:
  • 是Unity3D定义的一种二进制类型。
  • 最好将prefab封装成AseetBundle,只要这个prefab上挂的是本地脚本,就可以。
  • 使用WWW类来下载。
案例:不同平台Resources、StreamingAssets、AssetBundle应用。
先新建一个文本,写入以下内容,然后把后缀改为.xml,已提供加载使用。
新建的测试test.xml文件:
    GarFey
    http://blog.sina.com.cn/u/3286376053
    26
   

China

Resources:
1.在Assets文件夹下新建一个Resources目录,将test.xml放在该目录下,
2.新建一个脚本ReadXmlInResources.cs挂在 Main Camera上,脚本内容:
 
//-----------------------
// @Author GarFe
// @date 20190612
// @version 1.0
//-----------------------
using UnityEngine;
using System.Collections;
using System.Xml;

///
/// 此类用于测试 在Resources目录下读取XML文件
///
public class ReadXmlInResources : MonoBehaviour
{
    private string readXmlResult;

    // Use this for initialization
    void Start () {
        LoadXML("test");
        Debug.Log("xml:" + readXmlResult);
    
    }

    void OnGUI()
    {
        GUIStyle titleStyle = new GUIStyle();
        titleStyle.fontSize = 20;
        titleStyle.normal.textColor = new Color(0, 255, 255);
        GUI.Label(new Rect(400, 10, 500, 200), readXmlResult, titleStyle );
    }

    ///
    /// 加载Assets/Resources文件夹下resourcesName文件
    ///
    ///
    private void LoadXML(string resourcesName)
    {
        readXmlResult = Resources.Load(resourcesName).ToString();
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(readXmlResult);
    }
}

StreamingAssets:
1.在Assets目录下新建StreamingAssets目录,将test.xml放到该目录下。
2.创建脚本ReadXmlInStreamingAssets.cs 挂在Main Camera上,脚本内容:
 
//-----------------------
// @Author GarFe
// @date 20190612
// @version 1.0
//-----------------------
using UnityEngine;
using System.Collections;
using System.IO;

public class ReadXmlInStreamingAssets: MonoBehaviour
{
    ///
    /// 读取XML结果
    ///
    private string readXmlResult;

    // Use this for initialization
    void Start ()
    {
        loadXML();
        Debug.Log(readXmlResult);
    }
    
    void OnGUI()
    {
        GUIStyle titleStyle = new GUIStyle();
        titleStyle.fontSize = 20;
        titleStyle.normal.textColor = new Color(0, 255, 255);
        GUI.Label(new Rect(400, 10, 500, 200), readXmlResult, titleStyle);
    }

    ///
    /// 根据不同的平台加载StremingAssets文件夹下的XML文件
    ///
    private void loadXML()
    {
        if(Application.platform == RuntimePlatform.Android)
        {
            Debug.Log("android platform");
            StartCoroutine("loadXMLWithWWW");
        }
        else if(Application.platform == RuntimePlatform.WindowsEditor)
        {
            Debug.Log("window platform");
            //获得文件路径
            string path = Application.streamingAssetsPath + "/text.xml";
            loadXMLWithStreamReader(path);
        }
        else if(Application.platform == RuntimePlatform.IPhonePlayer)
        {
            Debug.Log("iphone platform");
            //获得文件路径
            string path = Application.streamingAssetsPath +"/text.xml";
            loadXMLWithStreamReader(path);

        }
        //发现所有平台StreamingAssets目录下的文件路径都可以用Application.streamingAssetsPath +"/fileName.xml"来加载,但Android加载方式不一样
    }

    ///
    /// 使用StreamReader读取文件信息
    ///
    /// 读取文件的路径
    private void loadXMLWithStreamReader(string path)
    {  
        FileInfo fileInfo = new FileInfo(path);
        StreamReader reader = fileInfo.OpenText();
        readXmlResult = reader.ReadToEnd();
        reader.Close();
    }

    ///
    /// 
    ///对于android平台, streamingAssets中的文件一般只能用www来异步读取
    /// (同时,可以用persistenDataPath来保存streamingassets里读取的数据)
    ///
    ///
    IEnumerator loadXMLWithWWW()
    {
        //获得文件路径
        string path = Application.streamingAssetsPath + "/text.xml";
        WWW www = new WWW(path);
        yield return www;
        readXmlResult = www.text;
    }
}

猜你喜欢

转载自blog.csdn.net/u013774978/article/details/129847731