Search Unity

File I/O via Unity

Discussion in 'Scripting' started by Xen, Nov 22, 2009.

  1. Xen

    Xen

    Joined:
    Nov 18, 2009
    Posts:
    17
    If I create a project that wants to save data to an external file - how is this done in a Unity app?

    e.g. Save/Load user preferences in a INI file or retain game progress, level info and anything else that might be useful to have in permanent storage.

    I know I can read in text data via TextAsset but how do I write out data and control the formatting (as in a INI file)?
     
  2. Ender13

    Ender13

    Joined:
    May 3, 2009
    Posts:
    91
    You'll have to use C#'s StreamReader and StreamWriter. Have to create your own method of writing and parsing the data though.
     
  3. Xen

    Xen

    Joined:
    Nov 18, 2009
    Posts:
    17
    I don't suppose anyone has already done this and created a few File i/o classes/functions that we can just plug into our projects?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,090
    Have a look at the various functions in System.IO. Note that this is related to Mono (.net), not C#.

    --Eric
     
  5. Xen

    Xen

    Joined:
    Nov 18, 2009
    Posts:
    17
    The more I grapple with this thing, the more fragmented the information sources seem to be and the steeper the learning curve becomes. <sigh>

    I dunno where on earth I got the idea this was going to be easy.

    Thanks Eric for your pointer.
     
  6. Troy-Dawson

    Troy-Dawson

    Joined:
    Nov 2, 2009
    Posts:
    120
    I just posted a sample StreamReader last night, check out the Road Reader class for how to read a text file from your asset bundle.

    http://forum.unity3d.com/viewtopic.php?p=232729#232729

    I didn't know anything about .net I/O until I started this last night, though having 30 years of programming experience helped me to figure out the best (?) direction to go as I was learning.

    Managed code is a lot easier than the old way, but the learning curve is indeed fiercer. dotnet / C# programming is the summation of 40 years of API and language design and the concepts are not easy to understand.

    But with practice comes competency!
     
  7. bernardfrancois

    bernardfrancois

    Joined:
    Oct 29, 2009
    Posts:
    370
  8. bernardfrancois

    bernardfrancois

    Joined:
    Oct 29, 2009
    Posts:
    370
    As erique pointed out here, since Unity 3.3 there's a way to do it without platform-specific code using #if statements.

    Check the updated blog post for more information.
     
  9. blueddarker

    blueddarker

    Joined:
    Jan 17, 2015
    Posts:
    13
  10. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,398
    It is, actually. In your project settings set API Compatibility Level to .Net 2.0 and then use the standard .Net I/O functionality.

    How to write files:
    Code (csharp):
    1. using (var writer = new BinaryWriter(File.Open(filename, FileMode.CreateNew)))
    2. {
    3.     writer.Write(123);
    4.     writer.Write(123.456f);
    5.     writer.Write("Hello, World!");
    6. }
    How to read files:
    Code (csharp):
    1. using (var reader = new BinaryReader(File.Open(filename, FileMode.Open)))
    2. {
    3.     var integer = reader.ReadInt32();
    4.     var real = reader.ReadSingle();
    5.     var text = reader.ReadString();
    6. }
    Also check File.ReadAllText, File.ReadAllLines, File.WriteAllText, File.WriteAllLines inside System.IO namespace.
     
    DragonRider likes this.
  11. DragonRider

    DragonRider

    Joined:
    Jul 19, 2012
    Posts:
    9
    I've been looking into generating levels from text files and wondered where to find the System.IO api reference. Invaluable, thanks! :D
     
  12. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,473
    Just follow the link
     
  13. Caedriel

    Caedriel

    Joined:
    Jun 19, 2015
    Posts:
    8
    any clue on how to generate levels using file IO ? been trying it for a while and am lost