-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializableElement.cs
177 lines (151 loc) · 4.88 KB
/
SerializableElement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Sirenix.OdinInspector;
using UnityEngine.Serialization;
namespace AltSalt.Maestro
{
[ExecuteInEditMode]
public class SerializableElement : MonoBehaviour
{
#if UNITY_EDITOR
protected string activeLayoutName;
#endif
[ShowInInspector]
[ReadOnly]
[InfoBox("This ID will be appended to the filename where the object's data will be stored.")]
[SerializeField]
protected int id;
[ReadOnly]
[SerializeField]
protected string sceneName = "";
[ReadOnly]
[SerializeField]
bool initialized;
#if UNITY_EDITOR
[Title("$"+nameof(activeLayoutName))]
#endif
[ShowInInspector]
[InfoBox("Active layout should be set by a ModifyHandler at runtime, or via the LayoutTools or TextTools." +
" Do not touch this value unless you know what you're doing.", InfoMessageType.Warning)]
private bool _allowLayoutDebugging;
[ShowInInspector]
[EnableIf(nameof(_allowLayoutDebugging))]
private LayoutConfig _activeLayout;
protected LayoutConfig activeLayout
{
get => _activeLayout;
set => _activeLayout = value;
}
[FormerlySerializedAs("_layouts")]
[SerializeField]
[OnValueChanged(nameof(RefreshActiveLayout))]
private List<LayoutConfig> _layoutsLegacy = new List<LayoutConfig>();
[SerializeField]
private List<LayoutConfigReference> _layoutReferences = new List<LayoutConfigReference>();
private List<LayoutConfigReference> layouts => _layoutReferences;
[SerializeField]
[ReadOnly]
protected List<string> nonserializedProperties = new List<string>();
public int GetID()
{
return id;
}
[SerializeField]
private bool _migrated;
private bool migrated
{
get => _migrated;
set => _migrated = value;
}
protected virtual void OnEnable()
{
#if UNITY_EDITOR
for (int i = 0; i < _layoutReferences.Count; i++) {
_layoutReferences[i].PopulateVariable(this,
$"{nameof(_layoutReferences)}.{i.ToString()}");
}
Initialize();
if (migrated == false) {
migrated = true;
for (int i = 0; i < _layoutsLegacy.Count; i++) {
layouts.Add(new LayoutConfigReference(_layoutsLegacy[i]));
}
}
#endif
RefreshActiveLayout();
}
protected void RefreshActiveLayout()
{
if (layouts.Count > 0) {
activeLayout = LayoutConfig.GetActiveLayout(layouts.Select(x => x.GetVariable() as LayoutConfig).ToList());
}
}
#if UNITY_EDITOR
protected void Initialize()
{
if(initialized == false) {
SaveSceneName();
GenerateID();
initialized = true;
}
}
void SaveSceneName()
{
if(sceneName.Length < 1) {
sceneName = this.gameObject.scene.name;
}
}
void SaveSceneName(bool force)
{
if (force == true) {
sceneName = this.gameObject.scene.name;
}
}
void GenerateID()
{
if (id == 0 || initialized == false) {
id = (int)DateTime.Now.Ticks;
}
}
void GenerateID(bool force)
{
if (force == true) {
id = (int)DateTime.Now.Ticks;
}
initialized = true;
}
protected virtual void PopulateNonSerializedProperties()
{
nonserializedProperties.Clear();
nonserializedProperties.Add(nameof(id));
nonserializedProperties.Add(nameof(sceneName));
nonserializedProperties.Add(nameof(initialized));
nonserializedProperties.Add(nameof(_layoutsLegacy));
nonserializedProperties.Add(nameof(nonserializedProperties));
}
protected virtual void OnGUI()
{
UpdateActiveLayoutName();
}
private void UpdateActiveLayoutName()
{
if (activeLayout != null) {
activeLayoutName = "Current active layout: " + activeLayout.name;
}
else {
activeLayoutName = "No active layout.";
}
}
[InfoBox("Force reset of the element's scene name and serializable ID.")]
[Button(ButtonSizes.Large), GUIColor(0.4f, 0.8f, 1)]
[PropertyOrder(10)]
public void Reinitialize()
{
SaveSceneName(true);
GenerateID(true);
}
#endif
}
}