After several searches on the backup of the game, I could see the number of questions asked on the forums to understand the functionality or the videos. After several days of research I have more hair on my head.
I find that there is a big shortage on the Asset Store about saving a project.
I need to be helped at GameObject level.
How do I save my gameobject “Disable” that found in the Hierarchy of “Canvas” “UI”?
I would like to know how to activate the gameObject and the backup, for which the player loads the game it finds the gameObject Activate
The process is generally called serialization and deserialization. Once the desired parts of the game have been serialized (into a string), then you write it to a storage spot, perhaps with PlayerPrefs or perhaps writing it to a file.
It is a fairly non-trivial part of game engineering, but it can be approached one tiny part at a time. Try saving a single binary value and restoring it when the player reloads. From there you will get a feel for what you may ultimately be facing.
Properly managed, it can be a breeze, but the key is learning HOW to properly manage it for the context you need it in.
The noob way is to use PlayerPrefs. However they are slow, clumsy to organize and very easy for inexperienced cheaters to change. When I say “inexperienced cheaters”, I’m referring to people who’re looking for a quick text file to change and don’t know much about game hacking. More advanced cheaters will use memory hacks (such as Cheat Engine), and/or will be able to decode the serizalized file.
The better way is to use serialization. I use this script to serialize a dictionary:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System;
using System.Collections.Generic;
//this is SaveInfoNewer.cs
//this script saves and loads all the info we want
public class SaveInfoNewer : MonoBehaviour
{
public SlotLoader SlotLoader;
//data is what is finally saved
public Dictionary<string, int> data;
void Awake()
{
//LoadUpgradeData();
LoadData();
//WARNING! data.Clear() deletes EVERYTHING
//data.Clear();
//SaveData();
}
public void LoadData()
{
//this loads the data
data = SaveInfoNewer.DeserializeData<Dictionary<string, int>>("PleaseWork.save");
}
public void SaveData()
{
//this saves the data
SaveInfoNewer.SerializeData(data, "PleaseWork.save");
}
public static void SerializeData<T>(T data, string path)
{
//this is just magic to save data.
//if you're interested read up on serialization
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, data);
Debug.Log("Data written to " + path + " @ " + DateTime.Now.ToShortTimeString());
}
catch (SerializationException e)
{
Debug.LogError(e.Message);
}
finally
{
fs.Close();
}
}
public static T DeserializeData<T>(string path)
{
//this is the magic that deserializes the data so we can load it
T data = default(T);
if (File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
data = (T)formatter.Deserialize(fs);
Debug.Log("Data read from " + path);
}
catch (SerializationException e)
{
Debug.LogError(e.Message);
}
finally
{
fs.Close();
}
}
return data;
}
}
It might not be perfect for your game and it’s certainly not the most secure system, but it is fully functional and it’s a good starting point.
With this script, to save data, you’ll do this:
public SaveInfoNewer SaveInfo;
void SaveAKey()
{
SaveInfo.data["YourKeyName"] = 1234;
}
To pull data, use this:
public SaveInfoNewer SaveInfo;
void LoadAKey()
{
YourVariable = SaveInfo.data["YourKeyName"];
}
Thank you for your help! I will see what I can do with this script! If I have difficulties to start the script at the level of my gameObject I would come to appeal to you.
So can we use this to save a full scene???, i’m making a game where it only has two scenes in it, one is the menu the other is the game itself so i need to save the game scene.
Generally you don’t save game objects states directly. You save common game state like player position, health, monsters killed, items collected. And then you restore whole level based on that data.
There’s no built-in functionality for saving the scene as a whole since it is rare case. But there are few whole-scene serializers in asset store, for example C# Scene Serializer | Integration | Unity Asset Store
You’re very likely thinking about this in the wrong way if you think you need to save the entire scene. Figure out the minimum data you need to save in order to recreate that scene, and write code which both stores that information and loads that information to recreate the scene.
For example, you have an NPC monster to store. Well you don’t need to store the object structure and every variable. You likely just need to store an identifier for its prefab, its position and rotation, and a few other key bits of information. You write some code that serializes that information to whatever format you choose, and you run that code against all the NPC monsters. You take a similar approach for every other object in your scene which you need to recreate to restore the scene.
It’s confusing why you’re necroing a thread that’s 6 years old to say this. This purpose of the forum is to help people with problems, it’s not a chat room.