Search Unity

[SOLVED] Special folder path in mac

Discussion in 'Scripting' started by Apache, May 25, 2009.

  1. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    Hi !!!
    Do someone of you know a wa to get the special path folder in mac with Unity?
    For example I wold like to get the path to the "<username>/Library/Application Support" to create a folder for my game nd save there the settings it need. That's because if I need to give a new version of the application, the user won't loose the preferences.
    I tried the C# "Environment.SpecialFolder" but it only works on Windows.

    THANKS GUYS !!! :wink:
     
  2. lankoski

    lankoski

    Joined:
    Apr 20, 2008
    Posts:
    148
    Would PlayerPrefs (http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html) class work for you?
     
  3. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    Thank you lankoski, but this is not usefull to me because I need to store some files created by the application itself so think that is not safe enough to use the Unity settngs system !!!

    I'm looking for something that allow me to create some files in a specific folder that I give to the application.
     
  4. lankoski

    lankoski

    Joined:
    Apr 20, 2008
    Posts:
    148
    Hmm.
    Code (csharp):
    1. Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/Library/Application Support");
    Would get you the directory, but obviously this does not work in Windows.

    This should give some directory in Mac and Windows
    Code (csharp):
    1. Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    In Mac you should get $HOME/.local/share directory an in Windows application data directory.
     
  5. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    Well... I already tried this
    Code (csharp):
    1. File.WriteAllBytes (Environment.SpecialFolder.Personal + "/Library/Application Support/ciao.dat", new byte[] {0,1,2,3,4,5,6,7,8,9});
    and it doesn't seems to work because
    Code (csharp):
    1. DirectoryNotFoundException: Destination directory not found: Personal/Library/Application Support
    so the function called by Environment cannot be used for mac.
    Also
    Code (csharp):
    1. Environment.SpecialFolder.LocalApplicationData
    doesn't seems to work for mac.

    I tried now the
    Code (csharp):
    1. $HOME/.local/share
    folder suggested but I got
    Code (csharp):
    1. DirectoryNotFoundException: Destination directory not found: $HOME/.local/share
    and to tell you the thruth I also tried
    Code (csharp):
    1. ~/Library/Application Support
    but nothing.
     
  6. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    Ok, I hadn't seen the first part of your codeline so I tried
    Code (csharp):
    1. Environment.GetFolderPath(Environment.SpecialFolder.Personal));
    and it gives me the root directory. Now I will try to get the rest part of the path.

    THANKS !!!

    By the way, I tried
    Code (csharp):
    1. File.WriteAllBytes (Environment.SpecialFolder.Personal + "/Library/Application Support/ciao.dat", new byte[] {0,1,2,3,4,5,6,7,8,9});
    in Windows and it works properly but in mac you should use
    Code (csharp):
    1. File.WriteAllBytes (Environment.GetFolderPath (Environment.SpecialFolder.Personal + "/Library/Application Support/ciao.dat", new byte[] {0,1,2,3,4,5,6,7,8,9}));
    but I really don't know why.
     
    iwaldrop and Frz95 like this.
  7. lankoski

    lankoski

    Joined:
    Apr 20, 2008
    Posts:
    148

    You need to check if the directory exists and if not then create it from the code.

    Code (csharp):
    1. using System.IO;
    2. ...
    3. if (!Directory.Exists (directoryString)){
    4.    Directory.CreateDirectory (directoryString);
    5. }
    (Untested code)
     
  8. Apache

    Apache

    Joined:
    Sep 26, 2008
    Posts:
    134
    Oh yes, I already did that in another portion of code.

    However all works now.

    Here's an example:
    Code (csharp):
    1. string path = "";
    2.     if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor) {
    3.             path = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData);
    4.     } else {
    5.             path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
    6.     } // if
    7.     return path;
    THANKS !!! :)
     
  9. Steven-Walker

    Steven-Walker

    Joined:
    Oct 27, 2010
    Posts:
    38
    The mac app store has special requirements for where the application data can be saved.
    http://developer.apple.com/devcente....html#//apple_ref/doc/uid/TP40010572-CH16-SW9

    ~/Library/Application Support/<app-identifier>
    ~/Library/<app-identifier>
    ~/Library/Caches/<app-identifier>

    I accomplished this with the following code, a bit different than stated above:
    Code (csharp):
    1. var dir : String = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    2. dir = dir.Replace("/Documents", "/Library/Application Support/[I]<app-identifier>[/I]/");
    3. if(!Directory.Exists(dir)) {
    4.     Directory.CreateDirectory(dir);
    5. }
    6.  
    7. FilePath = dir+"GameData.dat";
    If you don't use one of the above file paths (such as the default Application.dataPath), your app will probably be rejected.
     
    Solverlabs likes this.
  10. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    604
    This may be a bug....

    If you select the API Compatibility Level as "Net 2.0" (not the Subset option), the path in a Mac is correctly identified as the user folder and not the Documents folder.
     
  11. zazoum

    zazoum

    Joined:
    Sep 3, 2011
    Posts:
    78
    Sorry for highjacking the thread.
    I'm new and I try to get accustomed with Unity Engine API.
    And now I see all those classes and function not included in the Unity API documentation. Like "File.", "Environment", "WriteAllBytes()" etc.
    Are these from MSDN Library?
    Does Unity supports other Libraries?
    And if yes which ones?
    Are they cross-platform, and if not which ones for each platform?
     
  12. striche

    striche

    Joined:
    Jan 4, 2011
    Posts:
    61
    If it's important that you really use Application Support that's fine. If your primary concern is just meeting the Mac App Store Requirements you can just use Application.persistantDataPath. This seems to resolve to /Users/CURRENTUSER/Library/Caches/Compnay Name/Product Name. This would meet the Apple requirements as one of the listed directories that are allowed is ~/Library/Caches/<app-identifier>.