浏览代码
Removed BinaryFormatter and Refactor.
Removed BinaryFormatter and Refactor.
Removed the BinaryFormatter because it's unsafe to use, so we're currently serializing and deserializing to json. Refactored the code for simplicity and readability./UI
Bronson Zgeb
4 年前
当前提交
5d81bec6
共有 7 个文件被更改,包括 106 次插入 和 84 次删除
-
20UOP1_Project/Assets/Scripts/SaveSystem/Save.cs
-
93UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs
-
9UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs
-
39UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs
-
3UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs.meta
-
23UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.cs
-
3UOP1_Project/Assets/Scripts/SaveSystem/ISaveable.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/>
|
|||
using UnityEngine; |
|||
|
|||
/// <summary>
|
|||
/// This class contains all the variables that will be serialized and saved to a file.<br/>
|
|||
// This is test data, written accodring to TestScript.cs class
|
|||
// This is test data, written according to TestScript.cs class
|
|||
|
|||
|
|||
public string ToJson() |
|||
{ |
|||
return JsonUtility.ToJson(this); |
|||
} |
|||
|
|||
public void LoadFromJson(string json) |
|||
{ |
|||
JsonUtility.FromJsonOverwrite(json, this); |
|||
} |
|||
} |
|
|||
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; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ec47a33ed5b5434f8bf6500b09136709 |
|||
timeCreated: 1613414864 |
|
|||
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); |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ea156e29696b4339a873e00d168dd164 |
|||
timeCreated: 1613415034 |
撰写
预览
正在加载...
取消
保存
Reference in new issue