XRPackageInitialization.cs
8.79 KB
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
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.XR.Management.Metadata;
namespace UnityEditor.XR.Management
{
/// <summary>Interface for specifying package initialization information</summary>
public interface XRPackageInitializationBase
{
/// <summary>Package name property</summary>
/// <value>The name of the package</value>
string PackageName { get; }
/// <summary>The loader full type name for this package</summary>
/// <value>Loader fulltype name</value>
string LoaderFullTypeName { get; }
/// <summary>The loader type name for this package</summary>
/// <value>Loader type name</value>
string LoaderTypeName { get; }
/// <summary>The settings full type name for this package</summary>
/// <value>Settings full type name</value>
string SettingsFullTypeName { get; }
/// <summary>The settings type name for this package</summary>
/// <value>Settings type name</value>
string SettingsTypeName { get; }
/// <summary>Package initialization key</summary>
/// <value>The init key for the package</value>
string PackageInitKey { get; }
/// <summary>Initialize package settings</summary>
/// <param name="obj">The scriptable object instance to initialize</param>
/// <returns>True if successful, false if not.</returns>
bool PopulateSettingsOnInitialization(ScriptableObject obj);
}
[InitializeOnLoad]
class XRPackageInitializationBootstrap
{
static XRPackageInitializationBootstrap()
{
if (!EditorApplication.isPlayingOrWillChangePlaymode)
{
EditorApplication.update += BeginPackageInitialization;
}
EditorApplication.playModeStateChanged += PlayModeStateChanged;
}
private static void PlayModeStateChanged(PlayModeStateChange state)
{
switch (state)
{
case PlayModeStateChange.EnteredPlayMode:
BeginPackageInitialization();
break;
case PlayModeStateChange.EnteredEditMode:
BeginPackageInitialization();
break;
}
}
internal static void BeginPackageInitialization()
{
EditorApplication.update -= BeginPackageInitialization;
foreach (var t in TypeLoaderExtensions.GetAllTypesWithInterface<IXRPackage>())
{
if (t.IsInterface || t.FullName.Contains("Unity.XR.Management.TestPackage") || t.FullName.Contains("UnityEditor.XR.Management.Metadata.KnownPackages"))
continue;
IXRPackage package = Activator.CreateInstance(t) as IXRPackage;
if (package == null)
{
Debug.LogError($"Unable to find an implementation for expected package type {t.FullName}.");
continue;
}
InitPackage(package);
}
foreach (var t in TypeLoaderExtensions.GetAllTypesWithInterface<XRPackageInitializationBase>())
{
if (t.IsInterface)
continue;
XRPackageInitializationBase packageInit = Activator.CreateInstance(t) as XRPackageInitializationBase;
if (packageInit == null)
{
Debug.LogError($"Unable to find an implementation for expected package type {t.FullName}.");
continue;
}
InitPackage(packageInit);
}
if (XRSettingsManager.Instance != null) XRSettingsManager.Instance.ResetUi = true;
}
internal static void InitPackage(IXRPackage package)
{
var packageMetadata = package.metadata;
if (packageMetadata == null)
{
Debug.LogError($"Package {package.GetType().Name} has a package definition but has no metadata. Skipping initialization.");
return;
}
XRPackageMetadataStore.AddPluginPackage(package);
if (!InitializePackageFromMetadata(package, packageMetadata))
{
Debug.LogWarning(
String.Format("{0} package Initialization not completed. You will need to create any instances of the loaders and settings manually before you can use the intended XR Plug-in Package.", packageMetadata.packageName));
}
}
static bool InitializePackageFromMetadata(IXRPackage package, IXRPackageMetadata packageMetadata)
{
bool ret = true;
ret = ret && InitializeLoaderFromMetadata(packageMetadata.packageName, packageMetadata.loaderMetadata);
ret = ret && InitializeSettingsFromMetadata(package, packageMetadata.packageName, packageMetadata.settingsType);
return ret;
}
static bool InitializeLoaderFromMetadata(string packageName, List<IXRLoaderMetadata> loaderMetadatas)
{
if (String.IsNullOrEmpty(packageName))
return false;
if (loaderMetadatas == null || loaderMetadatas.Count == 0)
{
Debug.LogWarning($"Package {packageName} has no loader metadata. Skipping loader initialization.");
return true;
}
bool ret = true;
foreach (var loader in loaderMetadatas)
{
bool hasInstance = EditorUtilities.AssetDatabaseHasInstanceOfType(loader.loaderType);
if (!hasInstance)
{
var obj = EditorUtilities.CreateScriptableObjectInstance(loader.loaderType,
EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultLoaderPath));
hasInstance = (obj != null);
if (!hasInstance)
{
Debug.LogError($"Error creating instance of loader {loader.loaderName} for package {packageName}");
}
}
ret |= hasInstance;
}
return ret;
}
static bool InitializeSettingsFromMetadata(IXRPackage package, string packageName, string settingsType)
{
if (String.IsNullOrEmpty(packageName))
return false;
if (settingsType == null)
{
Debug.LogWarning($"Package {packageName} has no settings metadata. Skipping settings initialization.");
return true;
}
bool ret = EditorUtilities.AssetDatabaseHasInstanceOfType(settingsType);
if (!ret)
{
var obj = EditorUtilities.CreateScriptableObjectInstance( settingsType,
EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultSettingsPath));
ret = package.PopulateNewSettingsInstance(obj);
}
return ret;
}
static void InitPackage(XRPackageInitializationBase packageInit)
{
if (!InitializeLoaderInstance(packageInit))
{
Debug.LogWarning(
String.Format("{0} Loader Initialization not completed. You will need to create an instance of the loader manually before you can use the intended XR Plug-in Package.", packageInit.PackageName));
}
if (!InitializeSettingsInstance(packageInit))
{
Debug.LogWarning(
String.Format("{0} Settings Initialization not completed. You will need to create an instance of settings to customize options specific to this package.", packageInit.PackageName));
}
}
static bool InitializeLoaderInstance(XRPackageInitializationBase packageInit)
{
bool ret = EditorUtilities.AssetDatabaseHasInstanceOfType(packageInit.LoaderTypeName);
if (!ret)
{
var obj = EditorUtilities.CreateScriptableObjectInstance(packageInit.LoaderFullTypeName,
EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultLoaderPath));
ret = (obj != null);
}
return ret;
}
static bool InitializeSettingsInstance(XRPackageInitializationBase packageInit)
{
bool ret = EditorUtilities.AssetDatabaseHasInstanceOfType(packageInit.SettingsTypeName);
if (!ret)
{
var obj = EditorUtilities.CreateScriptableObjectInstance(packageInit.SettingsFullTypeName,
EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultSettingsPath));
ret = packageInit.PopulateSettingsOnInitialization(obj);
}
return ret;
}
}
}