@Satellite-109
2018-07-21T07:25:00.000000Z
字数 4252
阅读 2150
Unity Android Xml
前文转自:Unity3D各平台路径(包括手机内置存储路径、SD卡等等)
1、Resources 路径
Resources文件夹是Unity里自动识别的一种文件夹,可在Unity编辑器的Project窗口里创建,并将资源放置在里面。Resources文件夹下的资源不管是否有用,全部会打包进.apk或者.ipa,并且打包时会将里面的资源压缩处理。加载方法是Resources.Load(文件名),需要注意:文件名不包括扩展名,打包后不能更改Resources下的资源内容,但是从Resources文件夹中加载出来的资源可以更改。
2、Application.dataPath 路径
这个属性返回的是程序的数据文件所在文件夹的路径,例如在Editor中就是项目的Assets文件夹的路径,通过这个路径可以访问项目中任何文件夹中的资源,但是在移动端它是完全没用。
3、Application.streamingAssetsPath 路径
这个属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。在Unity工程的Assets目录下起一个名为“StreamingAssets”的文件夹即可,然后用Application.streamingAssetsPath访问,这个文件夹中的资源在打包时会原封不动的打包进去,不会压缩,一般放置一些资源数据。在PC/MAC中可实现对文件的“增删改查”等操作,但在移动端是一个只读路径。
4、Application.persistentDataPath 路径(推荐使用)
此属性返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。这个路径可读、可写,但是只能在程序运行时才能读写操作,不能提前将数据放入这个路径。在IOS上是应用程序的沙盒,可以被iCloud自动备份,可以通过同步推送一类的助手直接取出文件;在Android上的位置是根据Project Setting里设置的Write Access路径,可以设置是程序沙盒还是sdcard,注意:如果在Android设置保存在沙盒中,那么就必须root以后才能用电脑取出文件,因此建议写入sdcard里。一般情况下,建议将获得的文件保存在这个路径下,例如可以从StreamingAsset中读取的二进制文件或者从AssetBundle读取的文件写入PersistentDatapath。
5、Application.temporaryCachePath 路径
此属性返回一个临时数据的缓存目录,跟Application.persistentDataPath类似,但是在IOS上不能被自动备份。
6、 /sdcard/.. 路径
表示Android手机的SD卡根目录。
7、 /storage/emulated/0/.. 路径(这个路径我查找了好久……)
表示Android手机的内置存储根目录。
以上各路径中的资源加载方式都可以用WWW类加载,但要注意各个平台路径需要加的访问名称,例如Android平台的路径前要加"jar:file://",其他平台使用"file://"。以下是各路径在各平台中的具体位置信息:
| Android平台 | |
|---|---|
| Application.dataPath | /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/xxx-xx/xxx.app/Data |
| Application.streamingAssetsPath | Application/xxxxx-xxxx/xxx.app/Data/Raw |
| Application.persistentDataPath | Application/x-xxxx-xxx/Documents |
| Application.temporaryCachePath | Application/xxxxxx-xxx/Library/Caches |
| Windows Web Player | |
| Application.dataPath | file:///D:/MyGame/WebPlayer |
| (即导包后保存的文件夹,html文件所在文件夹) | |
| Application.streamingAssetsPath | |
| Application.persistentDataPath | |
| Application.temporaryCachePath | |
下面是Android下的demo
/// <summary>/// 游戏Xml存档/// </summary>private void SaveInfoByXml(){GameInfo info = GetGameState();XmlDocument xmlDoc = new XmlDocument();string filePath;#if UNITY_ANDROIDfilePath = Application.persistentDataPath + "/" + "infoFile.xml";#endif#if UNITY_EDITORfilePath = Application.dataPath + "/StreamingAssets/infoFile.xml";#endifXmlElement root = xmlDoc.CreateElement("Info");XmlElement target;XmlElement modelIndex;XmlElement materialIndex;for(int i = 0; i < info.activeIndex.Count; i++){target = xmlDoc.CreateElement("Target");target.SetAttribute("index", info.activeIndex[i].ToString());modelIndex = xmlDoc.CreateElement("Model");modelIndex.InnerText = info.activeModel[i].ToString();materialIndex = xmlDoc.CreateElement("Material");materialIndex.InnerText = info.activeMaterialIndex[i].ToString();target.AppendChild(modelIndex);target.AppendChild(materialIndex);root.AppendChild(target);}target = xmlDoc.CreateElement("Score");target.SetAttribute("score", info.score.ToString());root.AppendChild(target);xmlDoc.AppendChild(root);xmlDoc.Save(filePath);}/// <summary>/// 游戏Xml读档/// </summary>/// <returns></returns>private GameInfo LoadInfoByXml(){GameInfo info = new GameInfo();string filePath;#if UNITY_ANDROIDfilePath = Application.persistentDataPath + "/" + "infoFile.xml";#endif#if UNITY_EDITORfilePath = Application.dataPath + "/StreamingAssets/infoFile.xml";#endifif (File.Exists(filePath)){XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(filePath);XmlNodeList targets = xmlDoc.SelectSingleNode("Info").ChildNodes;foreach(XmlElement target in targets){if(target.Name == "Target"){info.activeIndex.Add(int.Parse(target.GetAttribute("index")));XmlNode model = target.SelectSingleNode("Model");info.activeModel.Add(int.Parse(model.InnerText));XmlNode material = target.SelectSingleNode("Material");info.activeMaterialIndex.Add(int.Parse(material.InnerText));}else if(target.Name == "Score"){info.score = int.Parse(target.GetAttribute("score"));}}return info;}else{return null;}}
因为我测试的时候打包的Android上的,所以用的路径是 Application.persistentDataPath
这里用的是XML文档,也可以用JSON或者直接二进制文档,对于单机小游戏的话,没什么存储的数据可以使用内置的 PlayerPrefs 进行简单数据存储,这个类可以在UnityAPI中直接找到。
1. 如果用最新的版本SDK的话,可能检测不到tool工具什么的,可以下载旧版本把tools下面的文件做个替换
2. 平台切换到Android的时候,#elif 可能出现无法正常判断的情况,这时应该把非编辑器添加进去,例如:#elif UNITY_ANDROID && !UNITY_EDITOR
3. 实际开发中不要像我那个例子那样写,把平台什么的归结到一个文件下,维护和测试都好做点。