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

[Solved] How to save-load multiple users with binary formatter?

Discussion in 'Scripting' started by tranos, Apr 23, 2015.

  1. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    Hello.I am currently saving and loading my game with binary formatter . Is there a way to save multiple users and when loading,display the saved users and choose what to load?

    [Edit] I have eventually found a way to save and load multiple users in different files.
    This is my updated code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.IO;
    6.  
    7. public string playerName;
    8. public string slot1Name="";
    9. public string slot2Name="";
    10. public string slot3Name="";
    11. public string slotToSaveLoad="";
    12.  
    13.  
    14.  
    15. public void SaveSlots()
    16.     {
    17.  
    18.  
    19.         BinaryFormatter bf = new BinaryFormatter();
    20.         FileStream file = File.Create(Application.persistentDataPath + "SavedSlots.data");
    21.      
    22.         SavedSlots slots = new SavedSlots ();
    23.         slots.slot1Name = slot1Name;
    24.         slots.slot2Name = slot2Name;
    25.         slots.slot3Name = slot3Name;
    26.  
    27.         bf.Serialize (file, slots);
    28.         file.Close ();
    29.     }
    30.  
    31.     void LoadSlots()
    32.     {
    33.         if (File.Exists(Application.persistentDataPath + "SavedSlots.data"))
    34.         {
    35.             BinaryFormatter bf = new BinaryFormatter();
    36.             FileStream file = File.Open(Application.persistentDataPath + "SavedSlots.data", FileMode.Open);
    37.             SavedSlots slots = (SavedSlots)bf.Deserialize(file);
    38.             file.Close();
    39.  
    40.             slot1Name = slots.slot1Name;
    41.             slot2Name = slots.slot2Name;
    42.             slot3Name = slots.slot3Name;
    43.         }
    44.     }
    45.  
    46. public void Save ()
    47.     {
    48.         BinaryFormatter bf = new BinaryFormatter();
    49.         FileStream file = File.Create(Application.persistentDataPath + slotToSaveLoad + ".data");
    50.  
    51.         PlayerData data = new PlayerData ();
    52.  
    53.         bf.Serialize (file, data);
    54.         file.Close ();
    55.     }
    56.  
    57. public void Load()
    58.     {
    59.         if (File.Exists(Application.persistentDataPath + slotToSaveLoad + ".data"))
    60.         {
    61.             BinaryFormatter bf = new BinaryFormatter();
    62.             FileStream file = File.Open(Application.persistentDataPath + slotToSaveLoad+".data", FileMode.Open);
    63.  
    64.             PlayerData data = (PlayerData)bf.Deserialize(file);
    65.             file.Close();
    66.         }
    67.     }
    68.  
    69. [Serializable]
    70. public class SavedSlots
    71. {
    72.     public string slot1Name;
    73.     public string slot2Name;
    74.     public string slot3Name;
    75. }
     
    Last edited: Apr 29, 2015
    dimaandal13 likes this.
  2. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    I eventually solved it myself.
     
    dimaandal13 likes this.
  3. dimaandal13

    dimaandal13

    Joined:
    Nov 13, 2017
    Posts:
    2
    sir how do you make a multiple user? can you show me your code?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    This is a really old post. If you have a question about something, you might want to try posting your question in a thread describing what you want, what you've done (including any relevant code). :)
     
  5. dimaandal13

    dimaandal13

    Joined:
    Nov 13, 2017
    Posts:
    2
    i would like to create a save and load data after making an account
     
  6. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    This is too vague and belongs in your own thread.
    Within your NEW THREAD. Answer these questions.
    What are you trying to save?
    Do you have any existing code you have tried?
    Do you want a human readable format like XML or JSON or would you prefer Binary or similar?