Patryk Kowalski
Patryk Kowalski

Reputation: 337

Get cache path for Unity on Android

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?

Upvotes: 1

Views: 1098

Answers (2)

Jorge Galv&#227;o
Jorge Galv&#227;o

Reputation: 1893

If you want to use Unity, the Application.temporaryCachePath that you are using is the best you can do. I recall stumbling with the same issue as you in the past, and I resorted implementing my own (for Android and iOS in that case).

In the case of Android, your code seems correct at the first glance, just make sure to store the resulting path so that you do not keep calling that method every time.

So unfortunately it seems that the best answer is already in your question.

Upvotes: 0

Davian_Machete
Davian_Machete

Reputation: 34

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

Upvotes: -1

Related Questions