Get cache path for Unity on Android

333 Views Asked by At

I try to retreive a path to cache in Unity on Android without using the native code. I thought that I could use Application.temporaryCachePath.

This method returns the path: /storage/emulated/0/Android/data/com.<myapp>/cache.

But when I use native Android code I see it should be: /data/user/0/com.<myapp>/cache.

Is there any way to retrieve in Unity the path to the cache which is used by Android native code? The only working workaround I found is:

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
string cache = jo.Call<AndroidJavaObject>("getCacheDir").Call<string>("getCanonicalPath");

It returns the expected path to /data/data/com.<myapp>/cache.

Any suggestions to use something cleaner than embedding android native code into c# like above? Maybe the Unity app is configured incorrectly?

1

There are 1 best solutions below

3
Davian_Machete On

You can use the Application.persistentDataPath. It points to /storage/emulated/0/Android/data/<packagename>/files on Android. Also, you can use it on other platforms and it will point to a valid cache path. Check the unity docs for more details -> Application.persistentDataPath