Skip to content

Instantly share code, notes, and snippets.

@velzie
Created May 7, 2021 15:33
Show Gist options
  • Save velzie/f84ac3342a128dc97191a0ac07ce45da to your computer and use it in GitHub Desktop.
Save velzie/f84ac3342a128dc97191a0ac07ce45da to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SaveStatesManager : MonoBehaviour
{
public List<SavedGameObject> savedState;
public void SaveState()
{
GameObject[] allObjects =
UnityEngine.Object.FindObjectsOfType<GameObject>();
foreach (object go in allObjects)
{
SavedGameObject sGo = new SavedGameObject();
STransform sTransform =
new STransform(go.transform.position,
go.transform.localScale,
go.transform.rotation.eulerAngles);
sGo.transform = sTransform;
MonoBehaviour[] go_scripts =
go.GetComponentsInChildren<MonoBehaviour>();
foreach (MonoBehaviour mono in go_scripts)
{
switch (typeof (mono).ToString())
{
case "Player":
Player player = go.GetComponent<Player>();
PlayerScript ps =
new PlayerScript(player.health, ScriptType.PLAYER);
sGo.scripts.Add (ps);
break;
case "Enemy":
Enemy enemy = go.GetComponent<Enemy>();
EnemyScript es =
new EnemyScript(enemy.health, ScriptType.ENEMY);
sGo.scripts.Add (es);
break;
default:
Debug.Log("can't serialize that, sorry");
break;
}
}
savedState.Add (sGo);
}
}
}
class SavedGameObject
{
public STransform transform;
public List<Script> scripts;
public SavedGameObject()
{
scripts = new List<Script>();
}
}
class STransform
{
public Vector3 pos;
public Vector3 scale;
public Vector3 rotation;
public STransform(Vector3 _pos, Vector3 _scale, Vector3 _rotation)
{
pos = _pos;
scale = _scale;
rotation = _rotation;
}
}
class Script
{
public ScriptType type;
public Script(ScriptType _type)
{
type = _type;
}
}
class PlayerScript : Script
{
public float health;
public override Script(float _health)
{
health = _health;
}
}
class EnemyScript : Script
{
public float health;
public EnemyScript(float _health, ScriptType _type)
{
type = _type;
health = _health;
}
}
enum ScriptType
{
PLAYER,
ENEMY
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment