Unity 中 Application 四种常用目录总结

Unity 中 Application 提供了多种目录路径用于不同场景,以下是四种常用目录路径的区别、用途和跨平台下的具体路径示例:


1. Application.dataPath

描述

  • 指向项目的 数据目录
  • 在编辑器中指向 Assets 目录,打包后的应用程序中是构建的 Data 文件夹路径。

用途

  • 只读,通常用于访问内部资源。

示例路径

  • Windows(编辑器): C:/YourProject/Assets
  • Windows(打包): C:/YourGame/GameName_Data
  • Android: jar:file:///data/app/YourPackageName/base.apk!/assets
  • iOS: /var/containers/Bundle/Application/YourAppID/YourAppName.app/Data

示例代码

  • Windows:
    string dataPath = Application.dataPath;
    Debug.Log("Data Path on Windows: " + dataPath);
    // 输出示例: C:/Users/Username/Projects/MyGame/Builds/MyGame_Data
    
  • Android:
    string dataPath = Application.dataPath;
    Debug.Log("Data Path on Android: " + dataPath);
    // 输出示例: /data/app/com.company.mygame-1/base.apk
    

2. Application.persistentDataPath

描述

  • 指向应用的 持久化存储目录,适合保存需要长期存储的数据(如存档、设置)。
  • 数据不会因应用更新或重启丢失,但可能因用户清除数据而丢失。

用途

  • 可读写,适合存储 游戏存档、设置文件 等。

示例路径

  • Windows: C:/Users/YourUsername/AppData/LocalLow/CompanyName/GameName
  • Android: /data/data/YourPackageName/files
  • iOS: /var/mobile/Containers/Data/Application/YourAppID/Documents

-示例代码:

  • Windows:
    string streamingAssetsPath = Application.streamingAssetsPath;
    Debug.Log("Streaming Assets Path on Windows: " + streamingAssetsPath);
    // 输出示例: C:/Users/Username/Projects/MyGame/Builds/MyGame_Data/StreamingAssets
    
  • Android:
    string streamingAssetsPath = Application.streamingAssetsPath;
    Debug.Log("Streaming Assets Path on Android: " + streamingAssetsPath);
    // 输出示例: jar:file:///data/app/com.company.mygame-1/base.apk!/assets
    

3. Application.streamingAssetsPath

描述

  • 指向 Assets/StreamingAssets 文件夹。
  • 文件 只读,打包后路径因平台而异,适合存放初始资源、配置文件等。

用途

  • 存储随应用分发的文件(例如配置文件、媒体文件)。

示例路径

  • Windows(编辑器): C:/YourProject/Assets/StreamingAssets
  • Windows(打包): C:/YourGame/GameName_Data/StreamingAssets
  • Android: jar:file:///data/app/YourPackageName/base.apk!/assets
  • iOS: /var/containers/Bundle/Application/YourAppID/YourAppName.app/Data/Raw

示例代码:

  • Windows:
    string persistentDataPath = Application.persistentDataPath;
    Debug.Log("Persistent Data Path on Windows: " + persistentDataPath);
    // 输出示例: C:/Users/Username/AppData/LocalLow/Company/ProductName
    
  • Android:
    string persistentDataPath = Application.persistentDataPath;
    Debug.Log("Persistent Data Path on Android: " + persistentDataPath);
    // 输出示例: /data/data/com.company.mygame/files
    

4. Application.temporaryCachePath

描述

  • 指向 临时存储目录,数据可能会被系统清理。
  • 适合存储缓存文件、临时数据。

用途

  • 可读写,适合存储 下载的临时数据 等。

示例路径

  • Windows: C:/Users/YourUsername/AppData/LocalLow/CompanyName/GameName/Cache
  • Android: /data/data/YourPackageName/cache
  • iOS: /var/mobile/Containers/Data/Application/YourAppID/Library/Caches

示例代码:

  • Windows:
    string temporaryCachePath = Application.temporaryCachePath;
    Debug.Log("Temporary Cache Path on Windows: " + temporaryCachePath);
    // 输出示例: C:/Users/Username/AppData/Local/Temp/Company/ProductName
    
  • Android:
    string temporaryCachePath = Application.temporaryCachePath;
    Debug.Log("Temporary Cache Path on Android: " + temporaryCachePath);
    // 输出示例: /data/data/com.company.mygame/cache
    

总结

属性功能描述可读/可写示例路径(Windows/Android/iOS)
Application.dataPath数据目录,存储应用数据文件只读C:/YourGame/GameName_Data / jar:file:///.../base.apk!/assets / /YourAppName.app/Data
Application.persistentDataPath持久化存储目录,用户可访问可读可写C:/Users/.../LocalLow/... / /data/data/.../files / /Documents
Application.streamingAssetsPathStreamingAssets 文件路径只读C:/YourGame/.../StreamingAssets / jar:file:///.../base.apk!/assets / /YourAppName.app/Data/Raw
Application.temporaryCachePath临时缓存目录可读可写C:/Users/.../Cache / /data/data/.../cache / /Library/Caches

  • Application.dataPath:用于访问应用程序的资源文件,通常是只读的。
  • Application.streamingAssetsPath:用于存储和访问需要在运行时动态加载的文件,通常用于大文件。
  • Application.persistentDataPath:用于存储需要长期保存的数据,不会随着应用的更新而被清除。
  • Application.temporaryCachePath:用于存储临时文件,这些文件在应用退出时可能会被清理。

这些路径在不同平台上的具体路径可能会有所不同,但它们的用途和行为是相似的。通过正确使用这些路径,可以确保应用程序在不同平台上都能正确地管理和访问文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值