-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsMenu.cs
113 lines (86 loc) · 2.81 KB
/
SettingsMenu.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
using Zenvin.Settings.UI;
using UnityEngine;
using Zenvin.Settings.Framework;
using Zenvin.Settings.Framework.Serialization;
using System.IO;
using TMPro;
namespace Zenvin.Settings.Samples {
public class SettingsMenu : MonoBehaviour {
[SerializeField] private SettingsAsset asset;
[SerializeField] private bool sortSettings = true;
[SerializeField] private RectTransform settingHeaderPrefab;
[SerializeField] private SettingControlCollection controlPrefabs;
[SerializeField] private TabView tabView;
private JsonSerializer serializer;
private void Start () {
if (asset == null) {
return;
}
SpawnMenu ();
serializer = new JsonFileSerializer (Path.Combine (Application.dataPath, "settings_test.json"));
}
private void SpawnMenu () {
// get settings groups for tabs
var tabGroups = asset.GetGroups ();
foreach (var tg in tabGroups) {
// spawn tab
RectTransform tabTransform = tabView.AddTab (tg);
// populate tab
var tabSettings = tg.GetSettings (sortSettings);
foreach (var setting in tabSettings) {
SpawnPrefab (tabTransform, setting);
}
var contentGroups = tg.GetAllGroups ();
foreach (var cg in contentGroups) {
// spawn group header, if possible
if (settingHeaderPrefab != null) {
RectTransform header = Instantiate (settingHeaderPrefab);
header.SetParent (tabTransform);
header.localScale = Vector3.one;
TextMeshProUGUI label = header.GetComponentInChildren<TextMeshProUGUI> ();
if (label != null) {
label.SetText (cg.Name);
}
}
// spawn controls
var settings = cg.GetAllSettings (sortSettings);
foreach (var setting in settings) {
SpawnPrefab (tabTransform, setting);
}
}
}
}
private void SpawnPrefab (RectTransform parent, SettingBase setting) {
// get prefab fitting the setting type.
if (controlPrefabs.TryGetControl (setting.GetType (), out SettingControl ctrl)) {
// spawn prefab, if it is spawnable for the current setting
if (ctrl.TryInstantiateWith (setting, out SettingControl control)) {
// setup instance
control.transform.SetParent (parent);
control.transform.localScale = Vector3.one;
control.transform.localPosition = Vector3.zero;
} else {
Debug.Log ($"Can't spawn prefab for {setting.Name} ({setting.GetType ()})");
}
} else {
Debug.Log ($"No prefab found for {setting.Name} ({setting.GetType ()}).");
}
}
// Button methods
public void ApplyDirtySettings () {
asset.ApplyDirtySettings ();
}
public void RevertDirtySettings () {
asset.RevertDirtySettings ();
}
public void ResetAllSettings () {
asset.ResetAllSettings (true);
}
public void Save () {
asset.SerializeSettings (serializer);
}
public void Load () {
asset.DeserializeSettings (serializer);
}
}
}