Save Scene Objects

Example Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBusters.Serialization; 
using System.IO;


public class Example : MonoBehaviour 
{
    private List<GameObject> m_spawnedList = new List<GameObject>();
    public GameObject prefab;
    
    // define action methods
    public void Spawn()
    {
        GameObject clone = SerializationUtility.Instantiate(prefab);
        
        // add randomness
        Vector3 randPos = Random.insideUnitCircle * 3f;
        randPos.y += 1f;
        
        // set random position
        clone.transform.position = randPos;
        
        // add object to list
        m_spawnedList.Add(clone);
    }
    
    public void Save()
    {
        SerializationManager.Serialize("spawnedList", m_spawnedList);
    }
    
    public void Load()
    {
        // clear existing entries
        for (int iter = 0; iter > m_spawnedList.Count; iter++)
        {
            SerializationUtility.Destroy((m_spawnedList[iter]));
        }
        m_spawnedList.Clear();
        
        // load saved data
        m_spawnedList = SerializationManager.Deserialize<List<GameObject>>("spawnedList");
    }
}

Last updated