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

标签Unity Android Xml

  • Unity各平台上读写文件-Android例子
      • 原理
        • 1、Resources 路径
        • 2、Application.dataPath 路径
        • 3、Application.streamingAssetsPath 路径
        • 4、Application.persistentDataPath 路径(推荐使用)
        • 5、Application.temporaryCachePath 路径
        • 6、 /sdcard/.. 路径
        • 7、 /storage/emulated/0/.. 路径(这个路径我查找了好久……)
      • 路径对照表
      • AndroidDemo
      • 后话

原理

前文转自: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

AndroidDemo

    ///     /// 游戏Xml存档    ///     private void SaveInfoByXml()    {        GameInfo info = GetGameState();        XmlDocument xmlDoc = new XmlDocument();        string filePath;#if UNITY_ANDROID        filePath = Application.persistentDataPath + "/" + "infoFile.xml";#endif#if UNITY_EDITOR        filePath = Application.dataPath + "/StreamingAssets/infoFile.xml";#endif        XmlElement 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);    }    ///     /// 游戏Xml读档    ///     ///     private GameInfo LoadInfoByXml()    {        GameInfo info = new GameInfo();        string filePath;#if UNITY_ANDROID        filePath = Application.persistentDataPath + "/" + "infoFile.xml";#endif#if UNITY_EDITOR        filePath = Application.dataPath + "/StreamingAssets/infoFile.xml";#endif        if (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. 实际开发中不要像我那个例子那样写,把平台什么的归结到一个文件下,维护和测试都好做点。


更好的阅读模式可以点击作业部落

更多相关文章

  1. Android Studio:正确引入so文件的方法
  2. SAX解析XML文件
  3. 文件的读写操作
  4. 使用Android Studio开发百度地图LBS过程中遇到找不到so文件
  5. android6.0创建文件问题
  6. Android studio 不能预览布局文件
  7. Android Studio开发Android(一)——build.gradle文件详解
  8. 读取android根目录下的文件或文件夹
  9. Android 功耗优化(5)---Android O 的Doze模式白名单路径

随机推荐

  1. Google Android介绍..
  2. android的intent使用方法
  3. Android核心分析---Android电话系统
  4. 如何在 iOS、Android、macOS、Windows 之
  5. Android View学习示例
  6. Hello, Android(安卓)深入(二)
  7. Android设计登陆界面
  8. 【Android(安卓)开发教程】解决Intent-Fi
  9. [置顶] Android学习之--prelink
  10. android获取程序安装时间