Pavel Pája Halbich
Pavel Pája Halbich

Reputation: 1583

Unity game - make saved data private (invisible to other than my app)

I'm using Application.persistentDataPath path to save graphical content required by my application. Unfortunatelly, I can't anyhow force Unity to make it private (Android docs), thus it is visible and could be grabbed by any user with basic knowledge of Android from path Android/data/com.mycompany.myapp/files/. I tried more things, among it setting Player Settings / Write permission to Internal didn't solved it.

I'm aware that I can create native Intents and run them from Unity game, but it looks like it is achieved by reflection and I need to load the data really fast

This content can't be delivered with app (it is downloaded after user's purchase) and it is expected to be loaded as fast as possible - it has a lot of images - together having ~ 200MB at least.

How can I force Unity to use private storage? Or how can I hide the whole folder from User at least?

Upvotes: 1

Views: 1382

Answers (1)

Pavel Pája Halbich
Pavel Pája Halbich

Reputation: 1583

I managed to find a simple solution, so here it is for you:

I called Android native function getDir with my desired private folder's name and stored it to a static property ap. If I'm not on Android, I use Application.persistentDataPath.

RESULT:

persistentDataPath returns /storage/emulated/0/Android/data/com.mycomp.myapp/files

getDir returns /data/user/0/com.mycomp.myapp/app_pFiles

Full code:

public static class FolderHelper
{
    // cached value
    private static string ap;

    static FolderHelper()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        try
        {
            using (AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
            {
                using (AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"))
                {
                    ap = jo.Call<AndroidJavaObject>("getDir", "pFiles", 0).Call<string>("getAbsolutePath");
                }
            }
        }
        catch (System.Exception e)
        {
            Debug.LogWarning(e.ToString());
            ap = Path.GetFullPath(Application.persistentDataPath);
        }
#else
        ap = Path.GetFullPath(Application.persistentDataPath);
#endif
    }


    // other code depending on property ap

}

Upvotes: 1

Related Questions