Skip to content

Commit 2787414

Browse files
committed
fix: The database is now saved to disk when changed
1 parent 2ce50d0 commit 2787414

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

Runtime/GenericDerivativesDatabase.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,50 @@
22
{
33
using System;
44
using System.Collections.Generic;
5-
using System.Runtime.Serialization;
5+
using Sirenix.OdinInspector;
66
using TypeReferences;
7+
using UnityEditor;
78
using UnityEngine;
89

910
public class GenericDerivativesDatabase :
1011
SingletonScriptableObject<GenericDerivativesDatabase>,
11-
ISerializationCallbackReceiver,
12-
IDeserializationCallback,
13-
ISerializable
12+
ISerializationCallbackReceiver
1413
{
1514
public const string Template = "namespace GenericScriptableObjectsTypes { public class " +
1615
"Generic_#TYPE_NAME : GenericScriptableObjects.Generic<#TYPE> { } }";
1716

1817
private readonly Dictionary<TypeReference, TypeReference> _dict =
1918
new Dictionary<TypeReference, TypeReference>(new TypeReferenceComparer());
2019

21-
[SerializeField, TypeOptions(ExcludeNone = true, ShortName = true)]
20+
[SerializeField]
21+
// [TypeOptions(ExcludeNone = true, ShortName = true)]
2222
// [HideInInspector] // TODO: hide fields
2323
private TypeReference[] _keys;
2424

25-
[SerializeField, Inherits(typeof(Generic<>), ExcludeNone = true, ShortName = true, ExpandAllFolders = true)]
25+
[SerializeField]
26+
// [Inherits(typeof(Generic<>), ExcludeNone = true, ShortName = true, ExpandAllFolders = true)]
2627
// [HideInInspector]
2728
private TypeReference[] _values;
2829

2930
public TypeReference this[TypeReference key]
3031
{
3132
get => _dict[key];
32-
set => _dict[key] = value;
33+
set
34+
{
35+
_dict[key] = value;
36+
EditorUtility.SetDirty(this);
37+
}
3338
}
3439

3540
public static void Add(Type key, Type value) =>
3641
Instance._dict.Add(new TypeReference(key), new TypeReference(value));
3742

38-
public static bool ContainsKey(Type key) => Instance._dict.ContainsKey(new TypeReference(key));
43+
public static bool ContainsKey(Type key)
44+
{
45+
bool containsKey = Instance._dict.ContainsKey(new TypeReference(key));
46+
EditorUtility.SetDirty(Instance);
47+
return containsKey;
48+
}
3949

4050
public static bool TryGetValue(TypeReference key, out TypeReference value) =>
4151
Instance._dict.TryGetValue(key, out value);
@@ -80,10 +90,5 @@ public void OnBeforeSerialize()
8090
++keysIndex;
8191
}
8292
}
83-
84-
public void OnDeserialization(object sender) => _dict.OnDeserialization(sender);
85-
86-
public void GetObjectData(SerializationInfo info, StreamingContext context) =>
87-
_dict.GetObjectData(info, context);
8893
}
8994
}

Runtime/SingletonScriptableObject.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
namespace GenericScriptableObjects
22
{
3+
#if UNITY_EDITOR
34
using UnityEditor;
5+
#endif
46
using UnityEngine;
57

6-
#if UNITY_EDITOR
7-
#endif
8+
89

910
/// <summary>
1011
/// Abstract class for making reload-proof singletons out of ScriptableObjects
@@ -43,8 +44,9 @@ public static T Instance
4344
AssetDatabase.CreateFolder(assetsFolder, resourcesFolder);
4445

4546
AssetDatabase.CreateAsset(_instance, assetPath);
47+
EditorUtility.SetDirty(_instance);
4648
#else
47-
Debug.Log($"The asset of type {typeof(T)} was not created. Please go to editor and create it.");
49+
Debug.Log($"The asset of type {typeof(T)} was not created. Please go to editor and create it.");
4850
#endif
4951

5052
return _instance;

0 commit comments

Comments
 (0)