Saving and Loading Prefabs Instantiated at Runtime

Examples using Easy Save's API code

Saving and Loading Prefabs Instantiated at Runtime

Postby Joel » Wed Jun 20, 2018 5:32 pm

This is a basic example of saving and loading prefabs which are instantiated at runtime, using a single script: ES3PrefabManager.cs. The script is fully-commented, so it is recommended that you read the script to understand how this example works.

The script loads any saved prefabs when the scene is loaded, and saves instantiated prefabs when the application is quit or paused. Any prefabs which you wish to save at runtime should be instantiated using the script's InstantiatePrefab() method, which replaces Unity's Instantiate() method.

Note that separate ES3PrefabManager is required for each type of prefab you wish to save.

Note:
  • Easy Save should be installed in your project before importing this example. The example is located in Assets/Easy Save 3/Examples/.
  • All examples are only provided as an educational sample, and may need modification for your purposes. They are provided without warranty.

using UnityEngine;
using System.Collections.Generic;

/*
*
* This class will manage the creation of prefabs, including loading and saving them.
* It will also store a list of all of the prefabs we've created.
*
*/
public class ES3PrefabManager : MonoBehaviour
{
// The prefab we want to create.
public GameObject prefab;
// An automatically-generated unique identifier for this type of prefab.
// We will append this to our keys when saving to identifiy different types
// of prefab in the save file.
public string id = System.Guid.NewGuid().ToString();

// A List which we'll add the Transforms of our created prefabs to.
private List<Transform> prefabInstances = new List<Transform>();

/*
* This is called whenever the application is quit.
* This is where we will save our data.
*/
void OnApplicationQuit()
{
// Save the number of created prefabs there are.
ES3.Save<int>(id+"count", prefabInstances.Count);
// Save our Transforms.
ES3.Save<List<Transform>>(id, prefabInstances);
}

/* We also call OnApplicationPause, which is called when an app goes into the background. */
void OnApplicationPause(bool isPaused)
{
if(isPaused)
OnApplicationQuit();
}

/*
* This is called when the scene first loads.
* This is where we load our prefabs, if there are prefabs to load.
*/
void Start()
{
int count = ES3.Load<int>(id + "count", 0);
// If there are prefabs to load, load them.
if(count > 0)
{
// For each prefab we want to load, instantiate a prefab.
for(int i = 0; i < count; i++)
InstantiatePrefab();
// Load our List of Transforms into our prefab array.
ES3.LoadInto<List<Transform>>(id, prefabInstances);
}
}

/*
* Creates an instance of the prefab and adds it to the instance list.
*/
public GameObject InstantiatePrefab()
{
var go = Instantiate(prefab);
prefabInstances.Add(go.transform);
return go;
}

/*
* Instantiates the prefab at a random position and with a random rotation.
*/
public void CreateRandomPrefab()
{
var go = InstantiatePrefab();
go.transform.position = Random.insideUnitSphere * 5;
go.transform.rotation = Random.rotation;
}

/*
* Deletes all prefab instances from the scene and from the save file.
*/
public void DeletePrefabs()
{
// Delete the keys relating to this prefab.
ES3.DeleteKey(id);
ES3.DeleteKey(id+"count");
// Destroy each created prefab, and then clear the List.
for(int i=0; i<prefabInstances.Count; i++)
Destroy (prefabInstances[i].gameObject);
prefabInstances.Clear();
}
}
Attachments
Save and Load Prefabs Instantiated at Runtime.unitypackage
(49.61 KiB) Downloaded 369 times
Joel Tebbett @ Moodkie Interactive
Twitter - Unity - LinkedIn
User avatar
Joel
Moodkie Staff
 
Posts: 2476
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Prefabs Instantiated at Runtime

Postby cocche » Wed Apr 03, 2019 12:44 pm

Sorry if it's a stupid question but when I run this test and try to add a test variable to the sphere prefab, and then exit the scene and load it back the sphere loads correctly but the test variable doesn't. How can I load the values of the variable with the prefabs ?

To be more specific: in my case I added a simple "public int test" to the sphere prefab, changed the value during runtime, stopped the Scene, and restarted the scene. I know that I have to specify the variables that I want to be saved but I don't understand where and how should I do it.
cocche
 
Posts: 4
Joined: Wed Apr 03, 2019 12:07 pm

Re: Saving and Loading Prefabs Instantiated at Runtime

Postby Joel » Wed Apr 03, 2019 6:40 pm

cocche wrote:Sorry if it's a stupid question but when I run this test and try to add a test variable to the sphere prefab, and then exit the scene and load it back the sphere loads correctly but the test variable doesn't. How can I load the values of the variable with the prefabs ?

To be more specific: in my case I added a simple "public int test" to the sphere prefab, changed the value during runtime, stopped the Scene, and restarted the scene. I know that I have to specify the variables that I want to be saved but I don't understand where and how should I do it.

Hi there,

You do this by finding your type in the Window > Easy Save 3 > Types pane, and selecting the variable from there. More info can be found here: https://docs.moodkie.com/easy-save-3/es ... -es3types/

All the best,
Joel
Joel Tebbett @ Moodkie Interactive
Twitter - Unity - LinkedIn
User avatar
Joel
Moodkie Staff
 
Posts: 2476
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Prefabs Instantiated at Runtime

Postby Jorge » Wed Aug 28, 2019 3:04 pm

Thanks for the example. I have a question. Let's say I have instantiated 10 prefabs and I want to delete just 1? The delete function you posted, deletes I think all prefabs right?
Jorge
 
Posts: 2
Joined: Wed Aug 28, 2019 3:01 pm

Re: Saving and Loading Prefabs Instantiated at Runtime

Postby Joel » Wed Aug 28, 2019 3:08 pm

Hi there,

The easiest way to delete a single instance would simply be to destroy that instance in the game. Then when you re-save the game, this instance will no longer be in the data.

All the best,
Joel
Joel Tebbett @ Moodkie Interactive
Twitter - Unity - LinkedIn
User avatar
Joel
Moodkie Staff
 
Posts: 2476
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving and Loading Prefabs Instantiated at Runtime

Postby Jorge » Wed Aug 28, 2019 3:13 pm

hmm that is not working. I think somewhere I have to remove from the list and then save
edit: got it. Added this function to a UI button click and it works. After removing the actual object transform from the list, I can then save.
Code: Select all
public void DeletePrefab(GameObject objToRemove)
    {
        prefabInstances.Remove(objToRemove.transform);
    }
Jorge
 
Posts: 2
Joined: Wed Aug 28, 2019 3:01 pm


Return to Code Examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron