123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- 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;
- }
- }
- }
|