- Home /
Save entire Transform objects with childs
Hello,
I would like to save some Transform objects in a file (for example XML) including the state of the child objects of them (enabled/disabled).
I haven't got any idea how to realize this (so with saving the child objects and their states).
Thanks in advance for any helpful answer.
Sincerely MG
You could take a look at the Unity Save Load Utility I wrote which does what you are asking for. If you choose to write a solution yourself, be aware that this is an extremely difficult and complex task and some things are even impossible to save and load in Unity (like animation states).
Answer by MikGames · Jan 30, 2018 at 06:37 PM
Thanks for all the answers and sorry for my late response!
I fixed this using a bool List with the same index as the childs have. So the states of the objects are saved in this list and loaded and set with starting the game.
Answer by OrionGamesInc · Jan 16, 2018 at 06:07 PM
For starters, you need a save system. To create a save system you can use a basic skeleton for one here:
using System;
using System.IO;
using UnityEngine.Windows;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using UnityEngine.UI;
public static class SaveLoadManager {
public static void SavePlayer(Player player){
BinaryFormatter bf = new BinaryFormatter();
var createdir = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
String dir = Application.persistentDataPath + "/saves";
PlayerData data = new PlayerData (player);
DirectoryInfo dir_i = new DirectoryInfo(dir);
FileInfo[] info = dir_i.GetFiles("*.wyrm");
foreach (FileInfo f in info) {
Debug.Log (f);
}
FileStream stream = new FileStream (dir + "/player" + (info.Length + 1) + ".wyrm", FileMode.Create);
bf.Serialize (stream, data);
stream.Close ();
}
public static float[] LoadPlayer(){
String dir = Application.persistentDataPath + "/saves";
DirectoryInfo dir_i = new DirectoryInfo(dir);
FileInfo[] info = dir_i.GetFiles("*.save");
foreach (FileInfo f in info) {
Debug.Log (f);
}
if (info.Length >= 1) {
BinaryFormatter bf = new BinaryFormatter ();
var createdir = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
FileStream stream = new FileStream (dir + "/player" + (info.Length) + ".save", FileMode.Open);
PlayerData data = bf.Deserialize (stream) as PlayerData;
stream.Close ();
return data.stats;
} else {
return null;
}
}
}
[Serializable] //serializable means it is able to be saved
public class PlayerData {
public float[] stats;
public PlayerData(Player player){
stats = new float[5]; //create an empty array and use it to store values
stats [0] = player.Level; //you can change these into whatever you want to save, but it must be either a float or int.
stats [1] = player.Experience;
stats [2] = player.transform.position.x;
stats [3] = player.transform.position.y;
stats [4] = player.transform.position.z;
}
}
Now that you have a SaveLoadManager you must create a Save() and Load() method inside your Player script. If you want to save booleans as integers you can look up any tutorial on it, such as this: Stack Overflow Tutorial
public void Save(){ //compress the data into Binary using the SaveLoadManager
Debug.Log ("Saving data (player)");
SaveLoadManager.SavePlayer (this); //you do not need a reference to the SaveLoadManager as it is not MonoBehavior
}
public void Load(){ //load the data from the file and set the values accordingly
Debug.Log ("Loading data (player)");
float[] loadedStats = SaveLoadManager.LoadPlayer (); //grab the list of values that are returned from loading and set them into a new array
Level = (int) loadedStats [0]; //level is an int, so you can save ints as floats and load them back into ints
Experience = loadedStats [1];
transform.position = new Vector3 (loadedStats[2],loadedStats[3],loadedStats[4]); //this is how you can save your exact player position
Thanks, but I've already got a save script structure. $$anonymous$$y problem now is, to save the Transform components because they are not serializable.
You could use SerializationSurrogates for non-serializable types. For a Transform instance, you would need SSs for Vector3 and Quaternion (position, rotation and scale). SerializationSurrogates have been discussed here on UA countless times and you will even find complete examples for the types mentioned above.
Answer by anant_1910 · Jan 16, 2018 at 04:33 PM
You can drag the transform object (the parent) and place it to the Project menu
I'm not sure if you have unterstood my question correctly. So: I would like to save gameobjects in a simple file (like X$$anonymous$$L). Later I load them from the file and spawn them in the scene.
You cannot save the actual gameobjects with all the instanced data. You need to write the relevant values in the file like position, rotation, scale and custom script variables. When the game started, simply cycle through each item, representing a gameObject, in the file and try to use prefabs and instance them and initialize the values to each prefab. You can also use JSON to initialize serializable classes of the attached scripts.
Your answer
Follow this Question
Related Questions
XML Encryption 1 Answer
How to save my serialized levels ? 1 Answer
Transform Position? 1 Answer
Mapping between classes for serialization 2 Answers
Saving gameobjects' properties for Dynamically growing prefabs 1 Answer