Page 1 of 1

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

Posted: Sat Oct 26, 2019 11:19 pm
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?

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

Posted: Sun Oct 27, 2019 8:52 am
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

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

Posted: Mon Oct 28, 2019 9:30 pm
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.

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

Posted: Tue Oct 29, 2019 5:48 pm
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