Save for Unity Demonstration Project




The Save scene is made up of the following objects :-

World

The World object contains the camera and lighting information. As this object is also duplicated in the Load scene it is tagged NoSave so that it is ignored by the save system

WorldObjects

This object consists of a simple game space and a number of Spheres. The Spheres are driven by Rigidbody physics and in addition have a Monobehaviour script attached to allow the assigning of invividual colours.

SaverObject

This is an empty game object with only the Saver script attached

UI

The UI consists of a static description text, 3 buttons and a timer. The OnClick events on the buttons are attached to the Saver script













using SSSoftware.Save;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Saver : MonoBehaviour
{
	[SerializeField] private string fileName = "mySave"; // The name of the save file

	void Update()
	{
		if (Input.GetKeyUp(KeyCode.Escape))
		{
			Application.Quit();
		}
	}

	// Attached to the Save Button OnClick event
	public void OnSaveClick()
	{
		Scene scene = SceneManager.GetActiveScene();

		//Save.Trace = true;            // Enable this for debugging
		Save save = new Save(fileName); // Create the save file
		save.Scene(scene);              // Save the contents of the scene to the save file

		Debug.Log("Scene Saved");
	}


	// Attached to the Load Button OnClick event
	public void OnLoadClick()
	{
		SceneManager.LoadScene(1);	// Load the Load Scene
	}

 
	// Attached to the Restart Button OnClick event
	public void OnRestartClick()
	{
		SceneManager.LoadScene(0);	// Load the Save Scene
	}
}






The Load scene is made up of the following objects :-

World

This is a duplicate of the World object from the Save scene

Loader

This is an empty game object with only the Loader script attached. It is also tagged as NoSave so that it is ignored by the Save system







using SSSoftware.Save;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Loader : MonoBehaviour
{
	[SerializeField] private string fileName = "mySave"; // The name of the Load file


	// Automatically run when the Load scene is loaded
	private void Start()
	{
		Scene scene = SceneManager.GetActiveScene();

		//Load.Trace = true;                // Enable this for debugging
		Load load = new Load(fileName);     // Open the Load file
		load.Scene(scene);                  // Load the contents of the Load file into the scene
		load.Close();                       // Close the load file

		Debug.Log("Scene Loaded");
	}
}
			






When the Load button is pressed the Loader script reads the contents of the latest save file and recreates all of the gameobjects and their components into the Load scene.