Last active
September 6, 2017 08:11
-
-
Save hasanbayatme/a27a1b60b6ff1d6b2f1d24aee5f20392 to your computer and use it in GitHub Desktop.
Learn how to save the runtime instantiated game objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using SaveGamePro; | |
public class ObjectSpawner : MonoBehaviour { | |
public GameObject prefab; | |
public Transform spawnPoint; | |
public List<Transform> spawnedGameObjects; | |
void Awake () { | |
Load (); | |
} | |
void OnApplicationQuit () { | |
Save (); | |
} | |
public void Spawn () { | |
GameObject go = Instantiate<GameObject> (prefab, spawnPoint.transform.position, Quaternion.identity); | |
spawnedGameObjects.Add (go.transform); | |
} | |
public void Save () { | |
SaveGame.Save<Transform> (spawnedGameObjects, "spawnedGameObjects"); | |
} | |
public void Load () { | |
if (SaveGame.Exists ("spawnedGameObjects")) { | |
spawnedGameObjects = SaveGame.LoadList<Transform> ("spawnedGameObjects"); | |
} else { | |
spawnedGameObjects = new List<Transform> (); | |
} | |
foreach (Transform tr in spawnedGameObjects) { | |
Instantiate<GameObject> (prefab, tr.position, tr.rotation); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment