Search Unity

Resolved Cannot read files in Application.persistentDataPath in Unity Editor for Mac & iOS

Discussion in 'macOS' started by ronchito, Feb 18, 2023.

  1. ronchito

    ronchito

    Joined:
    Dec 4, 2013
    Posts:
    12
    I have an issue regarding reading files in Unity editor (using Mac M1) and iOS devices (tested on my iPhone).
    In my previous version of the game this ability worked as intended, but now it doesn't work anymore.
    Which is strange, since I didn't change to code, and I can see that it still works in the old version) It also still works well on Windows and Android.

    I upgraded from Unity 2019 LTS to 2021 LTS and it might cause this problems.

    I save the file into the following path successfully: Application.persistentDataPath + "/Files/".

    But when it comes to reading that file, it doesn't seem to find that file in that path in unity editor (for Mac) and iOS devices.

    I thought maybe it's related to the ' \ ' difference in the path on Mac.

    Printed path of Application.persistentDataPath:
    /Users/{username}/Library/Application Support/{company}/{my app}/Files/{FileName}

    The actual file is in this path:
    /Users/{username}/Library/Application\ Support/{company}/{my\ app}/Files/{FileName}

    Could it be because of the ' \ ' character for the space indication?

    Does anyone know how to read files properly in Unity Editor (using Mac) & iOS?

    Thanks in advance!
     
  2. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    523
    This is quite likely because of '\' ; replace it by '/'.
     
  3. ronchito

    ronchito

    Joined:
    Dec 4, 2013
    Posts:
    12
    Thanks for the super quick response!
    I solved the issue by removing all "file://" prefixes when using Mac or iOS platforms and also used the following Uri method:

    Code (CSharp):
    1. Uri baseUri = new Uri(path);
    2. Uri myUri = new Uri(baseUri, fileNameWithExtenstion);
    3. using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(myUri.AbsoluteUri, AudioType.WAV))