Unity资源路径与读取

Unity资源路径有:

1、StreamingAssets:只读,一般用于存放应用程序运行时需要加载的资源文件,可以通过Application.streamingAssetsPath来获取。

2、PersistentDataPath:可读写,一般用于存放应用程序运行时生成或下载的文件,可以通过Application.persistentDataPath来获取。

3、DataPath:应用程序的安装目录,它包含了应用程序的可执行文件和默认资源文件,可以通过Application.dataPath来获取。

4、TemporaryCachePath:临时缓存目录,用于存放临时文件,可以通过Application.temporaryCachePath来获取。

不同平台中资源路径的实际路径是不一样的,可参考下表:

|---------|-------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|
| api: | Application.dataPath | Application.streamingAssetsPath | Application.persistentDataPath |
| Windows | 例:C:/Projects/YourProject/ Assets | 例: C:/Projects/YourProject/ Assets/StreamingAssets | 例: C:/Users/YourUsername/AppData/ LocalLow/CompanyName/ YourProjectName |
| iOS | 例: /var/containers/Bundle/ Application/AppId/ YourProjectName.app/ Data | 例: /var/containers/Bundle/ Application/AppId/ YourProjectName.app/ Data/Raw/ StreamingAssets | 例: /var/mobile/Containers/Data/ Application/AppId/Documents |
| Android | 例: /data/app/com. companyname. yourprojectname-1/base.apk | 例: /data/app/com. companyname. yourprojectname-1/base.apk!/assets | 例: /storage/emulated/0/Android/ data/com.companyname. yourprojectname/files |

上面的路径是相对路径,并不是具体的文件路径。具体的文件路径会根据应用程序安装位置而变化。所以要保证不出错,我们在读写资源的时候,最好的办法就是使用代码的API方法(即上表行一)创建资源路径和读取资源。

在Windows、iOS、Android和Mac等平台上,读取这些资源路径可以使用WWW类、UnityWebRequest类或者File类来进行读取,不过需要注意的是,在Android平台上,StreamingAssets路径下的文件是压缩的,无法直接使用File类读取,需要使用WWW类或UnityWebRequest类来进行读取。如:

复制代码
string filePath1 = Path.Combine(Application.streamingAssetsPath, "filename.txt");
string filePath2 = Path.Combine(Application.persistentDataPath, "filename.txt");
string filePath3 = Path.Combine(Application.dataPath, "filename.txt");


#if UNITY_ANDROID && !UNITY_EDITOR
// 在Android平台上,StreamingAssets路径下的文件是压缩的,需要使用WWW类来读取
StartCoroutine(LoadFile(filePath1));

#else
// 在其他平台上,可以直接使用File类来读取
string files1= File.ReadAllText(filePath1);
string files2= File.ReadAllText(filePath2);
string files3= File.ReadAllText(filePath3);
#endif

// 在Android平台上使用WWW类读取StreamingAssets路径下的文件
private IEnumerator LoadFile(string filePath)
{
    using (UnityWebRequest www = UnityWebRequest.Get(filePath))
    {
        yield return www.SendWebRequest();
        if (www.result == UnityWebRequest.Result.Success)
        {
            string fileContent = www.downloadHandler.text;
            Debug.Log(fileContent);
        }
        else
        {
            Debug.LogError("Failed to load file: " + www.error);
        }
    }
}
相关推荐
孟无岐11 小时前
【Laya】Socket 使用指南
websocket·typescript·游戏引擎·游戏程序·laya
tealcwu14 小时前
【Unity资源】Unity MCP 介绍
unity·游戏引擎
晚霞的不甘17 小时前
Flutter for OpenHarmony 引力弹球游戏开发全解析:从零构建一个交互式物理小游戏
前端·flutter·云原生·前端框架·游戏引擎·harmonyos·骨骼绑定
Thomas_YXQ18 小时前
Unity3D中提升AssetBundle加载速度的详细指南
java·spring boot·spring·unity·性能优化·游戏引擎·游戏开发
Miss_SQ19 小时前
Unity接入AI—Deepseek,数据流式传输
unity·ai
chillxiaohan20 小时前
unity 批量修改场景内字体工具
unity
晚霞的不甘1 天前
Flutter 方块迷阵游戏开发全解析:构建可扩展的关卡式益智游戏
前端·flutter·游戏·游戏引擎·游戏程序·harmonyos
ellis19702 天前
Unity中ScriptableObject用法整理
unity
玉梅小洋2 天前
Unity Muse 完整使用文档:Sprite+Texture专项
unity·ai·游戏引擎
能源革命2 天前
Three.js、Unity、Cesium对比分析
开发语言·javascript·unity