浏览代码
Merge branch 'save-system' of https://github.com/CybernetHacker14/open-project-1 into CybernetHacker14-save-system
/UI
Merge branch 'save-system' of https://github.com/CybernetHacker14/open-project-1 into CybernetHacker14-save-system
/UI
Bronson Zgeb
4 年前
当前提交
efec78c5
共有 10 个文件被更改,包括 221 次插入 和 0 次删除
-
8UOP1_Project/Assets/Scripts/SaveSystem.meta
-
16UOP1_Project/Assets/Scripts/SaveSystem/Save.cs
-
11UOP1_Project/Assets/Scripts/SaveSystem/Save.cs.meta
-
11UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs.meta
-
8UOP1_Project/Assets/Scripts/SaveSystem/TestScript.meta
-
44UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs
-
11UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs.meta
-
112UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs
|
|||
fileFormatVersion: 2 |
|||
guid: 2f2c4e28b0a202d40a01e4ac82d1aed8 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
/// <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 { |
|||
// This is test data, written accodring to TestScript.cs class
|
|||
// This will change according to whatever data that needs to be stored
|
|||
|
|||
// The variables need to be public, else we would have to write trivial getter/setter functions.
|
|||
|
|||
public int _testInteger = default; |
|||
public float _testFloat = default; |
|||
public bool _testBool = default; |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 1d061ec6167cb064f8ec131f11729898 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 428b35af66b1ab44c9cca9bb53864745 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 2e698415cd1e96f46876987ec02fa185 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
/// <summary>
|
|||
/// A test script showing usage of the save system.<br/>
|
|||
/// This code corresponds to the code written in <see cref="Save"/> class.<br/>
|
|||
/// Unfortunately, inspite of all my efforts, I wasn't able to remove all dependencies.
|
|||
/// </summary>
|
|||
public class TestScript : MonoBehaviour, ISaveable |
|||
{ |
|||
int _testInteger = default; |
|||
float _testFloat = default; |
|||
bool _testBool = default; |
|||
|
|||
private void OnEnable() |
|||
{ |
|||
// This is necessary, otherwise the object won't get added to registry of objects which needs to be serialized
|
|||
SaveSystem.AddToRegistry += AddToSaveRegistry; |
|||
} |
|||
|
|||
private void OnDisable() |
|||
{ |
|||
SaveSystem.AddToRegistry -= AddToSaveRegistry; |
|||
} |
|||
|
|||
public void AddToSaveRegistry(HashSet<ISaveable> registry) |
|||
{ |
|||
registry.Add(this); |
|||
} |
|||
|
|||
public void Serialize(Save saveFile) |
|||
{ |
|||
saveFile._testInteger = _testInteger; |
|||
saveFile._testFloat = _testFloat; |
|||
saveFile._testBool = _testBool; |
|||
} |
|||
|
|||
public void Deserialize(Save saveFile) |
|||
{ |
|||
_testInteger = saveFile._testInteger; |
|||
_testFloat = saveFile._testFloat; |
|||
_testBool = saveFile._testBool; |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 46c55738ade3e484997c03784a7af40a |
|||
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"); |
|||
_saveRegistry = new HashSet<ISaveable>(); |
|||
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(); |
|||
} |
|||
|
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue