unity中 在移动平台各种读写文件夹存路径整理 如 StreamingAssets 等文件夹 各个路径在各种平台的文件路径

1.Resources 文件夹 少用

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

 2:StreamingAssets 在移动端也是只可读的不能写入数据    主要用来存放二进制文件。

 //安卓下这两个文件夹路径相同 
    //Application.streamingAssetsPath = jar:file:///data/app/com.xxx.xxx-1.apk!/assets/  == "jar:file://"+Application.dataPath+"!assets/"
    //Application.dataPath+"!assets/" = /data/app/com.xxx.xxx-1.apk!assets/

    //Ios下
    //Application.dataPath + "/Raw/" == @"file:///" + Application.streamingAssetsPath + "/"

    //Editor WIN 
    //@"file:///" + Application.streamingAssetsPath + "/"
    //"file:///" + Application.dataPath + "/StreamingAssets" + "/"

    private string path = string.Empty;
    public string GetSAPath()
    {
        //安卓平台 加文件名
#if UNITY_ANDROID && !UNITY_EDITOR
        path =  Application.streamingAssetsPath + "/"
#elif UNITY_IPHONE && !UNITY_EDITOR
        path = @"file:///" + Application.streamingAssetsPath + "/";
#elif UNITY_STANDLONE_WIN||UNITY_EDITOR
       path =  @"file:///" + Application.streamingAssetsPath + "/";
#endif
        return path;
    }

  3:Application.persistentDataPath 这个目录可读可写  一般存本地关卡等

 用于存档 直接使用 打包之前是没有这个目录的,直到应用程序在手机上安装完毕才有这个目录。
 该文件存在手机沙盒中,因为不能直接存放文件,
     1.通过服务器直接下载保存到该位置,也可以通过Md5码比对下载更新新的资源
     2.没有服务器的,只有间接通过文件流的方式从本地读取并写入Application.persistentDataPath文件下,然后再通过                  Application.persistentDataPath来读取操作。
 注:在Pc/Mac电脑 以及Android跟Ipad、ipone都可对文件进行任意操作,另外在IOS上该目录下的东西可以被iCloud自动备份。        Application.persistentDataPath + "/tempDic", "testXml"                                                    

对应存储路径 

Windows应用商店应用程序:application.persistentdatapath指向%userprofile%\appdata\local\packages\<productName>\localstate。
ios:application.persistentdatapath指向/var/mobile/containers/data/application/<guid>/documents。
 android:application.persistentdatapath指向大多数设备上的/storage/emulated/0/android/data/<packagename>/文件(有些旧手机可能指向SD卡上的位置,如果存在),该路径使用android.content.context.getexternalfilesdir解析


4:Application.temporaryCachePath来操作文件 同上但是     此属性用于返回一个临时数据的缓存目录(不会备份并且清空缓存会清掉)

 untiy 学习讨论群 184386599

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity,可以使用`System.IO`命名空间下的`File`类来操作文件,包括在Android环境下加载StreamingAssets文件夹的txt文件。但需要注意的是,直接使用`File`类在Android平台上加载StreamingAssets文件夹文件可能导致路径问题。 在Android平台上,StreamingAssets文件夹文件打包到APK,并且无法直接使用`File`类的路径来访问。为了解决这个问题,你可以使用Unity的`Application.streamingAssetsPath`属性来获取StreamingAssets文件夹路径,并结合`UnityWebRequest`类来加载文件。下面是一个示例代码: ```csharp using UnityEngine; using UnityEngine.Networking; using System.IO; public class LoadTxtFile : MonoBehaviour { public string fileName; // txt文件名 IEnumerator Start() { // 构建txt文件的完整路径 string filePath = Path.Combine(Application.streamingAssetsPath, fileName); UnityWebRequest www = UnityWebRequest.Get(filePath); #if UNITY_ANDROID && !UNITY_EDITOR // 在Android平台上使用UnityWebRequest来加载文件 www.url = filePath; #endif yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success) { // 获取txt文件内容 string txtContent = www.downloadHandler.text; Debug.Log(txtContent); } else { Debug.Log("Failed to load txt file: " + www.error); } } } ``` 上述代码,我们使用了`Path.Combine`方法来构建txt文件的完整路径,确保路径的正确性。然后,我们使用`UnityWebRequest`类来加载文件内容,并通过`downloadHandler.text`属性获取文件内容。 这样,你就可以在Android环境下加载StreamingAssets文件夹的txt文件了。请注意,上述代码在Android平台使用`UnityWebRequest`来加载文件,在其他平台使用`File`类来加载文件。这样可以保证在不同平台下的文件加载逻辑的一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值