Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How can I use the save system to save multiple objects ? Do I need to give a unique id?

Discussion in 'Scripting' started by SharonL75, Oct 2, 2020.

  1. SharonL75

    SharonL75

    Joined:
    Aug 13, 2020
    Posts:
    91
    I'm using json so I'm not sure if the json serializer is enough or I should also add the :

    Code (csharp):
    1.  
    2. [System.Serializable]
    3.  
    To each object I want to save ?

    I saw in other tutorials that they give each object some unique id number. Should I do it also in this case too?

    I have in the scene two buttons Save and Load but I'm not sure how to use the script SaveLoad with the buttons OnClick events. I know how to use the OnClick events but not sure how to do it with the script.

    If for example I want to add the SaveLoad for individual object for example to a Cube and it will save only this Cube or maybe to ass a List of GameObject in the top of the SaveLoad and then to drag objects I want to save to it and make some loop to save all the objects in the List.

    This is the class SaveSystem :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using UnityEngine;
    6.  
    7. public static class SaveSystem
    8. {
    9.    private static readonly string SAVE_FOLDER = Application.dataPath + "/save.txt";
    10.    public static void Init()
    11.    {
    12.        if (!Directory.Exists(SAVE_FOLDER))
    13.        {
    14.            Directory.CreateDirectory(SAVE_FOLDER);
    15.        }
    16.    }
    17.  
    18.    public static void Save(string saveString)
    19.    {
    20.        int saveNumber = 1;
    21.  
    22.        while (File.Exists(SAVE_FOLDER + "save_" + saveNumber + ".txt"))
    23.        {
    24.            saveNumber++;
    25.        }
    26.  
    27.        File.WriteAllText(SAVE_FOLDER + "save_" + saveNumber + ".txt", saveString);
    28.    }
    29.  
    30.    public static string Load()
    31.    {
    32.        DirectoryInfo directoryInfo = new DirectoryInfo(SAVE_FOLDER);
    33.        FileInfo[] saveFiles = directoryInfo.GetFiles();
    34.        FileInfo mostRecentFile = null;
    35.        foreach (FileInfo fileInfo in saveFiles)
    36.        {
    37.            if (mostRecentFile == null)
    38.            {
    39.                mostRecentFile = fileInfo;
    40.            }
    41.            else
    42.            {
    43.                if (fileInfo.LastWriteTime > mostRecentFile.LastWriteTime)
    44.                {
    45.                    mostRecentFile = fileInfo;
    46.                }
    47.            }
    48.        }
    49.  
    50.        if (mostRecentFile != null)
    51.        {
    52.            string saveString = File.ReadAllText(SAVE_FOLDER + mostRecentFile.FullName);
    53.            return saveString;
    54.        }
    55.        else
    56.        {
    57.            return null;
    58.        }
    59.    }
    60. }
    61.  
    And the SaveLoad script :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using UnityEngine;
    6.  
    7. public class SaveLoad : MonoBehaviour
    8. {
    9.    private void Awake()
    10.    {
    11.        SaveSystem.Init();
    12.    }
    13.  
    14.    public void Save()
    15.    {
    16.        SaveObject saveObject = new SaveObject
    17.        {
    18.            position = transform.position,
    19.            scaling = transform.localScale,
    20.            rotation = transform.rotation
    21.        };
    22.  
    23.        string json = JsonUtility.ToJson(saveObject);
    24.        SaveSystem.Save(json);
    25.      
    26.    }
    27.  
    28.    public void Load()
    29.    {
    30.        string saveString = SaveSystem.Load();
    31.  
    32.        if(saveString != null)
    33.        {
    34.            SaveObject saveObject = JsonUtility.FromJson<SaveObject>(saveString);
    35.  
    36.            transform.position = saveObject.position;
    37.            transform.localScale = saveObject.scaling;
    38.            transform.rotation = saveObject.rotation;
    39.        }
    40.    }
    41.  
    42.    public class SaveObject
    43.    {
    44.        public Vector3 position;
    45.        public Vector3 scaling;
    46.        public Quaternion rotation;
    47.    }
    48. }
    49.  
    If for example I attach the SaveLoad script to a Cube and then using the Save and Load methods with the buttons OnClick events but then what if I want to save another Cube if I will add to it the SaveLoad script too how then I will call the Save and Load methods with the buttons OnClick events from the second Cube ? I need something global that will save and load with the buttons and a script that will be attach to each object I want to save to get the info.

    This is the scene screenshot :



    What I want to do is to be ale to Save and Load each individual object info with attaching script to it. And also to have the option to drag any object I want to a empty gameobject with the SaveLoad script and it will loop over a List and save all the objects in the List.


    This is how I tried to do it with array to save multiple objects at once :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using UnityEngine;
    6.  
    7. public class SaveLoad : MonoBehaviour
    8. {
    9.    public Transform[] objToSave;
    10.  
    11.    private void Awake()
    12.    {
    13.        SaveSystem.Init();
    14.    }
    15.  
    16.    public void Save()
    17.    {
    18.        SaveObject[] saveObject = new SaveObject[objToSave.Length];
    19.  
    20.        for (int i = 0; i < objToSave.Length; i++)
    21.        {
    22.            saveObject[i] = new SaveObject();
    23.            saveObject[i].position = objToSave[i].position;
    24.            saveObject[i].scaling = objToSave[i].localScale;
    25.            saveObject[i].rotation = objToSave[i].rotation;
    26.        }
    27.  
    28.  
    29.        string json = JsonUtility.ToJson(saveObject);
    30.        SaveSystem.Save(json);
    31.  
    32.    }
    33.  
    34.    public void Load()
    35.    {
    36.        string saveString = SaveSystem.Load();
    37.  
    38.        if (saveString != null)
    39.        {
    40.            SaveObject[] saveObject = JsonUtility.FromJson<SaveObject[]>(saveString);
    41.            saveObject = new SaveObject[objToSave.Length];
    42.  
    43.            for (int i = 0; i < objToSave.Length; i++)
    44.            {
    45.                saveObject[i] = new SaveObject();
    46.                objToSave[i].position = saveObject[i].position;
    47.                objToSave[i].localScale = saveObject[i].scaling;
    48.                objToSave[i].rotation = saveObject[i].rotation;
    49.            }
    50.        }
    51.    }
    52.  
    53.    public class SaveObject
    54.    {
    55.        public Vector3 position;
    56.        public Vector3 scaling;
    57.        public Quaternion rotation;
    58.    }
    59. }
    60.  
    But when pressing the Load button it's not loading the cubes there are two cubes in the variable objToSave to the saved position and rotation. It put them somewhere far out of the scene.
     
    Last edited: Oct 2, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520