Skip to content

Commit 3af44eb

Browse files
authored
Merge pull request #5 from TarasK8/feature/default-file
Feature/default file
2 parents 028acb7 + 55e9551 commit 3af44eb

File tree

8 files changed

+151
-3
lines changed

8 files changed

+151
-3
lines changed

Editor/FileObjectEditor.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Newtonsoft.Json.Linq;
2+
using TarasK8.SaveSystem;
3+
using TarasK8.SaveSystemEditor.JEditor;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace TarasK8.SaveSystemEditor
8+
{
9+
[CustomEditor(typeof(FileObject))]
10+
public class FileObjectEditor : Editor
11+
{
12+
private SerializedProperty _json_string;
13+
private JsonEditor _fileEditor;
14+
15+
private void OnEnable()
16+
{
17+
_json_string = serializedObject.FindProperty("_json");
18+
19+
string json = _json_string.stringValue;
20+
JToken token = string.IsNullOrEmpty(json) ? new JObject() : JToken.Parse(json);
21+
_fileEditor = new JsonEditor(token, this);
22+
}
23+
24+
public override void OnInspectorGUI()
25+
{
26+
_fileEditor.DrawEditor();
27+
}
28+
29+
private void OnDisable()
30+
{
31+
_json_string.stringValue = _fileEditor.GetJson();
32+
serializedObject.ApplyModifiedProperties();
33+
}
34+
}
35+
}

Editor/FileObjectEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/SaveSystemObjectEditor.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using TarasK8.SaveSystemEditor.JEditor;
55
using UnityEditor;
66
using UnityEngine;
7+
using static TarasK8.SaveSystem.SaveSystemObject;
78

89
namespace TarasK8.SaveSystemEditor
910
{
@@ -25,6 +26,10 @@ public class SaveSystemObjectEditor : Editor
2526
private SerializedProperty _encryptionPassword_string;
2627
private SerializedProperty _autoSaving_bool;
2728
private SerializedProperty _savePerion_int;
29+
private SerializedProperty _enableDefaultFile_bool;
30+
private SerializedProperty _defaultFileType_enum;
31+
private SerializedProperty _defaultFileObject_obj;
32+
private SerializedProperty _defaultFile_fileObj;
2833

2934
private SerializedProperty _onSave_event;
3035
private SerializedProperty _onLoad_event;
@@ -45,6 +50,11 @@ private void OnEnable()
4550
_autoSaving_bool = serializedObject.FindProperty("_autoSaving");
4651
_savePerion_int = serializedObject.FindProperty("_savePeriod");
4752

53+
_enableDefaultFile_bool = serializedObject.FindProperty("_enableDefaultFile");
54+
_defaultFileType_enum = serializedObject.FindProperty("_defaultFileType");
55+
_defaultFileObject_obj = serializedObject.FindProperty("_defaultFileObject");
56+
_defaultFile_fileObj = serializedObject.FindProperty("_defaultFile");
57+
4858
_onSave_event = serializedObject.FindProperty("OnSave");
4959
_onLoad_event = serializedObject.FindProperty("OnLoad");
5060
}
@@ -86,6 +96,25 @@ public override void OnInspectorGUI()
8696
EditorGUILayout.PropertyField(_savePerion_int, new GUIContent("Period (seconds)"));
8797
EditorGUILayout.EndHorizontal();
8898

99+
EditorGUILayout.BeginHorizontal();
100+
EditorGUILayout.PropertyField(_enableDefaultFile_bool, new GUIContent("Default Data"));
101+
if (_enableDefaultFile_bool.boolValue)
102+
{
103+
EditorGUILayout.BeginHorizontal();
104+
switch ((DefaultFileType)_defaultFileType_enum.enumValueIndex)
105+
{
106+
case DefaultFileType.File:
107+
EditorGUILayout.PropertyField(_defaultFile_fileObj, new GUIContent(string.Empty));
108+
break;
109+
case DefaultFileType.Object:
110+
EditorGUILayout.PropertyField(_defaultFileObject_obj, new GUIContent(string.Empty));
111+
break;
112+
}
113+
EditorGUILayout.PropertyField(_defaultFileType_enum, new GUIContent(string.Empty), GUILayout.Width(60f));
114+
EditorGUILayout.EndHorizontal();
115+
}
116+
EditorGUILayout.EndHorizontal();
117+
89118
EditorGUILayout.Space(10f);
90119

91120
EditorGUILayout.LabelField("Events", EditorStyles.boldLabel);

Runtime/FileObject.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using UnityEngine;
2+
3+
namespace TarasK8.SaveSystem
4+
{
5+
[CreateAssetMenu(fileName ="New File", menuName = "Save System/File")]
6+
public class FileObject : ScriptableObject
7+
{
8+
[SerializeField] private string _json;
9+
10+
public string GetJson()
11+
{
12+
return _json;
13+
}
14+
}
15+
}

Runtime/FileObject.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/SFile.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ public void LoadFromJson(string json)
131131
_data = data;
132132
}
133133

134+
public void MergeFromJson(string json)
135+
{
136+
JObject o1 = JObject.FromObject(_data);
137+
JObject o2 = JObject.Parse(json);
138+
o2.Merge(o1, new JsonMergeSettings
139+
{
140+
// union array values together to avoid duplicates
141+
MergeArrayHandling = MergeArrayHandling.Union
142+
});
143+
LoadFromJson(o2.ToString());
144+
}
145+
134146
public string GetJson()
135147
{
136148
return JsonConvert.SerializeObject(_data, Formatting.Indented);

Runtime/SaveSystemObject.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Newtonsoft.Json;
12
using System.Collections;
23
using System.Collections.Generic;
34
using System.IO;
@@ -21,6 +22,10 @@ public class SaveSystemObject : MonoBehaviour
2122
[SerializeField] private string _encryptionPassword = "password";
2223
[SerializeField] private bool _autoSaving = false;
2324
[SerializeField] private float _savePeriod = 30f;
25+
[SerializeField] private bool _enableDefaultFile = false;
26+
[SerializeField] private DefaultFileType _defaultFileType;
27+
[SerializeField] private Object _defaultFileObject = null;
28+
[SerializeField] private FileObject _defaultFile = null;
2429

2530
// [Header("Events")]
2631
[SerializeField] public UnityEvent<SFile> OnSave;
@@ -51,9 +56,12 @@ public void Initialize()
5156
public void Save()
5257
{
5358
if(_collectSaveablesEverySave) _seveables = GetObjectsForSave();
54-
foreach (var saveObject in _seveables)
59+
if (_seveables != null)
5560
{
56-
saveObject.OnSave(File);
61+
foreach (var saveObject in _seveables)
62+
{
63+
//saveObject.OnSave(File);
64+
}
5765
}
5866
OnSave?.Invoke(File);
5967
File.Save(GetPath(), GetPassword());
@@ -62,18 +70,27 @@ public void Save()
6270
public void Load()
6371
{
6472
if (_collectSaveablesEverySave) _seveables = GetObjectsForSave();
73+
6574
File.Load(GetPath(), GetPassword());
75+
76+
if (_enableDefaultFile)
77+
{
78+
File.MergeFromJson(GetDefaultDataJson());
79+
}
80+
6681
foreach (var saveObject in _seveables)
6782
{
6883
saveObject.OnLoad(File);
6984
}
85+
7086
OnLoad?.Invoke(File);
7187
}
7288

7389
public void StartAutosaving(float period)
7490
{
7591
_autoSaving = true;
7692
_savePeriod = period;
93+
if (period < 1f) return;
7794
_autosavingRoutine = StartCoroutine(AutosavingRoutine(period));
7895
}
7996

@@ -121,11 +138,29 @@ public string GetPassword()
121138
return _useEncryption ? _encryptionPassword : string.Empty;
122139
}
123140

141+
private string GetDefaultDataJson()
142+
{
143+
if(_defaultFileType == DefaultFileType.File)
144+
{
145+
return _defaultFile.GetJson();
146+
}
147+
else
148+
{
149+
return JsonConvert.SerializeObject(_defaultFileObject);
150+
}
151+
}
152+
124153
public enum PathType
125154
{
126155
PersistentDataPath = 1,
127156
DataPath = 2,
128157
CustomPath = 3
129158
}
159+
160+
public enum DefaultFileType
161+
{
162+
File = 0,
163+
Object = 1,
164+
}
130165
}
131166
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.tarask8.save-system",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"displayName": "Save System",
55
"description": "This is a simple save system for unity.",
66
"unity": "2022.3",

0 commit comments

Comments
 (0)