Search Unity

Resolved How to save anything inside your game folder on Android [CHECK FIRST REPLY]

Discussion in 'Android' started by SirioRigel, Aug 1, 2021.

  1. SirioRigel

    SirioRigel

    Joined:
    Apr 10, 2021
    Posts:
    27
    So I've been trying to save some settings to a text file on android but it doesn't work! here's the code that tryes to read from the file, it works on Pc but not on android:
    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.         if (Application.platform == RuntimePlatform.Android)//chekcs if the platform is mobile
    4.         {
    5.             playerPrefs = "/storage/emulated/0/Android/data/com.RigelPixel.OreMiner/files/PreferencesSave.txt"; //define the path
    6.             if (File.Exists(playerPrefs))  //check if it exists
    7.             {
    8.                 Debug.Log("Exists on Android"); //working from the editor this shouldn't pop up
    9.                 settingsArray = File.ReadAllLines(playerPrefs);//assign the value to an array
    10.                 audioOn = Convert.ToBoolean(settingsArray[0]); //assign the boolean value to the audio
    11.                 selectedbutton = Convert.ToInt32(settingsArray[1]); //we assign the particles value
    12.             }
    13.         }
    14.         else
    15.         {
    16.             playerPrefs = "Assets/Texts/PreferencesSave.txt";  //define the path
    17.             if (File.Exists(playerPrefs))      //check if it exists
    18.             {
    19.                 Debug.Log("Exists");
    20.                 settingsArray = File.ReadAllLines(playerPrefs);   //assign the value to an array
    21.                 audioOn = Convert.ToBoolean(settingsArray[0]);    //assign the boolean value to the audio
    22.                 selectedbutton = Convert.ToInt32(settingsArray[1]); //we assign the particles value
    23.             }
    24.         }
    25.     }
    this is instead the write function, again, this works only on pc and not on android:

    Code (CSharp):
    1.         string[] settings = { audioOn.ToString(), selectedbutton.ToString() }; //defines an array to write on the text file
    2.         if (File.Exists(playerPrefs))     //checking if it exists
    3.         {
    4.             File.WriteAllLines(playerPrefs, settings);     //writing to the file with the path and the array corresponding to the lines
    5.         }
     
  2. SirioRigel

    SirioRigel

    Joined:
    Apr 10, 2021
    Posts:
    27
    Ok so I resolved, if someone has problem saving anything inside the application folder, which is located at /storage/emulated/Android/data/YourCompanyName/files/ , you have to use the Persistent Datapath function like it follows:
    NOTE: This code won't work if you are trying to save something outside your game folder because you will need to require external data read and write permission; also in this example I show you how you can read from a text file using a string array

    Code (CSharp):
    1.    private void Awake()
    2.     {
    3.         if (Application.platform == RuntimePlatform.Android)       //chekcs if the platform is mobile
    4.         {
    5.             playerPrefs = Application.persistentDataPath + "/PreferencesSave.txt";  //we define the path of our file
    6.             if (File.Exists(playerPrefs))                       //check if it exists
    7.             {
    8.                 settingsArray = File.ReadAllLines(playerPrefs);      //assign the value to an array
    9.                 audioOn = Convert.ToBoolean(settingsArray[0]);       //assign the boolean value to the audio
    10.                 selectedbutton = Convert.ToInt32(settingsArray[1]);  //we assign the particles value
    11.             }
    12.             else
    13.             {
    14.                 File.Create(playerPrefs);   //if it doesn't exists we create the file using the path that we assigned
    15.                 settingsArray = File.ReadAllLines(playerPrefs);      //assign the value to an array
    16.                 audioOn = Convert.ToBoolean(settingsArray[0]);       //assign the boolean value to the audio
    17.                 selectedbutton = Convert.ToInt32(settingsArray[1]);  //we assign the particles value
    18.             }
    19.         }
    20.         else
    21.         {
    22.             playerPrefs = "Assets/Texts/PreferencesSave.txt";   //define the path
    23.             if (File.Exists(playerPrefs))                       //check if it exists
    24.             {
    25.                 settingsArray = File.ReadAllLines(playerPrefs);      //assign the value to an array
    26.                 audioOn = Convert.ToBoolean(settingsArray[0]);       //assign the boolean value to the audio
    27.                 selectedbutton = Convert.ToInt32(settingsArray[1]);  //we assign the particles value
    28.             }
    29.             else
    30.             {
    31.                 File.Create(playerPrefs);   //if it doesn't exists we create it
    32.                 settingsArray = File.ReadAllLines(playerPrefs);      //assign the value to an array
    33.                 audioOn = Convert.ToBoolean(settingsArray[0]);       //assign the boolean value to the audio
    34.                 selectedbutton = Convert.ToInt32(settingsArray[1]);  //we assign the particles value
    35.             }
    36.         }
    37.     }
     
    Last edited: Aug 3, 2021