Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Local Android save data

Discussion in 'Android' started by faez, Apr 4, 2012.

  1. faez

    faez

    Joined:
    Jan 17, 2012
    Posts:
    11
  2. misterkid

    misterkid

    Joined:
    Feb 15, 2012
    Posts:
    79
    You can use the FileStream or StreamReader etc if you code in c# in unity

    Rought example
     
  3. MicroEyes_old

    MicroEyes_old

    Joined:
    Jun 9, 2011
    Posts:
    188
    FAEZ,
    You can use these functions for perform what you want....


    public void writeStringToFile( string str, string filename )
    {
    #if !WEB_BUILD
    string path = pathForDocumentsFile( filename );
    FileStream file = new FileStream (path, FileMode.Create, FileAccess.Write);

    StreamWriter sw = new StreamWriter( file );
    sw.WriteLine( str );

    sw.Close();
    file.Close();
    #endif
    }


    public string readStringFromFile( string filename)//, int lineIndex )
    {
    #if !WEB_BUILD
    string path = pathForDocumentsFile( filename );

    if (File.Exists(path))
    {
    FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read);
    StreamReader sr = new StreamReader( file );

    string str = null;
    str = sr.ReadLine ();

    sr.Close();
    file.Close();

    return str;
    }

    else
    {
    return null;
    }
    #else
    return null;
    #endif
    }


    public string pathForDocumentsFile( string filename )
    {
    if (Application.platform == RuntimePlatform.IPhonePlayer)
    {
    string path = Application.dataPath.Substring( 0, Application.dataPath.Length - 5 );
    path = path.Substring( 0, path.LastIndexOf( '/' ) );
    return Path.Combine( Path.Combine( path, "Documents" ), filename );
    }

    else if(Application.platform == RuntimePlatform.Android)
    {
    string path = Application.persistentDataPath;
    path = path.Substring(0, path.LastIndexOf( '/' ) );
    return Path.Combine (path, filename);
    }

    else
    {
    string path = Application.dataPath;
    path = path.Substring(0, path.LastIndexOf( '/' ) );
    return Path.Combine (path, filename);
    }
    }
     
    AliAfshari likes this.
  4. Raspilicious

    Raspilicious

    Joined:
    Jun 21, 2012
    Posts:
    12
    This worked perfectly for me, thank you so much!
     
  5. AliAfshari

    AliAfshari

    Joined:
    Sep 12, 2014
    Posts:
    13
    Awesome Code And Works Great