Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Save settings and load from a text file

Discussion in 'Scripting' started by 3B00DG4MER, Aug 2, 2016.

  1. 3B00DG4MER

    3B00DG4MER

    Joined:
    Jun 24, 2015
    Posts:
    64
    Hi guys,
    i have public class containing settings of game graphics,i want to set new values to those variables on game start up and save them on button save,
    i have no idea how to do that, even thought i checked the Docs, i couldn't understand ..
    Help me plz

    Code (CSharp):
    1. public class GameSettings  {
    2.  
    3.     public int qualitylevel;
    4.     public bool fullscreen;
    5.     public int textureQuality;
    6.     public int AA;
    7.     public int vSync;
    8.     public int resolution;
    9.     public bool realtimeReflection;
    10. }
     
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    PlayerPrefs are ideal for these kinds of settings.
     
  3. 3B00DG4MER

    3B00DG4MER

    Joined:
    Jun 24, 2015
    Posts:
    64
    I want a text file in case the user had problem with the video problems, so he can change it outside the game, you know what i mean .. ?
     
  4. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Yes, makes sense. Then I'd suggest using JsonUtility to convert back and forth between your class and a JSON string. And then you can use normal C# file IO classes for loading and saving.

    File.ReadAllText will read text from a file into a string. File.WriteAllText will write a string out to a file. And Application.persistentDataPath is a Unity property that will give you a path safe for storing application data.
     
  5. 3B00DG4MER

    3B00DG4MER

    Joined:
    Jun 24, 2015
    Posts:
    64
    Thank you,I'll tell you if i'm having problem ;)
     
  6. 3B00DG4MER

    3B00DG4MER

    Joined:
    Jun 24, 2015
    Posts:
    64
    Done, Thank you so much :)
     
    Dave-Carlile likes this.