Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Application.persistentDataPath and the "&" character (Unity 2019.4)

Discussion in 'Scripting' started by Benjamin_TYM, Aug 12, 2022.

  1. Benjamin_TYM

    Benjamin_TYM

    Joined:
    Jun 9, 2021
    Posts:
    11
    Hi,

    Had an issue today with Application.persistentDataPath returning " _ " instead of "&" in the product name, so returns "A _ B" instead of "A & B" in the path name.

    This was previously working but at some point the "A _ B" folder was created. Once deleting the "A _ B" folder in AppData/LocalLow it then returned "A & B" correctly again in the path.

    We updated from 2019.4.26 to 2019.4.40 recently but otherwise code has remained the same.

    Is there a reason for this to have happened? Just want to make sure this doesn't end up happening at runtime with release builds. Likewise is there a way to prevent / work around this happening?

    Many Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Never use any character except A to Z and 0 to 9 in files and directory names.

    Every other character is rolling the dice, especially when you consider the layers of software your path must go through: your code, the compiler, Unity, the middleware used to build the binary, the operating system, the underlying filesystem drivers, etc.

    Just say no to funny characters. They're just not ... fun.
     
  3. Benjamin_TYM

    Benjamin_TYM

    Joined:
    Jun 9, 2021
    Posts:
    11
    To follow from that, is it possible to keep "&" in the product name within Unity and set Application.persistentDataPath to use a non-special characters path?
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    What do you want the path to look like?
     
  5. Benjamin_TYM

    Benjamin_TYM

    Joined:
    Jun 9, 2021
    Posts:
    11
    I'm guessing "A & B" (Product Name) becomes "DataPath\AandB" (Application.persistentDataPath) or something similar (removing special characters)? Honestly I'm not sure what best practice is. I'm happy with a folder called "DataPath\A & B" but as Kurt mentions above, some systems seem to struggle.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Don't guess, use Debug.Log to find out for sure.

    Debug.Log("Path = " + Application.persistentDataPath);
     
  7. Benjamin_TYM

    Benjamin_TYM

    Joined:
    Jun 9, 2021
    Posts:
    11
    See original post. I don't really care what the path Application.persistentDataPath is, just that it is consistent.
    Application.persistentDataPath returned "A _ B" instead of "A & B".

    Application was never called "A _ B" but at some point created that folder using Application.persistentDataPath.
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Got it, I was just wondering how you were debugging and confirming the different paths.
     
  9. Benjamin_TYM

    Benjamin_TYM

    Joined:
    Jun 9, 2021
    Posts:
    11
    I'm using a Editor menu script to open the Application.persistentDataPath in windows explorer easily.


    Code (CSharp):
    1.  
    2. [MenuItem("Helpers/Locate",priority=-100)]
    3. public static void Locate()
    4. {
    5.     if (Directory.Exists(Application.persistentDataPath.Replace("/", @"\")))
    6.         System.Diagnostics.Process.Start("Explorer.exe", "/open, " + Application.persistentDataPath.Replace("/", @"\"));
    7.     else
    8.         Debug.Log("Folder Does not Exist. " + Application.persistentDataPath.Replace("/", @"\").ToString() );
    9. }
    10.  
    If the Product Name is named "A & B" it will go to the correct folder named "A & B" in windows explorer.
    However if a windows folder called "A _ B" exists in that same folder, the function above will return "Folder Does not Exist. [path]\A _ B". In this case Application.persistentDataPath should return [path] + "A & B" however is returning [path] + "A _B". In addition to this it's not opening "A _ B" even though it exists.

    I haven't worked out when the folder "A _ B" get created. It's happened a second time but i'm not sure when.

    I haven't been able to confirm but once the "A _ B" folder gets created it causes some issues with loading and save data using Application.persisitentDataPath as well. I haven't fully investigated this yet though.
     
    Last edited: Aug 16, 2022
  10. Benjamin_TYM

    Benjamin_TYM

    Joined:
    Jun 9, 2021
    Posts:
    11
    Ah found the issue.
    A windows build will create a folder "A _ B" instead of "A & B" which then causes all the issues.
    In Editor will create folder "A & B"
    If both folders exist then Editor will not return either.

    Windows 10 - Unity 2019.4.40f1

    I'm assuming this is a Unity bug that needs to be resolved?
     
    Last edited: Aug 16, 2022
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    I'm guessing it's Windows. Last I checked,
    &
    is not a valid path character.

    Again I'll say it: use only A-Z and 0-9 for files and paths or else you become a test pilot.
     
  12. Benjamin_TYM

    Benjamin_TYM

    Joined:
    Jun 9, 2021
    Posts:
    11
    Agreed however I would like an "&" in the product name, which Application.persistentDataPath is derived from.