当前提交
353c2f1b
共有 5 个文件被更改,包括 142 次插入 和 0 次删除
-
9UOP1_Project/Assets/Scripts/SaveSystem/Save.cs
-
11UOP1_Project/Assets/Scripts/SaveSystem/Save.cs.meta
-
111UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs
-
11UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs.meta
|
|||
/// <summary>
|
|||
/// <b>DO NOT DELETE THIS FILE !!!!!!</b><br/>
|
|||
/// This class contains all the variables that will be serialized and saved to a binary file.<br/>
|
|||
/// Can be considered as a save file structure or format.
|
|||
/// </summary>
|
|||
[System.Serializable] |
|||
public class Save { |
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 1d061ec6167cb064f8ec131f11729898 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine; |
|||
using UnityEditor; |
|||
using System.IO; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.Serialization.Formatters.Binary; |
|||
|
|||
|
|||
public interface ISaveable |
|||
{ |
|||
|
|||
/// <summary>
|
|||
/// <para>The function which needs to be subscribed to the <see cref="SaveSystem.AddToRegistryCallback"/></para>
|
|||
/// Include it in a place which only gets executed once to avoid data duplication.<br/>
|
|||
/// Like in OnEnable() function of Monobehaviour or ScriptableObject
|
|||
/// </summary>
|
|||
void AddToSaveRegistry(HashSet<ISaveable> registry); |
|||
|
|||
/// <summary>
|
|||
/// Pure virtual function for saving data to a save file.<br/>
|
|||
/// This will comprise the serialization logic.
|
|||
/// </summary>
|
|||
void Serialize(Save saveFile); |
|||
|
|||
/// <summary>
|
|||
/// Pure virtual function for loading data from a save file.<br/>
|
|||
/// This will comprise the deserialziation logic.
|
|||
/// </summary>
|
|||
void Deserialize(Save saveFile); |
|||
} |
|||
|
|||
public class SaveSystem : MonoBehaviour |
|||
{ |
|||
public delegate void AddToRegistryCallback(HashSet<ISaveable> registry); |
|||
public static AddToRegistryCallback AddToRegistry; |
|||
|
|||
HashSet<ISaveable> _saveRegistry = default; |
|||
|
|||
[SerializeField] |
|||
string _fileNameExtension; |
|||
|
|||
private void Start() |
|||
{ |
|||
Debug.Log("SaveSystem start getting called"); |
|||
AddToRegistry?.Invoke(_saveRegistry); |
|||
LoadGame(); |
|||
} |
|||
|
|||
private void LoadGame() |
|||
{ |
|||
FileStream file; |
|||
|
|||
if(!File.Exists(Application.persistentDataPath + "/save" + _fileNameExtension)) |
|||
{ |
|||
Debug.LogError("No Save Data found"); |
|||
return; |
|||
|
|||
} else |
|||
{ |
|||
file = File.Open(Application.persistentDataPath + "/save" + _fileNameExtension, FileMode.Open); |
|||
} |
|||
|
|||
BinaryFormatter formatter = new BinaryFormatter(); |
|||
|
|||
Save saveClass = (Save)formatter.Deserialize(file); |
|||
|
|||
file.Close(); |
|||
|
|||
foreach(ISaveable saveable in _saveRegistry) |
|||
{ |
|||
saveable.Deserialize(saveClass); |
|||
} |
|||
} |
|||
|
|||
private void SaveAndQuit() |
|||
{ |
|||
SaveGame(); |
|||
Application.Quit(); |
|||
} |
|||
|
|||
private void SaveGame() |
|||
{ |
|||
FileStream file; |
|||
|
|||
if(!File.Exists(Application.persistentDataPath + "/save" + _fileNameExtension)) |
|||
{ |
|||
file = File.Create(Application.persistentDataPath + "/save" + _fileNameExtension); |
|||
|
|||
} else |
|||
{ |
|||
file = File.Open(Application.persistentDataPath + "/save" + _fileNameExtension, FileMode.Open); |
|||
} |
|||
|
|||
// A class with name "Save" must exist in the project. It will contain the appropriate save file structure.
|
|||
Save saveClass = new Save(); |
|||
|
|||
foreach(ISaveable saveable in _saveRegistry) |
|||
{ |
|||
saveable.Serialize(saveClass); |
|||
} |
|||
|
|||
BinaryFormatter formatter = new BinaryFormatter(); |
|||
formatter.Serialize(file, saveClass); |
|||
file.Close(); |
|||
} |
|||
|
|||
private void OnDisable() |
|||
{ |
|||
SaveAndQuit(); |
|||
} |
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 428b35af66b1ab44c9cca9bb53864745 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue