Search Unity

  1. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  2. Unity 2017.1 is now released.
    Dismiss Notice
  3. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  4. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Application.dataPath ... not fixed ??? BUG ?

Discussion in 'iOS and tvOS' started by scinfu, Mar 9, 2010.

  1. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    403
    I'm trying to write and read a file with Application.dataPath ... returns an error that I can not write on that path.

    finding and using print, I discovered that the class Application.dataPath returns a path with a folder Data. Then I reading in manual confirmed the discovery wow. <path to player app bundle> / <AppName.app> / Date

    but the folder should not be Documents "<path to player app bundle> / <AppName.app> / Documents" ?

    or simply the path of the game <path to player app bundle> / <AppName.app> ?

    Unity iPhone 1.6 did not fix the problem but it has only changed without resolving it.

    in other version i used
    Code (csharp):
    1.  
    2. Application.dataPath.Substring(0, Application.dataPath.Length - 4) + "Documents/";
    and work .

    Now with the same code the path is right (...app/Documents) but dont work the error is :
    Code (csharp):
    1.  
    2. System.IO.DirectoryNotFoundException: Could not find a part of the path "/var/mobile/Applications/3FCB4B99-075A-468E-BA43-B6AF6881B1EE/Ricky.appDocuments/Prefs.txt".
    3.   at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000]
    4.   at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000]
    5.   at System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) [0x00000]
    6.   at System.IO.StreamWriter..ctor (System.String path) [0x00000]
    7.   at LevelEditor.SaveData () [0x00000]  
    8. (Filename: /Users/build/builds/unity-iphone-1.6/iphone-1.6/Projects/../Runtime/Export/Generated/BaseClass.cpp Line: 1719)
    9.  

    something wrong or is a bug ?
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    What do you think:

    :D
     
  3. redd

    redd

    Joined:
    Jul 23, 2009
    Posts:
    122
    I've found this works for me:

    Code (csharp):
    1. currentUserPath = Application.dataPath.Replace("Data", "Documents");
     
  4. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    403
    path = Application.dataPath.Substring(0, Application.dataPath.Length - 4) + "/Documents/";
    Code (csharp):
    1. System.IO.DirectoryNotFoundException: Could not find a part of the path "/var/mobile/Applications/70BC50EA-9A96-4ABE-96A8-6F309356C8E1/Ricky.app/Documents/Prefs.txt".
    2. ...
    path = Application.dataPath + "/Documents/";
    Code (csharp):
    1. System.IO.DirectoryNotFoundException: Could not find a part of the path "/var/mobile/Applications/6126B9A5-F6A7-4DD7-B750-45F07CB101C3/Ricky.app/Data/Documents/Prefs.txt"
    2. ...
    3. .
    path = Application.dataPath ;
    Code (csharp):
    1. System.UnauthorizedAccessException: Access to the path "/var/mobile/Applications/E9E7A93C-2D26-47F6-AB5E-740F1C659040/Ricky.app/Prefs.txt" is denied.
    Application.dataPath.Replace("Data", "Documents"); not work for me .

    you use Unity iPhone 1.6 ?
     
  5. redd

    redd

    Joined:
    Jul 23, 2009
    Posts:
    122
    The below path you have looks right to me.

    Code (csharp):
    1. /var/mobile/Applications/70BC50EA-9A96-4ABE-96A8-6F309356C8E1/Ricky.app/Documents/Prefs.txt
    (although I could be wrong. I'm not at my dev machine at the moment.)

    You might want to confirm that your Prefs.txt file is, indeed, actually there in the directory though. Go into Xcode's organizer, download your app's data directory and you can confirm if what you think should be there really is.
     
  6. redd

    redd

    Joined:
    Jul 23, 2009
    Posts:
    122
    Oh, and to answer your other question, no. I'm not using 1.6 yet, so it's very possible that my replace data w/ documents trick may not work there.
     
  7. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    403
    here a sample script whit some tests .

    Any idea ?
    :D :D :D at this moment do not update if you use it . with 1.5 work i know
     

    Attached Files:

  8. ESB

    ESB

    Joined:
    Mar 2, 2010
    Posts:
    71
    As far as I understand it from the documentation, you do not want to write to <appHomePath>/<appName>.app/Documents/, but to <appHomePath>/Documents/

    One simple* code solution (C#) would be something like:

    Code (csharp):
    1.  
    2. /.../
    3.  
    4. using System.Collections;
    5. using System.IO;
    6.  
    7. /.../
    8.  
    9. string fileLocation, newFileLocation;
    10. char[] pathPart;
    11. int lastSeparatorIndex;
    12.  
    13. string MyFilePath()
    14. {
    15.     //Returns file path up to, but not including, file name
    16.  
    17.     fileLocation = Application.dataPath;
    18.  
    19.     lastSeparatorIndex = fileLocation.LastIndexOf(Path.DirectorySeparatorChar)
    20.     pathPart = new char[ lastSeparatorIndex + 1];
    21.     fileLocation.CopyTo(0, pathPart, 0, lastSeparatorIndex + 1);
    22.     newFileLocation = new string(pathPart) + "Documents" + Path.DirectorySeparatorChar;
    23.  
    24.     return newFileLocation;
    25. }
    26.  
    27.  

    *) Probably improvable, but at least functioning - as far as I can tell. Well, with a caveat for typoes in this forum-adapted snippet.

    EDIT: Simplified the snippet a bit.
     
  9. scinfu

    scinfu

    Joined:
    Oct 23, 2008
    Posts:
    403
    thanks for the help,
    I solved by writing a native script to give me the right directory.

    in any case the problem remains ...
    I wrote to unity without a response from them.

    Having +100'000 users is degrading the service .


    If I am wrong I apologize to the unity team
     
  10. ESB

    ESB

    Joined:
    Mar 2, 2010
    Posts:
    71
    Sounds like a better solution.

    PS: My snippet is b0rx0red since the appPath ends with a /, so it'd have to be something like:

    Code (csharp):
    1.         lastSeparatorIndex = fileLocation.LastIndexOf(separatorChar);
    2.         pathPart = new char[lastSeparatorIndex];
    3.         fileLocation.CopyTo(0, pathPart, 0, lastSeparatorIndex);
    4.         lastSeparatorIndex = new string(pathPart).LastIndexOf(separatorChar);
    5.         pathPart = new char[lastSeparatorIndex + 1];
    6.         fileLocation.CopyTo(0, pathPart, 0, lastSeparatorIndex + 1);
    7.         newFileLocation = new string(pathPart) + "Documents" + separatorChar;
    8.  
    which is decidely unpretty.
     
  11. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    more the fact that it is GDC this week and UT has likely had a large focus on that this and the recent weeks (the dark unity demo is the gdc demo very likely for example) on their non-dev staff
     
  12. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
  13. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    the teamsplit though has no impact on support as the support team is global independent of both.
     
  14. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    I was under the impression that the support team would still route the work (fixes) to the iPhone team ?
     
  15. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    I doubt that.
    The instance to send it to normally would likely be the QA team to see if a problem is replicable / exists at all and then stuff is forwarded.
    Devs have better things to do
     
  16. imparare

    imparare

    Joined:
    Jun 24, 2008
    Posts:
    369
    Sorry for still being off topic.

    I thought the iPhone devs were all in a big locked room in Lithuania and some answer stuff on here and other support stuff. That is why I considered that the iPhone Devs all get involved in the support.

    Maybe one of them could explain the support process for iPhone ?
     
  17. Mantas-Puida

    Mantas-Puida

    Unity Technologies

    Joined:
    Nov 13, 2008
    Posts:
    1,721
    Chilly and prolonged winter in Lithuania definitely slowed down our response time :)

    In Unity iPhone manual section "Preparing your application for "In App Purchases" you can find sample code how to write something to "Documents" folder on device:

    Code (csharp):
    1.  
    2. public static string GetiPhoneDocumentsPath ()
    3.         {
    4.                 // Your game has read+write access to /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/Documents
    5.                 // Application.dataPath returns              
    6.                 // /var/mobile/Applications/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/myappname.app/Data
    7.                 // Strip "/Data" from path
    8.                 string path = Application.dataPath.Substring (0, Application.dataPath.Length - 5);
    9.                 // Strip application name
    10.                 path = path.Substring(0, path.LastIndexOf('/'));  
    11.                 return path + "/Documents";
    12.         }
    13.  
     
  18. ikeo

    ikeo

    Joined:
    Jul 28, 2007
    Posts:
    51
    I had a problem after upgrading to 1.6 when using application.dataPath.

    I found this article:
    http://wiki.monotouch.net/HowTo/Files/HowTo:_Store_Files

    It's a help article for MonoTouch, but explains how to use Mono's Environment.SpecialFolder enumeration.

    This code works for me to get the proper "Documents" directory for 1.6:

    Code (csharp):
    1. string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Documents");
    Hope this helps.
     
  19. ESB

    ESB

    Joined:
    Mar 2, 2010
    Posts:
    71
    Well... To paraphrase a favourite character in a favourite game:

    You are ridiculously awesome!

    Thanks for sharing this little treasure!