Search Unity

Get Application path problem

Discussion in 'iOS and tvOS' started by minevr, Nov 13, 2009.

  1. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    I would like to save and read the xml file. But the path is always incorrect. Who can help me?
    If I direct access to the path, then the $: / xxxx-xxx-xxx/data/filename.xml
    The real path is: $: / xxxx-xxx-xxx/appname.app/data/filename.xml
    I am programming problem, or bug?

    Thank you. :wink:
     
  2. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    // Application.dataPath returns something like "/var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/Data"

    Why not:like "/var/mobile/Applications/30B51836-D2DD-43AA-BCB4-9D4DADFED6A2/AppName.app/Data"
     
  3. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    My god!! This is a bug.... :( :( :(
     
  4. RazorCut

    RazorCut

    Joined:
    May 7, 2009
    Posts:
    393
    Try something like this:

    Code (csharp):
    1.  
    2.             string root = Application.dataPath.Substring(0, Application.dataPath.Length - 4) + "Documents";
    3.             if (System.IO.Directory.Exists(root))
    4.             {
    5.                 return root; // for phone
    6.             }
    7.             else
    8.             {
    9.                 return ""; // for development on PC (not phone)
    10.             }
    11.  
     
  5. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    Thank for all,I get it, :D

    But,Not have get "MYappname.app"is bug
     
  6. bernardfrancois

    bernardfrancois

    Joined:
    Oct 29, 2009
    Posts:
    373
    Does anyone know a way to get the name of the app (in code), so the appropriate number of characters can be removed from the Application.dataPath string?
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    the datapath handling and output has changed since then actually.
     
  8. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    It's probably much safer to use '/../' than Substring.


    For Documents I currently use: Application.dataPath + "/../../Documents/";

    It works like a charm for me.
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    If you have iPhone Advanced using ObjC is much faster and especially future proof cause it does not depend on apple not messing with the structure.

    The following function for example returns the path to the cache dir which is what you commonly want to use (not documents - cause cache is not backed up in itunes. you would use this for downloadable levels etc):

    put this into an .mm file in your xcode project
    Code (csharp):
    1. extern "C" const char * getCacheDirectory()
    2. {
    3.     NSString* cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    4.     return cocoaToMonoString(cacheDir);
    5. }

    and in unity add a C# class to the plugins folder and add
    Code (csharp):
    1.     [DllImport ("__Internal")]
    2.     public static extern string getCacheDirectory();
    dang and you can happily ask your os about the path to cache directory :)

    To get the documents directory, replace NSCachesDirectory with NSDocumentDirectory
     
  10. LKIM

    LKIM

    Joined:
    Feb 17, 2011
    Posts:
    40
    You can also use `new DirectoryInfo(Application.dataPath).Parent` instead of '/../'.