What is the best way to Save&Load GameObjects in hierarchy

Discussion and help for Easy Save 3
Post Reply
88888888
Posts: 20
Joined: Tue Sep 10, 2019 10:41 pm

What is the best way to Save&Load GameObjects in hierarchy

Post by 88888888 »

Hi. Let say I have 20 missions inside my game and each mission stored inside a game object inside my game hierarchy. So what is the best way to disable and enable those game objects when I load my game?

User avatar
Joel
Moodkie Staff
Posts: 2829
Joined: Wed Nov 07, 2012 10:32 pm

Re: What is the best way to Save&Load GameObjects in hierarc

Post by Joel »

Hi there,

The following script will do this. You simply need to drag the GameObjects you want to save the active states of into the gameObjects array of the script. This assumes that there is an Easy Save 3 Manager in your scene, and the GameObjects you are saving are in the reference manager.

This will save whenever the application is paused or the scene is changed, or whenever you manually call the Save() method. It will load when the script awakes.
using System.Collections.Generic;
using UnityEngine;

public class SaveActiveStates : MonoBehaviour 
{
	public GameObject[] gameObjects;
	public string guid = System.Guid.NewGuid().ToString();

	void Awake() 
	{
		if (ES3.KeyExists (guid))
		{
			var activeStates = ES3.Load <Dictionary<long, bool>>(guid);
			foreach (var kvp in activeStates) 
			{
				var go = (GameObject)ES3ReferenceMgr.Current.Get(kvp.Key);
				if (go != null)
					go.SetActive (kvp.Value);
			}
		}
	}

	void OnDestroy()
	{
		Save();
	}

	void OnApplicationPause(bool paused)
	{
		if (paused)
			Save();
	}

	public void Save()
	{
		var activeStates = new Dictionary<long, bool>();
		foreach (var go in gameObjects)
			activeStates.Add (ES3ReferenceMgr.Current.Get(go), go.activeSelf);
		ES3.Save<Dictionary<long, bool>>(guid, activeStates);
	}
}
All the best,
Joel
Joel @ Moodkie Interactive
Twitter - Unity

88888888
Posts: 20
Joined: Tue Sep 10, 2019 10:41 pm

Re: What is the best way to Save&Load GameObjects in hierarc

Post by 88888888 »

Joel wrote:Hi there,

The following script will do this. You simply need to drag the GameObjects you want to save the active states of into the gameObjects array of the script. This assumes that there is an Easy Save 3 Manager in your scene, and the GameObjects you are saving are in the reference manager.

This will save whenever the application is paused or the scene is changed, or whenever you manually call the Save() method. It will load when the script awakes.
using System.Collections.Generic;
using UnityEngine;

public class SaveActiveStates : MonoBehaviour 
{
	public GameObject[] gameObjects;
	public string guid = System.Guid.NewGuid().ToString();

	void Awake() 
	{
		if (ES3.KeyExists (guid))
		{
			var activeStates = ES3.Load <Dictionary<long, bool>>(guid);
			foreach (var kvp in activeStates) 
			{
				var go = (GameObject)ES3ReferenceMgr.Current.Get(kvp.Key);
				if (go != null)
					go.SetActive (kvp.Value);
			}
		}
	}

	void OnDestroy()
	{
		Save();
	}

	void OnApplicationPause(bool paused)
	{
		if (paused)
			Save();
	}

	public void Save()
	{
		var activeStates = new Dictionary<long, bool>();
		foreach (var go in gameObjects)
			activeStates.Add (ES3ReferenceMgr.Current.Get(go), go.activeSelf);
		ES3.Save<Dictionary<long, bool>>(guid, activeStates);
	}
}
All the best,
Joel
Is there PlayMaker action for this? I'm using ES3 along PM.

User avatar
Joel
Moodkie Staff
Posts: 2829
Joined: Wed Nov 07, 2012 10:32 pm

Re: What is the best way to Save&Load GameObjects in hierarc

Post by Joel »

Hi there,

I've created a version below which has a Save and Load method (so this will not automatically save or load the data). You can use PlayMaker's Call Method action to call the Save and Load methods on the script when required.
using System.Collections.Generic;
using UnityEngine;
 
public class SaveActiveStates : MonoBehaviour 
{
  public GameObject[] gameObjects;
    public string guid = System.Guid.NewGuid().ToString();
 
  public void Load() 
   {
       if (ES3.KeyExists (guid))
       {
           var activeStates = ES3.Load <Dictionary<long, bool>>(guid);
         foreach (var kvp in activeStates) 
          {
               var go = (GameObject)ES3ReferenceMgr.Current.Get(kvp.Key);
              if (go != null)
                 go.SetActive (kvp.Value);
           }
       }
   }

   public void Save()
  {
       var activeStates = new Dictionary<long, bool>();
      foreach (var go in gameObjects)
         activeStates.Add (ES3ReferenceMgr.Current.Get(go), go.activeSelf);
      ES3.Save<Dictionary<long, bool>>(guid, activeStates);
   }
}
All the best,
Joel
Joel @ Moodkie Interactive
Twitter - Unity

Post Reply