forked from Tunkali/com.unity.xr.management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XRGeneralSettingsPerBuildTarget.cs
259 lines (227 loc) · 11.2 KB
/
XRGeneralSettingsPerBuildTarget.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.XR.Management;
using UnityEditor.XR.Management.Metadata;
namespace UnityEditor.XR.Management
{
#if UNITY_EDITOR
[InitializeOnLoad]
#endif
/// <summary>Container class that holds general settings for each build target group installed in Unity.</summary>
public class XRGeneralSettingsPerBuildTarget : ScriptableObject, ISerializationCallbackReceiver
{
[SerializeField]
List<BuildTargetGroup> Keys = new List<BuildTargetGroup>();
[SerializeField]
List<XRGeneralSettings> Values = new List<XRGeneralSettings>();
Dictionary<BuildTargetGroup, XRGeneralSettings> Settings = new Dictionary<BuildTargetGroup, XRGeneralSettings>();
#if UNITY_EDITOR
static XRGeneralSettingsPerBuildTarget()
{
EditorApplication.playModeStateChanged -= PlayModeStateChanged;
EditorApplication.playModeStateChanged += PlayModeStateChanged;
}
[MethodImpl(MethodImplOptions.Synchronized)]
internal static bool TryFindSettingsAsset(out XRGeneralSettingsPerBuildTarget generalSettings)
{
EditorBuildSettings.TryGetConfigObject<XRGeneralSettingsPerBuildTarget>(XRGeneralSettings.k_SettingsKey, out generalSettings);
if (generalSettings == null)
{
var assets = AssetDatabase.FindAssets("t:XRGeneralSettingsPerBuildTarget");
if (assets.Length > 0)
{
string path = AssetDatabase.GUIDToAssetPath(assets[0]);
generalSettings = AssetDatabase.LoadAssetAtPath(path, typeof(XRGeneralSettingsPerBuildTarget)) as XRGeneralSettingsPerBuildTarget;
}
}
return generalSettings != null;
}
[MethodImpl(MethodImplOptions.Synchronized)]
static XRGeneralSettingsPerBuildTarget CreateAssetSynchronized()
{
var generalSettings = CreateInstance(typeof(XRGeneralSettingsPerBuildTarget)) as XRGeneralSettingsPerBuildTarget;
string assetPath = EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultGeneralSettingsPath);
if (!string.IsNullOrEmpty(assetPath))
{
assetPath = Path.Combine(assetPath, "XRGeneralSettingsPerBuildTarget.asset");
AssetDatabase.CreateAsset(generalSettings, assetPath);
AssetDatabase.SaveAssets();
}
EditorBuildSettings.AddConfigObject(XRGeneralSettings.k_SettingsKey, generalSettings, true);
return generalSettings;
}
internal static XRGeneralSettingsPerBuildTarget GetOrCreate()
=> TryFindSettingsAsset(out var generalSettings) ? generalSettings : CreateAssetSynchronized();
// Simple class to give us updates when the asset database changes.
class AssetCallbacks : AssetPostprocessor
{
static bool m_Upgrade = true;
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
if (m_Upgrade)
{
m_Upgrade = false;
BeginUpgradeSettings();
}
}
static void BeginUpgradeSettings()
{
string searchText = "t:XRGeneralSettings";
string[] assets = AssetDatabase.FindAssets(searchText);
if (assets.Length > 0)
{
string path = AssetDatabase.GUIDToAssetPath(assets[0]);
XRGeneralSettingsUpgrade.UpgradeSettingsToPerBuildTarget(path);
}
}
}
void OnEnable()
{
foreach (var setting in Settings.Values)
{
var assignedSettings = setting.AssignedSettings;
if (assignedSettings == null)
continue;
var filteredLoaders = from ldr in assignedSettings.activeLoaders where ldr != null select ldr;
assignedSettings.TrySetLoaders(filteredLoaders.ToList<XRLoader>());
}
XRGeneralSettings.Instance = XRGeneralSettingsForBuildTarget(BuildTargetGroup.Standalone);
}
static void PlayModeStateChanged(PlayModeStateChange state)
{
XRGeneralSettingsPerBuildTarget buildTargetSettings = null;
EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out buildTargetSettings);
if (buildTargetSettings == null)
return;
XRGeneralSettings instance = buildTargetSettings.SettingsForBuildTarget(BuildTargetGroup.Standalone);
if (instance == null || !instance.InitManagerOnStart)
return;
instance.InternalPlayModeStateChanged(state);
}
internal static bool ContainsLoaderForAnyBuildTarget(string loaderTypeName)
{
XRGeneralSettingsPerBuildTarget buildTargetSettings = null;
EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out buildTargetSettings);
if (buildTargetSettings == null)
return false;
foreach (var settings in buildTargetSettings.Settings.Values)
{
if (XRPackageMetadataStore.IsLoaderAssigned(settings.Manager, loaderTypeName))
return true;
}
return false;
}
#endif
/// <summary>
/// Query this settings store to see if there are settings for a specific <see cref="BuildTargetGroup"/>.
/// </summary>
/// <param name="buildTargetGroup">Build target to check</param>
/// <returns>True if there are settings, otherwise false.</returns>
public bool HasSettingsForBuildTarget(BuildTargetGroup buildTargetGroup)
{
return SettingsForBuildTarget(buildTargetGroup) != null;
}
/// <summary>
/// Create default settings for a given build target.
///
/// This <b>will overwrite</b> any current settings for that build target.
/// </summary>
/// <param name="buildTargetGroup">Build target to create default settings for.</param>
public void CreateDefaultSettingsForBuildTarget(BuildTargetGroup buildTargetGroup)
{
var settings = ScriptableObject.CreateInstance<XRGeneralSettings>() as XRGeneralSettings;
SetSettingsForBuildTarget(buildTargetGroup, settings);
settings.name = $"{buildTargetGroup.ToString()} Settings";
AssetDatabase.AddObjectToAsset(settings, AssetDatabase.GetAssetOrScenePath(this));
AssetDatabase.SaveAssets();
}
/// <summary>Set specific settings for a given build target.</summary>
///
/// <param name="targetGroup">An enum specifying which platform group this build is for.</param>
/// <param name="settings">An instance of <see cref="XRGeneralSettings"/> to assign for the given key.</param>
public void SetSettingsForBuildTarget(BuildTargetGroup targetGroup, XRGeneralSettings settings)
{
// Ensures the editor's "runtime instance" is the most current for standalone settings
if (targetGroup == BuildTargetGroup.Standalone)
XRGeneralSettings.Instance = settings;
Settings[targetGroup] = settings;
}
/// <summary>Get specific settings for a given build target.</summary>
/// <param name="targetGroup">An enum specifying which platform group this build is for.</param>
/// <returns>The instance of <see cref="XRGeneralSettings"/> assigned to the key, or null if not.</returns>
public XRGeneralSettings SettingsForBuildTarget(BuildTargetGroup targetGroup)
{
XRGeneralSettings ret = null;
Settings.TryGetValue(targetGroup, out ret);
return ret;
}
/// <summary>
/// Check if current settings instance has an instance of <see cref="XRManagerSettings"/>.
/// </summary>
/// <param name="targetGroup">An enum specifying which platform group this build is for.</param>
/// <returns>True if it exists, false otherwise.</returns>
public bool HasManagerSettingsForBuildTarget(BuildTargetGroup targetGroup)
{
return (SettingsForBuildTarget(targetGroup)?.Manager ?? null) != null;
}
/// <summary>
/// Create a new default instance of <see cref="XRManagerSettings"/> for a build target. Requires
/// that the there exists a settings instance for the build target. If there isn't, then one is created.
///
/// This <b>will overwrite</b> any current settings for that build target.
/// </summary>
/// <param name="targetGroup">An enum specifying which platform group this build is for.</param>
public void CreateDefaultManagerSettingsForBuildTarget(BuildTargetGroup targetGroup)
{
if (!HasSettingsForBuildTarget(targetGroup))
CreateDefaultSettingsForBuildTarget(targetGroup);
var xrManagerSettings = ScriptableObject.CreateInstance<XRManagerSettings>() as XRManagerSettings;
xrManagerSettings.name = $"{targetGroup.ToString()} Providers";
SettingsForBuildTarget(targetGroup).Manager = xrManagerSettings;
AssetDatabase.AddObjectToAsset(xrManagerSettings, AssetDatabase.GetAssetOrScenePath(this));
AssetDatabase.SaveAssets();
}
/// <summary>
/// Return the current instance of <see cref="XRManagerSettings"/> for a build target.
/// </summary>
/// <param name="targetGroup">An enum specifying which platform group this build is for.</param>
/// <returns></returns>
public XRManagerSettings ManagerSettingsForBuildTarget(BuildTargetGroup targetGroup)
{
return SettingsForBuildTarget(targetGroup)?.Manager ?? null;
}
/// <summary>Serialization override.</summary>
public void OnBeforeSerialize()
{
Keys.Clear();
Values.Clear();
foreach (var kv in Settings)
{
Keys.Add(kv.Key);
Values.Add(kv.Value);
}
}
/// <summary>Serialization override.</summary>
public void OnAfterDeserialize()
{
Settings = new Dictionary<BuildTargetGroup, XRGeneralSettings>();
for (int i = 0; i < Math.Min(Keys.Count, Values.Count); i++)
{
Settings.Add(Keys[i], Values[i]);
}
}
/// <summary>Given a build target, get the general settings container assigned to it.</summary>
/// <param name="targetGroup">An enum specifying which platform group this build is for.</param>
/// <returns>The instance of <see cref="XRGeneralSettings"/> assigned to the key, or null if not.</returns>
public static XRGeneralSettings XRGeneralSettingsForBuildTarget(BuildTargetGroup targetGroup)
{
if (!TryFindSettingsAsset(out var buildTargetSettings))
return null;
return buildTargetSettings.SettingsForBuildTarget(targetGroup);
}
}
}