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

Least Resource Consuming Method of Saving Values

Discussion in 'Scripting' started by kenaochreous, Apr 7, 2015.

  1. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    I need to save values for my game and I want to use the least resource consuming method possible. Would that be using playerprefs or are there other methods?
     
  2. MarkFowler

    MarkFowler

    Joined:
    Nov 29, 2012
    Posts:
    31
    I use a .bin file. Seems pretty fast if you don't have that much to save. Of course, you need to use C# scripting.

    An example from my game Ardalon:
    using System.IO;
    public void SaveGameState()
    {
    using (BinaryWriter MyWriter = new BinaryWriter(File.Open("Save.bin", FileMode.Create)))
    {
    MyWriter.Write(score);
    MyWriter.Write(highScore);
    MyWriter.Write(level);
    MyWriter.Write(wave);
    MyWriter.Write(lives);
    MyWriter.Write(baseHealth);
    MyWriter.Write(maxHealth);
    MyWriter.Write(power);
    MyWriter.Write(maxPower);
    MyWriter.Write((double)powerRegen);
    }

    UpdateText.text = "Game Saved";
    updateTextTimer = UPDATE_TIMER;
    }
     
  3. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Note, I don't think this will work for WebGL builds and will need some tweaking to work on iOS and Android (to locate a writeable location on the device).

    If you can get away with PlayerPrefs I think its a more portable and consistent approach.
     
  4. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    After looking over PlayerPrefs more I need to be able to store bools, arrays and lists. Which PlayerPrefs doesn't support.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  6. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    Thank you Eric.