Advertisement
Guest User

savemanager

a guest
Dec 10th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. SaveManager.cs
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5. using MoreMountains.Tools;
  6. using System;
  7. using System.Collections.Generic;
  8.  
  9. namespace MoreMountains.TopDownEngine
  10. {
  11.     /// <summary>
  12.     /// a class to save game settings (levels. eventually can incorporate inventory saving)
  13.     /// </summary>
  14.     [Serializable]
  15.     public class SaveSettings
  16.     {
  17.         public string CurrentScene;
  18.     }
  19.  
  20.     /// <summary>
  21.     /// This persistent singleton handles game saving
  22.     /// </summary>
  23.     [AddComponentMenu("TopDown Engine/Managers/Save Manager")]
  24.     public class SaveManager : PersistentSingleton<SaveManager>//, MMEventListener<TopDownEngineEvent>, MMEventListener<MMGameEvent>
  25.     {
  26.         [Header("Save Settings")]
  27.         /// the current save settings
  28.         public SaveSettings SaveSettings;
  29.  
  30.         protected const string _saveFolderName = "SaveManager/";
  31.         protected const string _saveFileName = "save.settings";
  32.  
  33.         /// <summary>
  34.         /// Saves the settings to file
  35.         /// </summary>
  36.         protected virtual void SaveGameSettings()
  37.         {
  38.             SaveLoadManager.Save(SaveSettings, _saveFileName, _saveFolderName);
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Loads the settings from file (if found)
  43.         /// </summary>
  44.         protected virtual void LoadGameSettings()
  45.         {
  46.             SaveSettings settings = (SaveSettings)SaveLoadManager.Load(_saveFileName, _saveFolderName);
  47.             if (settings != null) SaveSettings = settings;
  48.  
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Resets the settings by destroying the save file
  53.         /// </summary>
  54.         protected virtual void ResetGameSettings()
  55.         {
  56.             SaveLoadManager.DeleteSave(_saveFileName, _saveFolderName);
  57.         }
  58.  
  59.     public virtual void SetCurrentScene(string scene) { SaveSettings.CurrentScene = scene; ResetGameSettings(); SaveGameSettings(); }
  60.   public virtual void LoadSavedScene() { LoadGameSettings(); }
  61.   public virtual string LoadSceneName() { return SaveSettings.CurrentScene; }
  62.   //public virtual void GetCurrentScene(string scene) { SaveSettings.CurrentScene = scene; SaveGameSettings(); }
  63.     }
  64. }
  65.  
  66.  
  67.  
  68. SetScene.cs
  69.  
  70. using UnityEngine;
  71. using System.Collections;
  72. using MoreMountains.Tools;
  73. using UnityEngine.SceneManagement;
  74.  
  75. namespace MoreMountains.TopDownEngine
  76. {
  77.     /// <summary>
  78.     /// This will set the scene to the save file when a scene is loaded so that it can be loaded later.
  79.     /// </summary>
  80.     public class SetScene : MonoBehaviour
  81.     {
  82.         /// <summary>
  83.         /// Set the savemanager scene to the active scene name
  84.         /// </summary>
  85.         protected virtual void Start()
  86.         {
  87.             SaveManager.Instance.SetCurrentScene(SceneManager.GetActiveScene().name);
  88.         }
  89.     }
  90. }
  91.  
  92. GetScene.cs
  93.  
  94. using UnityEngine;
  95. using System.Collections;
  96. using MoreMountains.Tools;
  97. using UnityEngine.SceneManagement;
  98.  
  99. namespace MoreMountains.TopDownEngine
  100. {
  101.     /// <summary>
  102.     /// This will get the save file from disk on start and can be used with a button for GoToLevel().
  103.     /// </summary>
  104.     public class GetScene : MonoBehaviour
  105.     {
  106.       public string SceneName;
  107.  
  108.         /// <summary>
  109.         /// Loads the save file from disk, save string is not null, set the SceneName.
  110.         /// </summary>
  111.         protected virtual void Start()
  112.         {
  113.             SaveManager.Instance.LoadSavedScene();
  114.             string scenename = (string)SaveManager.Instance.LoadSceneName();
  115.             if (scenename != "") SceneName = scenename;
  116.         }
  117.  
  118.         /// <summary>
  119.             /// if the scene name is not null, go to the level
  120.             /// </summary>
  121.             public virtual void GoToLevel()
  122.             {
  123.               if (SceneName != "") LevelManager.Instance.GotoLevel(SceneName);
  124.             }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement