浏览代码

Merge branch 'CybernetHacker14-save-system' into main

/UI
Bronson Zgeb 3 年前
当前提交
81d376d9
共有 14 个文件被更改,包括 243 次插入0 次删除
  1. 8
      UOP1_Project/Assets/Scripts/SaveSystem.meta
  2. 11
      UOP1_Project/Assets/Scripts/SaveSystem/Save.cs.meta
  3. 11
      UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs.meta
  4. 8
      UOP1_Project/Assets/Scripts/SaveSystem/TestScript.meta
  5. 39
      UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs
  6. 3
      UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs.meta
  7. 23
      UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.cs
  8. 3
      UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.cs.meta
  9. 26
      UOP1_Project/Assets/Scripts/SaveSystem/Save.cs
  10. 55
      UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs
  11. 11
      UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs.meta
  12. 45
      UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs

8
UOP1_Project/Assets/Scripts/SaveSystem.meta


fileFormatVersion: 2
guid: 2f2c4e28b0a202d40a01e4ac82d1aed8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

11
UOP1_Project/Assets/Scripts/SaveSystem/Save.cs.meta


fileFormatVersion: 2
guid: 1d061ec6167cb064f8ec131f11729898
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs.meta


fileFormatVersion: 2
guid: 428b35af66b1ab44c9cca9bb53864745
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
UOP1_Project/Assets/Scripts/SaveSystem/TestScript.meta


fileFormatVersion: 2
guid: 2e698415cd1e96f46876987ec02fa185
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

39
UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs


using System;
using System.IO;
using UnityEngine;
public static class FileManager
{
public static bool WriteToFile(string fileName, string fileContents)
{
var fullPath = Path.Combine(Application.persistentDataPath, fileName);
try
{
File.WriteAllText(fullPath, fileContents);
return true;
}
catch (Exception e)
{
Debug.LogError($"Failed to write to {fullPath} with exception {e}");
return false;
}
}
public static bool LoadFromFile(string fileName, out string result)
{
var fullPath = Path.Combine(Application.persistentDataPath, fileName);
try
{
result = File.ReadAllText(fullPath);
return true;
}
catch (Exception e)
{
Debug.LogError($"Failed to read from {fullPath} with exception {e}");
result = "";
return false;
}
}
}

3
UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs.meta


fileFormatVersion: 2
guid: ec47a33ed5b5434f8bf6500b09136709
timeCreated: 1613414864

23
UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.cs


using System.Collections.Generic;
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);
}

3
UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.cs.meta


fileFormatVersion: 2
guid: ea156e29696b4339a873e00d168dd164
timeCreated: 1613415034

26
UOP1_Project/Assets/Scripts/SaveSystem/Save.cs


using UnityEngine;
/// <summary>
/// This class contains all the variables that will be serialized and saved to a file.<br/>
/// Can be considered as a save file structure or format.
/// </summary>
[System.Serializable]
public class Save {
// This is test data, written according 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;
public string ToJson()
{
return JsonUtility.ToJson(this);
}
public void LoadFromJson(string json)
{
JsonUtility.FromJsonOverwrite(json, this);
}
}

55
UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs


using System.Collections.Generic;
using UnityEngine;
public class SaveSystem : MonoBehaviour
{
public delegate void AddToRegistryCallback(HashSet<ISaveable> registry);
public static AddToRegistryCallback AddToRegistry;
HashSet<ISaveable> _saveRegistry = default;
const string SAVE_FILENAME = "save.chop";
private void Start()
{
Debug.Log("SaveSystem start getting called");
_saveRegistry = new HashSet<ISaveable>();
AddToRegistry?.Invoke(_saveRegistry);
LoadGame();
}
private void LoadGame()
{
if (FileManager.LoadFromFile(SAVE_FILENAME, out var json))
{
Save saveData = new Save();
saveData.LoadFromJson(json);
foreach (ISaveable saveable in _saveRegistry)
{
saveable.Deserialize(saveData);
}
}
}
private void SaveGame()
{
// A class with name "Save" must exist in the project. It will contain the appropriate save file structure.
Save saveData = new Save();
foreach (ISaveable saveable in _saveRegistry)
{
saveable.Serialize(saveData);
}
if (FileManager.WriteToFile(SAVE_FILENAME, saveData.ToJson()))
{
Debug.Log("Save successful");
}
}
private void OnDisable()
{
SaveGame();
}
}

11
UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs.meta


fileFormatVersion: 2
guid: 46c55738ade3e484997c03784a7af40a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

45
UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs


using System.Collections.Generic;
using System.IO;
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, in spite of all my efforts, I wasn't able to remove all dependencies.
/// </summary>
public class TestScript : MonoBehaviour, ISaveable
{
int _testInteger;
float _testFloat;
bool _testBool;
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;
}
}
正在加载...
取消
保存