我尝试在不使用本机代码的情况下在 Android 上检索 Unity 中的缓存路径。我认为我可以使用 Application.temporaryCachePath。
此方法返回路径:
/storage/emulated/0/Android/data/com.<myapp>/cache
。
但是当我使用原生 Android 代码时,我发现它应该是:
/data/user/0/com.<myapp>/cache
。
有没有办法在Unity中检索Android本机代码使用的缓存路径?我发现的唯一可行的解决方法是:
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
string cache = jo.Call<AndroidJavaObject>("getCacheDir").Call<string>("getCanonicalPath");
它返回到
/data/data/com.<myapp>/cache
的预期路径。
有什么建议可以使用比上面那样将 Android 本机代码嵌入到 C# 中更干净的东西吗?也许 Unity 应用程序配置不正确?
如果您想使用 Unity,那么您正在使用的 Application.temporaryCachePath 是最好的选择。我记得我也遇到过和你一样的问题,然后我就自己实现了。
对于Andoroid,你的代码乍一看似乎是正确的,只需确保存储结果路径,这样你就不会每次都调用该方法。
不幸的是,最好的答案似乎已经在你的问题中了。
您可以使用
Application.persistentDataPath
。它指向 Android 上的 /storage/emulated/0/Android/data/<packagename>/files
。此外,您可以在其他平台上使用它,它将指向有效的缓存路径。检查 Unity 文档以获取更多详细信息 -> Application.persistentDataPath