A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3
4namespace Unity.VisualScripting
5{
6 public abstract class LudiqScriptableObject : ScriptableObject, ISerializationCallbackReceiver
7 {
8 [SerializeField, DoNotSerialize] // Serialize with Unity, but not with FullSerializer.
9 protected SerializationData _data;
10
11 internal event Action OnDestroyActions;
12
13 void ISerializationCallbackReceiver.OnBeforeSerialize()
14 {
15 // Ignore the FullSerializer callback, but still catch the Unity callback
16 if (Serialization.isCustomSerializing)
17 {
18 return;
19 }
20
21 Serialization.isUnitySerializing = true;
22
23 try
24 {
25 OnBeforeSerialize();
26 _data = this.Serialize(true);
27 OnAfterSerialize();
28 }
29 catch (Exception ex)
30 {
31 // Don't abort the whole serialization thread because this one object failed
32 Debug.LogError($"Failed to serialize scriptable object.\n{ex}", this);
33 }
34
35 Serialization.isUnitySerializing = false;
36 }
37
38 void ISerializationCallbackReceiver.OnAfterDeserialize()
39 {
40 // Ignore the FullSerializer callback, but still catch the Unity callback
41 if (Serialization.isCustomSerializing)
42 {
43 return;
44 }
45
46 Serialization.isUnitySerializing = true;
47
48 try
49 {
50 object @this = this;
51 OnBeforeDeserialize();
52 _data.DeserializeInto(ref @this, true);
53 OnAfterDeserialize();
54
55 _data.Clear();
56
57 UnityThread.EditorAsync(OnPostDeserializeInEditor);
58 }
59 catch (Exception ex)
60 {
61 // Don't abort the whole deserialization thread because this one object failed
62 Debug.LogError($"Failed to deserialize scriptable object.\n{ex}", this);
63 }
64
65 Serialization.isUnitySerializing = false;
66 }
67
68 protected virtual void OnBeforeSerialize() { }
69
70 protected virtual void OnAfterSerialize() { }
71
72 protected virtual void OnBeforeDeserialize() { }
73
74 protected virtual void OnAfterDeserialize() { }
75
76 protected virtual void OnPostDeserializeInEditor() { }
77
78 private void OnDestroy()
79 {
80 OnDestroyActions?.Invoke();
81 }
82
83 protected virtual void ShowData()
84 {
85 var data = this.Serialize(true);
86 data.ShowString(ToString());
87
88 data.Clear();
89 }
90
91 public override string ToString()
92 {
93 return this.ToSafeString();
94 }
95 }
96}