浏览代码

Basic structure for saving location to a file

Added a Uuid type and drawer

Removed saving TestScript

Added a Database and Desc type to reference other SOs by UUID

When it comes to serializing data (scenes and items for example) it's much simpler to be able to reference everything by a UUID and to be able to easily look them up in a table.

Added a uuid to the LocationSOs and put them in a DB

Converted SaveSystem into a SO. Removed test vars from Save.
/UI
Bronson Zgeb 3 年前
当前提交
451b07c9
共有 27 个文件被更改,包括 222 次插入46 次删除
  1. 2
      UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/Beach.asset
  2. 2
      UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/Forest.asset
  3. 2
      UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/Glade.asset
  4. 4
      UOP1_Project/Assets/Scripts/SaveSystem/Save.cs
  5. 56
      UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs
  6. 6
      UOP1_Project/Assets/Scripts/SceneManagement/ScriptableObjects/LocationSO.cs
  7. 2
      UOP1_Project/Assets/Scripts/SaveSystem/Editor/UuidDrawer.cs.meta
  8. 8
      UOP1_Project/Assets/ScriptableObjects/SaveSystem.meta
  9. 18
      UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/LocationDatabase.asset
  10. 8
      UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/LocationDatabase.asset.meta
  11. 20
      UOP1_Project/Assets/Scripts/SaveSystem/Database.cs
  12. 11
      UOP1_Project/Assets/Scripts/SaveSystem/Database.cs.meta
  13. 8
      UOP1_Project/Assets/Scripts/SaveSystem/Editor.meta
  14. 4
      UOP1_Project/Assets/Scripts/SaveSystem/IDesc.cs
  15. 11
      UOP1_Project/Assets/Scripts/SaveSystem/IDesc.cs.meta
  16. 1
      UOP1_Project/Assets/Scripts/SaveSystem/LocationDatabase.cs
  17. 11
      UOP1_Project/Assets/Scripts/SaveSystem/LocationDatabase.cs.meta
  18. 11
      UOP1_Project/Assets/Scripts/SaveSystem/Uuid.cs
  19. 11
      UOP1_Project/Assets/Scripts/SaveSystem/Uuid.cs.meta
  20. 19
      UOP1_Project/Assets/ScriptableObjects/SaveSystem/SaveSystem.asset
  21. 8
      UOP1_Project/Assets/ScriptableObjects/SaveSystem/SaveSystem.asset.meta
  22. 37
      UOP1_Project/Assets/Scripts/SaveSystem/Editor/UuidDrawer.cs
  23. 8
      UOP1_Project/Assets/Scripts/SaveSystem/TestScript.meta
  24. 0
      /UOP1_Project/Assets/Scripts/SaveSystem/Editor/UuidDrawer.cs.meta

2
UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/Beach.asset


m_Script: {fileID: 11500000, guid: 8db63739a6674d74e8e232347422172f, type: 3}
m_Name: Beach
m_EditorClassIdentifier:
uuid:
uuid: 9b8c3478-f2b2-4346-a969-d04f92c809b7
sceneAsset: {fileID: 102900000, guid: dfaf60263d5c5dc4e80e2793f4328b32, type: 3}
scenePath: Assets/Scenes/Locations/Beach.unity
shortDescription: The level to find X

2
UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/Forest.asset


m_Script: {fileID: 11500000, guid: 8db63739a6674d74e8e232347422172f, type: 3}
m_Name: Forest
m_EditorClassIdentifier:
uuid:
uuid: 47723aeb-1bae-48c7-a760-4d6339e4a68a
sceneAsset: {fileID: 102900000, guid: 3a816f06c17361b4398583570bcbf8eb, type: 3}
scenePath: Assets/Scenes/Locations/Forest.unity
shortDescription: Find Y

2
UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/Glade.asset


m_Script: {fileID: 11500000, guid: 8db63739a6674d74e8e232347422172f, type: 3}
m_Name: Glade
m_EditorClassIdentifier:
uuid:
uuid: beb088ab-40f0-4c23-b0d4-08f05b41e310
sceneAsset: {fileID: 102900000, guid: 92a4a277a346e4344b629ad5613f7c51, type: 3}
scenePath: Assets/Scenes/Locations/Glade.unity
shortDescription: Glade Location

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


// 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 _locationId;
public string ToJson()
{

56
UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs


using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
public class SaveSystem : MonoBehaviour
public class SaveSystem : ScriptableObject
public delegate void AddToRegistryCallback(HashSet<ISaveable> registry);
public static AddToRegistryCallback AddToRegistry;
public LocationDatabase locationDatabase;
[SerializeField] private LoadEventChannelSO _loadLocation = default;
HashSet<ISaveable> _saveRegistry = default;
public string saveFilename = "save.chop";
public Save saveData = new Save();
const string SAVE_FILENAME = "save.chop";
void OnEnable()
{
_loadLocation.OnLoadingRequested += CacheLoadLocations;
}
private void Start()
void OnDisable()
Debug.Log("SaveSystem start getting called");
_saveRegistry = new HashSet<ISaveable>();
AddToRegistry?.Invoke(_saveRegistry);
LoadGame();
_loadLocation.OnLoadingRequested -= CacheLoadLocations;
private void LoadGame()
private void CacheLoadLocations(GameSceneSO[] locationsToLoad, bool showLoadingScreen)
if (FileManager.LoadFromFile(SAVE_FILENAME, out var json))
LocationSO locationSo = locationsToLoad[0] as LocationSO;
if (locationSo)
Save saveData = new Save();
saveData.LoadFromJson(json);
foreach (ISaveable saveable in _saveRegistry)
{
saveable.Deserialize(saveData);
}
saveData._locationId = locationSo.DescId.uuid;
private void SaveGame()
public void LoadGame()
// 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)
if (FileManager.LoadFromFile(saveFilename, out var json))
saveable.Serialize(saveData);
saveData.LoadFromJson(json);
}
if (FileManager.WriteToFile(SAVE_FILENAME, saveData.ToJson()))
public void SaveGame()
{
if (FileManager.WriteToFile(saveFilename, saveData.ToJson()))
}
private void OnDisable()
{
SaveGame();
}
}

6
UOP1_Project/Assets/Scripts/SceneManagement/ScriptableObjects/LocationSO.cs


/// </summary>
[CreateAssetMenu(fileName = "NewLocation", menuName = "Scene Data/Location")]
public class LocationSO : GameSceneSO
public class LocationSO : GameSceneSO, IDesc
[Header("Serialization")]
[SerializeField] Uuid uuid;
public Uuid DescId => uuid;
}

2
UOP1_Project/Assets/Scripts/SaveSystem/Editor/UuidDrawer.cs.meta


fileFormatVersion: 2
guid: 46c55738ade3e484997c03784a7af40a
guid: 69b0175021c7d9d4f9bed72462c8df1d
MonoImporter:
externalObjects: {}
serializedVersion: 2

8
UOP1_Project/Assets/ScriptableObjects/SaveSystem.meta


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

18
UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/LocationDatabase.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a07abb7875a4fc945b9a7cf5212ed565, type: 3}
m_Name: LocationDatabase
m_EditorClassIdentifier:
_descs:
- {fileID: 11400000, guid: 2f386bbc670c05145834a0f9c7d55798, type: 2}
- {fileID: 11400000, guid: d102ba8fe3b291249aeb9de4b95c1904, type: 2}
- {fileID: 11400000, guid: 3902a4362b3a4fb4ab5ce5dcc07d8427, type: 2}

8
UOP1_Project/Assets/ScriptableObjects/SceneData/Locations/LocationDatabase.asset.meta


fileFormatVersion: 2
guid: 9abcccc3f16b8c54e984de463d147899
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

20
UOP1_Project/Assets/Scripts/SaveSystem/Database.cs


using System.Collections.Generic;
using UnityEngine;
public abstract class Database<T> : ScriptableObject where T : IDesc
{
[SerializeField] protected List<T> _descs = new List<T>();
public virtual object FindByUuid(string uuid)
{
foreach (var desc in _descs)
{
if (desc.DescId.uuid == uuid)
{
return desc;
}
}
return null;
}
}

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


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

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


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

4
UOP1_Project/Assets/Scripts/SaveSystem/IDesc.cs


public interface IDesc
{
Uuid DescId { get; }
}

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


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

1
UOP1_Project/Assets/Scripts/SaveSystem/LocationDatabase.cs


public class LocationDatabase : Database<LocationSO> { }

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


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

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


[System.Serializable]
public class Uuid
{
public string uuid;
public static Uuid Generate()
{
Uuid newUuid = new Uuid {uuid = System.Guid.NewGuid().ToString()};
return newUuid;
}
}

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


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

19
UOP1_Project/Assets/ScriptableObjects/SaveSystem/SaveSystem.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 428b35af66b1ab44c9cca9bb53864745, type: 3}
m_Name: SaveSystem
m_EditorClassIdentifier:
locationDatabase: {fileID: 11400000, guid: 9abcccc3f16b8c54e984de463d147899, type: 2}
_loadLocation: {fileID: 11400000, guid: 00e3063edc5902e40832ea618644c597, type: 2}
saveFilename: save.chop
saveData:
_locationId: 47723aeb-1bae-48c7-a760-4d6339e4a68a

8
UOP1_Project/Assets/ScriptableObjects/SaveSystem/SaveSystem.asset.meta


fileFormatVersion: 2
guid: edc355c4a7d5028408d322b90814e19e
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

37
UOP1_Project/Assets/Scripts/SaveSystem/Editor/UuidDrawer.cs


using System;
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(Uuid))]
public class UuidDrawer : PropertyDrawer
{
public override void OnGUI(Rect a_Position, SerializedProperty a_Property, GUIContent a_Label)
{
EditorGUI.BeginProperty(a_Position, a_Label, a_Property);
a_Position = EditorGUI.PrefixLabel(a_Position, GUIUtility.GetControlID(FocusType.Passive), a_Label);
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
const int uuidButtonWidth = 80;
Rect uuidRect = new Rect(a_Position.x, a_Position.y, a_Position.width - uuidButtonWidth, a_Position.height);
Rect newUuidButtonRect =
new Rect(a_Position.x + uuidRect.width, a_Position.y, uuidButtonWidth, a_Position.height);
SerializedProperty uuidProperty = a_Property.FindPropertyRelative("uuid");
EditorGUI.SelectableLabel(uuidRect, uuidProperty.stringValue);
if (GUI.Button(newUuidButtonRect, "New Uuid"))
{
if (EditorUtility.DisplayDialog("Generate new UUID?",
"Generating a new UUID will break any data using the current uuid", "Yes", "Cancel"))
{
uuidProperty.stringValue = Guid.NewGuid().ToString();
}
}
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
}

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


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

/UOP1_Project/Assets/Scripts/SaveSystem/TestScript/TestScript.cs.meta → /UOP1_Project/Assets/Scripts/SaveSystem/Editor/UuidDrawer.cs.meta

正在加载...
取消
保存