2

I'm building a Oculus Go App with Unity. I'm trying to create a txt file in the internal storage of the GO. (The one you see when you plug the HMD to your PC).

I tried those differents paths, none of them seems to work :

  • Application.persistentDataPath
  • /mnt/sdcard/myFolder
  • //storage/emulated/0/Android/data/myFolder.

If someone knows the correct one that would be cool Thank you !

tinkz
  • 100
  • 2
  • 17

3 Answers3

4

This is the write function:

File.WriteAllText(Path.Combine(_directoryPath, fileName), _text, System.Text.Encoding.ASCII);

This is the read function:

#if PLATFORM_ANDROID && !UNITY_EDITOR
        TextAsset textAsset = new TextAsset(System.IO.File.ReadAllText(filePathAndName));
#elif UNITY_EDITOR
        TextAsset textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(filePathAndName);
#endif

This is the path to use:

#if PLATFORM_ANDROID && !UNITY_EDITOR
        SavedTextsCompleteFilePath = Application.persistentDataPath;

        // just for debugging and playing in the editor
#elif UNITY_EDITOR
        SavedTextsCompleteFilePath = "Assets/Resources";
#endif
        // set the base file path, then add the directory if it's not there yet
        SavedTextsCompleteFilePath = MakeFolder(SavedTextsCompleteFilePath, "MyGameSaveFolder");
    }

and the helper function:

private string MakeFolder(string path, string savedTextsFolder)
{
    string saveDirectory = path + savedTextsFolder;
    if (!Directory.Exists(saveDirectory))
    {
        Directory.CreateDirectory(saveDirectory);
        Debug.Log("directory created! at: " + path);
    }
    return saveDirectory;
}
pale bone
  • 1,746
  • 2
  • 19
  • 26
1

Try using Application.streamingAssetsPath.

Richard Muthwill
  • 320
  • 2
  • 16
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/20814885) – Max Vollmer Sep 09 '18 at 23:40
  • 1
    The op asked for a directory and I gave him exactly what he asked for – Richard Muthwill Sep 10 '18 at 21:09
  • 3
    Then write your answer accordingly. You wrote "what about", not "using this will work". "what about" (and tbh also the "try using" that was edited by @JosephSible) implies that it might not work, and that you expect OP to respond (e.g. to check and come back with a report if it worked or not). That makes it a comment, not an answer. If this is *the* solution, make it clear, so reviewers don't misjudge. A proper explanation as to why and how a given solution works, even a simple one like a specific path, is always best anyway. – Max Vollmer Sep 10 '18 at 21:17
0

On the Oculus, you can read and write to Application.persistentDataPath. And you can only read from Application.streamingAssetsPath

I am not sure what failed. But I can r/w in the persistentDataPath.

Make sure you put a '/' between the directory and file name.

string FileName = Application.persistentDataPath + "/" + "file.txt";

Doug Royer
  • 67
  • 5