一、Unity 加载路径
Unity中我们使用到的资源类型主要有:Resources、StreamingAssets、AssetBundle和PersistentDataPath,资源处理的时候它们的区别如下:
Resources:
是作为一个Unity3D的保留文件夹出现的,也就是如果你新建的文件夹的名字叫Resources,那么里面的内容在打包时都会被无条件的打到发布包中。它的特点简单总结一下就是:
- 只读,即不能动态修改。所以想要动态更新的资源不要放在这里。
- 会将文件夹内的资源打包集成到.asset文件里面。因此建议可以放一些Prefab,因为Prefab在打包时会自动过滤掉不需要的资源,有利于减小资源包的大小。
- 主线程加载。
- 资源读取使用Resources.Load()。
StreamingAssets:
StreamingAssets,其实和Resources还是蛮像的。同样作为一个只读的Unity3D的保留文件夹出现。不过两者也有很大的区别,那就是Resources文件夹中的内容在打包时会被压缩和加密。而StreamingAssets文件夹中的内容则会原封不动的打入包中,因此StreamingAssets主要用来存放一些二进制文件。下面也同样做一个简单的总结:
- 同样,只读不可写。
- 主要用来存放二进制文件。
- 只能用过WWW类来读取。
AssetBundle:
AssetBundle的介绍已经有很多了。简而言之就是把prefab或者二进制文件封装成AssetBundle文件(也是一种二进制)。但是也有硬伤,就是在移动端无法更新脚本。下面简单的总结下:
- 是Unity3D定义的一种二进制类型。
- 最好将prefab封装成AssetBundle,不过上面不是才说了在移动端无法更新脚本吗?那从AssetBundle中拿到的Prefab上挂的脚本是不是就无法运行了?也不一定,只要这个prefab上挂的是本地脚本,就可以。
- 使用WWW类来下载。
PersistentDataPath:
看上去它只是个路径呀,可为什么要把它从路径里面单独拿出来介绍呢?因为它的确蛮特殊的,这个路径下是可读写。而且在IOS上就是应用程序的沙盒,但是在Android可以是程序的沙盒,也可以是sdcard。并且在Android打包的时候,ProjectSetting页面有一个选项Write Access,可以设置它的路径是沙盒还是sdcard。下面同样简单的总结一下:
- 内容可读写,不过只能运行时才能写入或者读取。 提前将数据存入这个路径是不可行的。
- 无内容限制。你可以从StreamingAssets中读取二进制文件或者从AssetBundle读取文件来写入 PersistentDataPath
- 写下的文件,可以在电脑上查看。同样也可以清掉。
1.Android平台:
路径属性 | 路径 |
Application.dataPath | /data/app/xxx.xxx.xxx.apk |
Application.streamingAssetsPath | jar:file:///data/app/xxx.xxx.xxx.apk/!/assets 或"jar:file://" + Application.dataPath + "!/assets/" |
Application.persistentDataPath | /data/data/xxx.xxx.xxx/files |
Application.temporaryCachePath | /data/data/xxx.xxx.xxx/cache |
2.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 |
二、StreamingAssets路径加载Xml
1.搭建场景
我这里使用的Unity版本为5.6.3f1,一个C#脚本和XML文件,项目地址: 点击打开链接
2.编辑XML
<?xml version="1.0" encoding="utf-8" ?>
<Xml>
<JD>
<Name>LuoYang</Name>
<Title>洛阳</Title>
<Manager>XX</Manager>
<Phone>0379-64361091</Phone>
<X>85</X>
<Y>-304</Y>
</JD>
<JD>
<Name>XinYang</Name>
<Title>信阳</Title>
<Manager>XXX</Manager>
<Phone>0376-6766856</Phone>
<X>003</X>
<Y>004</Y>
</JD>
</Xml>
3.XML解析切记必须是utf-8的格式。该xml文件就用来后续我们用来测试资源加载,把xml文件当成资源,就不再实例出怪物之类的物体了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using UnityEngine.UI;
public class ReadXMLInfo : MonoBehaviour
{
public Text _title;
public Text _manager;
public Text _phone;
void Start()
{
ReadXMLAllInfo();
}
private void ReadXMLAllInfo()
{
string filePath =
#if UNITY_EDITOR
"file://" + Application.streamingAssetsPath + "/ReadProvince.xml";
#elif UNITY_IPHONE
Application.dataPath +"/Raw"+"/ReadProvince.xml";
#elif UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/" + "ReadProvince.xml";
#endif
StartCoroutine(Load(filePath));
}
IEnumerator Load(string filePath)
{
WWW www = new WWW(filePath);
yield return www;
//Debug.Log(www.text);
if (www.error != null)
{
print("请求失败");
}
else
{
print("请求成功");
//创建
XmlDocument XmlDoc = new XmlDocument();
System.IO.StringReader stringReader = new System.IO.StringReader(www.text);
stringReader.Read();//跳过BOM
string result = stringReader.ReadToEnd();
//关闭
stringReader.Close();
//加载文本
XmlDoc.LoadXml(result);
//获取节点个数
int XmlCount = XmlDoc.GetElementsByTagName("JD").Count;
for (int i = 0; i < XmlCount; i++)
{
if (XmlDoc.GetElementsByTagName("Name")[i].InnerText == "LuoYang")
{
_title.text = XmlDoc.GetElementsByTagName("Title")[i].InnerText;
_manager.text = XmlDoc.GetElementsByTagName("Manager")[i].InnerText;
_phone.text = XmlDoc.GetElementsByTagName("Phone")[i].InnerText;
}
}
}
StopCoroutine(Load(filePath));
}
}
3.1错误分析:
3.1.2“XmlException: Text node cannot appear in this state. Line 1, position 1.”出现这个错误的原因是因为Unity3D加载XML文件的时候。如图:
解决办法就是把XML文件必须保存为UTF-8编码的格式,同时还必须去掉开头的两个字节(BOM)用来标识UTF-8用的。如何去掉bom标示就用下方图所示中的方框里面,Xml错误原因链接: