[关闭]
@Satellite-109 2018-07-21T07:25:00.000000Z 字数 4252 阅读 1886

Unity各平台上读写文件-Android例子

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

  1. /// <summary>
  2. /// 游戏Xml存档
  3. /// </summary>
  4. private void SaveInfoByXml()
  5. {
  6. GameInfo info = GetGameState();
  7. XmlDocument xmlDoc = new XmlDocument();
  8. string filePath;
  9. #if UNITY_ANDROID
  10. filePath = Application.persistentDataPath + "/" + "infoFile.xml";
  11. #endif
  12. #if UNITY_EDITOR
  13. filePath = Application.dataPath + "/StreamingAssets/infoFile.xml";
  14. #endif
  15. XmlElement root = xmlDoc.CreateElement("Info");
  16. XmlElement target;
  17. XmlElement modelIndex;
  18. XmlElement materialIndex;
  19. for(int i = 0; i < info.activeIndex.Count; i++)
  20. {
  21. target = xmlDoc.CreateElement("Target");
  22. target.SetAttribute("index", info.activeIndex[i].ToString());
  23. modelIndex = xmlDoc.CreateElement("Model");
  24. modelIndex.InnerText = info.activeModel[i].ToString();
  25. materialIndex = xmlDoc.CreateElement("Material");
  26. materialIndex.InnerText = info.activeMaterialIndex[i].ToString();
  27. target.AppendChild(modelIndex);
  28. target.AppendChild(materialIndex);
  29. root.AppendChild(target);
  30. }
  31. target = xmlDoc.CreateElement("Score");
  32. target.SetAttribute("score", info.score.ToString());
  33. root.AppendChild(target);
  34. xmlDoc.AppendChild(root);
  35. xmlDoc.Save(filePath);
  36. }
  37. /// <summary>
  38. /// 游戏Xml读档
  39. /// </summary>
  40. /// <returns></returns>
  41. private GameInfo LoadInfoByXml()
  42. {
  43. GameInfo info = new GameInfo();
  44. string filePath;
  45. #if UNITY_ANDROID
  46. filePath = Application.persistentDataPath + "/" + "infoFile.xml";
  47. #endif
  48. #if UNITY_EDITOR
  49. filePath = Application.dataPath + "/StreamingAssets/infoFile.xml";
  50. #endif
  51. if (File.Exists(filePath))
  52. {
  53. XmlDocument xmlDoc = new XmlDocument();
  54. xmlDoc.Load(filePath);
  55. XmlNodeList targets = xmlDoc.SelectSingleNode("Info").ChildNodes;
  56. foreach(XmlElement target in targets)
  57. {
  58. if(target.Name == "Target")
  59. {
  60. info.activeIndex.Add(int.Parse(target.GetAttribute("index")));
  61. XmlNode model = target.SelectSingleNode("Model");
  62. info.activeModel.Add(int.Parse(model.InnerText));
  63. XmlNode material = target.SelectSingleNode("Material");
  64. info.activeMaterialIndex.Add(int.Parse(material.InnerText));
  65. }
  66. else if(target.Name == "Score")
  67. {
  68. info.score = int.Parse(target.GetAttribute("score"));
  69. }
  70. }
  71. return info;
  72. }
  73. else
  74. {
  75. return null;
  76. }
  77. }

因为我测试的时候打包的Android上的,所以用的路径是 Application.persistentDataPath
这里用的是XML文档,也可以用JSON或者直接二进制文档,对于单机小游戏的话,没什么存储的数据可以使用内置的 PlayerPrefs 进行简单数据存储,这个类可以在UnityAPI中直接找到。
1. 如果用最新的版本SDK的话,可能检测不到tool工具什么的,可以下载旧版本把tools下面的文件做个替换
2. 平台切换到Android的时候,#elif 可能出现无法正常判断的情况,这时应该把非编辑器添加进去,例如:#elif UNITY_ANDROID && !UNITY_EDITOR
3. 实际开发中不要像我那个例子那样写,把平台什么的归结到一个文件下,维护和测试都好做点。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注