- Home /
Save GameObject to file without Playerpref
Hi! So I am trying to save everything in my level and with the state they were. EX: If a door was broken, it stays broken. Door was unlocked, it stays unlocked. etc.
.
But All I can find currently find online are lacking a lot of informations and it is really frustrating.
.
I have done multiple test with just the player and I can't understand why it doesn't work and what is missing to make it work..
. I would like to be able to save everything on my player : All its components, all its children and all the childrens' children (including all their component). In a way, I would sort of think of having the similarity of a prefab of the player for lets say the "save A" or do what unity does when loading a level and remembering exactly where everything was and what were their components etc.
.
Here is a sample of what I tried so far :
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public static class Load
{
//Path of the saves
static string SavesPath;
public static GameObject LoadPlayer()
{
SavesPath = GameManager.Instance.GetSavesPath;
if(File.Exists(SavesPath + "/save.json"))
{
try
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(SavesPath + "/save.json", FileMode.Open);
//Get the data
string data = (string)bf.Deserialize(file);
//Close the file
file.Close();
//Convert the Data into the an Object
SerializedGameObject player = JsonUtility.FromJson<SerializedGameObject>(data);
Debug.Log(player.gameObject);
//Return the gameObject
return player.gameObject;
}catch(System.Exception e)
{
Debug.Log(e);
}
}
return null;
}
}
public static class Save
{
//Path of the save
static string SavesPath;
/// <summary>
/// Save instance of the player
/// </summary>
public static void Player()
{
//Get the save path from the GameManager
SavesPath = GameManager.Instance.GetSavesPath;
//Look if the save folder exist
CheckSaveFolder();
//Look if the save file exists
CheckSaveFile();
try
{
//Get the gameobject of the player
SerializedGameObject player = new SerializedGameObject();
player.gameObject = PlayerManager.Instance.gameObject;
//Stringify the GameObject
string data = JsonUtility.ToJson(player);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(SavesPath + "/save.json", FileMode.Open);
bf.Serialize(file,data);
file.Close();
Debug.Log("Player saved successfully!");
}catch(System.Exception e)
{
Debug.Log(e);
}
}
}
using UnityEngine;
/// <summary>
/// Allow to serialization of a GameObject
/// </summary>
[System.Serializable]
public class SerializedGameObject
{
public GameObject gameObject;
}
Your answer
Follow this Question
Related Questions
Save GameObject to file without Playerpref 2 Answers
How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers
How do I get an object to Instantiate infront of a specific object/player? 1 Answer
Why doesent my code get executed? 1 Answer
How can i clear specific printed text to console window from code ? 0 Answers