UNITY_资源路径与加载外部文件

Posted on 2019-03-11 22:43  FudgeBear  阅读(8013)  评论(1编辑  收藏  举报

UNITY_资源路径与加载外部文件

https://www.tuicool.com/articles/qMNnmm6
https://blog.csdn.net/appppppen/article/details/51396256
https://unity3d.com/cn/learn/tutorials/topics/best-practices/resources-folder
https://blog.csdn.net/qq_18995513/article/details/51958906

背景:

有许多静态数据是放在客户端中的,比如csv/xml文件,是需要动态读取文件的

实例: 动态读取一个xml文件,并生成一个类

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <name>chenjd</name>
    <blog>http://www.cnblogs.com/murongxiaopifu/</blog>
    <organization>Fanyoy</organization>
    <age>25</age>
</test>

将此xml文件随意放在某路径下:Assets/aa/bb/Test.xml

使用代码读取文件内容

void Start() {
    XElement result = LoadXML("Assets/aa/bb/Test.xml");
}

void LoadXML(string path) {
    XElement xml = XElement.Load(path);
    return xml;
}

读取成功。

问题1. 路径和地址。在移动端是找不到文件的。
问题2. 使用的是PC上传统的一套读取资源的做法,而没有使用unity3d提供的方法。
  可能导致找得到文件但是没能正确地读取文件内容

移动平台的资源路径:

Application.dataPath: 程序的数据文件所在文件夹。在Editor中就是Assets
  安卓: /data/app/xxx.xxx.xxx.apk
  iOS:  Application/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data

Application.streamingAssetsPath: 流数据的缓存目录,为相对路径,适合设置一些外部数据文件
  安卓: jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
  iOS:  Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw

Application.persistentDataPath: 持久化数据存储目录的路径,可用于存储一些持久化的数据文件
  安卓: /data/data/xxx.xxx.xxx/files
  iOS:  Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents

Application.temporaryCachePath: 临时数据的缓存目录
  安卓: /data/data/xxx.xxx.xxx/cache
  iOS:  Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches

dataPath和streamingAssetsPath一般是相对程序的安装目录位置
persistentDataPath和temporaryCachePath一般是与系统有关的固定位置

那么,打包之后的资源的路径如何与这些地址对应上呢?

Unity中的资源处理种类:

Resources:

如果有文件夹名为Resources,则里面的内容会无条件地在打包时打包集成到.asset文件中
可以放置一些Prefab,因为Prefab在打包时会自动过滤掉不需要的资源,有利于减小资源包
1. 只读。不能动态修改,会动态更新的资源不要放在这里
2. 主线程加载
3. 使用Resources.Load()加载资源

StreamingAssets:

与Resources类似
区别为:Resources文件夹中的内容在打包时会被压缩和加密,而StreamingAssets中的内容则原封不动地打包
一般用来存放一些二进制文件
1. 只读,不能动态修改
2. 主要存放二进制文件
3. 只能通过WWW类读取

AssetBundle:

将prefab或二进制文件封装成AssetBundle文件(也是二进制文件)
缺点: 在移动端无法更新脚本
总结:
1. 该二进制文件(AssetBundle文件) 是Unity3D定义的一种二进制类型
2. 最好将Prefab封装成AssetBundle,但是在移动端无法更新脚本
3. 使用WWW类读取

PersistentDataPath:

这是一个路径(可读写)
在iOS上就是应用程序的沙盒;
在安卓上可以是程序的沙盒或sdCard -- 在安卓打包时,ProjectSetting的选项WriteAccess可设置路径是沙盒还是sdcard
总结:
1. 内容在运行时可读写,提前将数据存入这个路径是不可行的
2. 无内容类型的限制
3. 写下的文件可以在电脑上查看,同样也可以在电脑中清除

移动平台读取外部文件的方法:

使用Unity3D规定的操作方式来读取外部资源:Resources/ StreamingAssets/ AssetBundle

Resources: 

新建Resources目录,在目录中创建文件Test.xml(之前在背景中提到的那个文件)
通过Resources的方法来读取Test.xml中的内容。

public class Test: MonoBehaviour {
    private string _result;
    void Start(){  LoadXML("Test");  }

    private void LoadXML(string path){
        _result = Resources.Load(path).ToString();
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(_result);
    }
}

StreamingAssets:

新建StreamingAssets文件夹并存放Test.xml文件
(StreamingAssets文件夹中的文件不会被压缩或加密,所以一般是要放二进制文件的,这里放xml只是做一个演示,实际操作中切记不要直接把数据文件放到该目录中)

public class Test : MonoBehaviour {
    private string _result;
    void Start(){  StartCoroutine(LoadXML());  }

    IEnumerator LoadXML(){
        string path = Application.streamingAssetsPath;
        WWW www = new WWW(path);
        yield return www;
        _result = www.text;
    }
}

AssetBundle:

比较麻烦,需要先把Test.xml打包成AssetBundle文件
创建好AssetBundle文件并命名为TestXML后,因为ab文件是二进制文件,因此放入StreamingAssets文件夹。

public class Test: MonoBehaviour {
    private string _result;
    void Start(){  LoadXML();  }

    void LoadXML(){
        AssetBundle assetBundleCsv = new AssetBundle();
        // 读取放入StreamingAssets文件夹中的ab文件
        string str = Application.streamingAssetsPath + "/TestXML.bundle";
        WWW www = new WWW(str);
        www = WWW.LoadFromCacheOrDownload(str, 0);
        assetBundleCsv = www.assetBundle;
        string path = "Test";
        TextAsset test = assetBundleCsv.Load(path, typeof(TextAsset)) as TextAsset;
        _result = test.ToString();
    }
}

PersistentDataPath:

只有在运行时才能读写,例如通过网络下载资源存放在PersistantDataPath中

与StreamingAssets的读取很类似,但要注意通过www类加载PersistentDataPath必须使用file://协议实现加载

public class Test: MonoBehaviour{
    private string _result;
    void Start(){  StartCoroutine(LoadXML());  }

    private void LoadXML(){
        string path = "file://" + Application.persistentDataPath + "/test.xml";
        WWW www = new WWW(path);
        yield return www;
        _result = www.text;
    }
}

深入Resources.Load()

Recommendation: DO NOT USE IT.

Reasons:

1. Use of the Resources folder makes fine-grained memory management more difficult;

2. Improper use of Resources folders will increase applicaiton startup time and the length of builds
  The increase of the number of Resources folders makes the management of the "Resources Assets" more difficult;

3. The Resources system degrades a project's ability to deliver custom content to specific platforms and eliminates the possibility of incremental content upgrades
  AssetBundle Variants are Unity's primary tool for adjusting content on a per-device basis

Proper uses:

Two specific use cases where Resources system can be helpful

1. The ease of the Resources folder makes it an excellent system to rapidly prototype.
  But when a project moves into full production, the use of Resources system should be eliminated.

2. When the content is:
  Generally required throughout a project's lifetime
  Not memory-intensive
  Not prone to patching, or does not vary across platforms/ devices
  Used for minimal bootstrapping

  比如: 持有预制体的MonoBehaviour单例、包含第三方配置数据的ScriptableObject容器等

Resources的卸载:

Resources资源类型的加载方式只有一种,但卸载有多种。

1. Resources.UnloadAsset(Object assetToUnload)
  从内存中卸载(非GameObject类型的资源???),会将内存中已加载的资源卸载掉
2. Destroy(obj) 
  仅用于卸载(GameObject类型???)的资源的克隆体
3. DestroyImmediately(obj)
  卸载GameObject类型的资源,会将内存中已加载资源及其克隆体卸载;
  但该方法只能用在非编辑模式下,否则会报错,提示改为DestroyImmediately(obj, true),
  然而编辑模式下使用该函数会连文件夹里的原始Asset一并删除。

官方推荐的卸载方法为:
  public static AsyncOperation UnloadUnusedAssets()
  异步检索资源如果没有被使用才会卸载。
  被全局变量引用的资源会导致一直无法释放。