using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine; using BayatGames.SaveGameFree.Encoders; using BayatGames.SaveGameFree.Serializers; using BayatGames.SaveGameFree.Types; namespace BayatGames.SaveGameFree { /// /// Save Game Auto. /// Make your game objects save their position, rotaiton and scale automatically. /// [AddComponentMenu ( "Save Game Free/Auto Save" )] public class SaveGameAuto : MonoBehaviour { /// /// Save format. /// public enum SaveFormat { /// /// The XML. /// XML, /// /// The JSON. /// JSON, /// /// The Ninary. /// Binary } [Header ( "Settings" )] [Space] [Tooltip ( "You must specify a value for this to be able to save it." )] /// /// The position identifier. /// public string positionIdentifier = "enter the position identifier"; [Tooltip ( "You must specify a value for this to be able to save it." )] /// /// The rotation identifier. /// public string rotationIdentifier = "enter the rotation identifier"; [Tooltip ( "You must specify a value for this to be able to save it." )] /// /// The scale identifier. /// public string scaleIdentifier = "enter the scale identifier"; [Tooltip ( "Encode the data?" )] /// /// The encode. /// public bool encode = false; [Tooltip ( "If you leave it blank this will reset to it's default value." )] /// /// The encode password. /// public string encodePassword = ""; [Tooltip ( "Which serialization format?" )] public SaveFormat format = SaveFormat.JSON; [Tooltip ( "If you leave it blank this will reset to it's default value." )] /// /// The serializer. /// public ISaveGameSerializer serializer; [Tooltip ( "If you leave it blank this will reset to it's default value." )] /// /// The encoder. /// public ISaveGameEncoder encoder; [Tooltip ( "If you leave it blank this will reset to it's default value." )] /// /// The encoding. /// public Encoding encoding; [Tooltip ( "Where to save? (PersistentDataPath highly recommended)." )] /// /// The save path. /// public SaveGamePath savePath = SaveGamePath.PersistentDataPath; [Tooltip ( "Reset the empty fields to their default value." )] /// /// The reset blanks. /// public bool resetBlanks = true; [Header ( "What to Save?" )] [Space] [Tooltip ( "Save Position?" )] /// /// The save position. /// public bool savePosition = true; [Tooltip ( "Save Rotation?" )] /// /// The save rotation. /// public bool saveRotation = true; [Tooltip ( "Save Scale?" )] /// /// The save scale. /// public bool saveScale = true; [Header ( "Defaults" )] [Space] [Tooltip ( "Default Position Value" )] /// /// The default position. /// public Vector3 defaultPosition = Vector3.zero; [Tooltip ( "Default Rotation Value" )] /// /// The default rotation. /// public Vector3 defaultRotation = Quaternion.identity.eulerAngles; [Tooltip ( "Default Scale Value" )] /// /// The default scale. /// public Vector3 defaultScale = Vector3.one; [Header ( "Save Events" )] [Space] [Tooltip ( "Save on Awake()" )] /// /// The save on awake. /// public bool saveOnAwake; [Tooltip ( "Save on Start()" )] /// /// The save on start. /// public bool saveOnStart; [Tooltip ( "Save on OnEnable()" )] /// /// The save on enable. /// public bool saveOnEnable; [Tooltip ( "Save on OnDisable()" )] /// /// The save on disable. /// public bool saveOnDisable = true; [Tooltip ( "Save on OnApplicationQuit()" )] /// /// The save on application quit. /// public bool saveOnApplicationQuit = true; [Tooltip ( "Save on OnApplicationPause()" )] /// /// The save on application pause. /// public bool saveOnApplicationPause; [Header ( "Load Events" )] [Space] [Tooltip ( "Load on Awake()" )] /// /// The load on awake. /// public bool loadOnAwake; [Tooltip ( "Load on Start()" )] /// /// The load on start. /// public bool loadOnStart = true; [Tooltip ( "Load on OnEnable()" )] /// /// The load on enable. /// public bool loadOnEnable = false; protected virtual void Awake () { if ( resetBlanks ) { if ( string.IsNullOrEmpty ( encodePassword ) ) { encodePassword = SaveGame.EncodePassword; } if ( serializer == null ) { serializer = SaveGame.Serializer; } if ( encoder == null ) { encoder = SaveGame.Encoder; } if ( encoding == null ) { encoding = SaveGame.DefaultEncoding; } } switch ( format ) { case SaveFormat.Binary: serializer = new SaveGameBinarySerializer (); break; case SaveFormat.JSON: serializer = new SaveGameJsonSerializer (); break; case SaveFormat.XML: serializer = new SaveGameXmlSerializer (); break; } if ( loadOnAwake ) { Load (); } if ( saveOnAwake ) { Save (); } } protected virtual void Start () { if ( loadOnStart ) { Load (); } if ( saveOnStart ) { Save (); } } protected virtual void OnEnable () { if ( loadOnEnable ) { Load (); } if ( saveOnEnable ) { Save (); } } protected virtual void OnDisable () { if ( saveOnDisable ) { Save (); } } protected virtual void OnApplicationQuit () { if ( saveOnApplicationQuit ) { Save (); } } protected virtual void OnApplicationPause () { if ( saveOnApplicationPause ) { Save (); } } /// /// Save this instance. /// public virtual void Save () { if ( savePosition ) { SaveGame.Save ( positionIdentifier, transform.position, encode, encodePassword, serializer, encoder, encoding, savePath ); } if ( saveRotation ) { SaveGame.Save ( rotationIdentifier, transform.rotation, encode, encodePassword, serializer, encoder, encoding, savePath ); } if ( saveScale ) { SaveGame.Save ( scaleIdentifier, transform.localScale, encode, encodePassword, serializer, encoder, encoding, savePath ); } } /// /// Load this instance. /// public virtual void Load () { if ( savePosition ) { transform.position = SaveGame.Load ( positionIdentifier, defaultPosition, encode, encodePassword, serializer, encoder, encoding, savePath ); } if ( saveRotation ) { transform.rotation = SaveGame.Load ( rotationIdentifier, Quaternion.Euler ( defaultRotation ), encode, encodePassword, serializer, encoder, encoding, savePath ); } if ( saveScale ) { transform.localScale = SaveGame.Load ( scaleIdentifier, defaultScale, encode, encodePassword, serializer, encoder, encoding, savePath ); } } } }