暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AdaptivePerformancePackageInitialization.cs 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor.AdaptivePerformance.Editor.Metadata;
  5. namespace UnityEditor.AdaptivePerformance.Editor
  6. {
  7. /// <summary>Interface for specifying package initialization information</summary>
  8. public interface AdaptivePerformancePackageInitializationBase
  9. {
  10. /// <summary>Package name property</summary>
  11. /// <value>The name of the package</value>
  12. string PackageName { get; }
  13. /// <summary>The loader full type name for this package</summary>
  14. /// <value>Loader full type name</value>
  15. string LoaderFullTypeName { get; }
  16. /// <summary>The loader type name for this package</summary>
  17. /// <value>Loader type name</value>
  18. string LoaderTypeName { get; }
  19. /// <summary>The settings full type name for this package</summary>
  20. /// <value>Settings full type name</value>
  21. string SettingsFullTypeName { get; }
  22. /// <summary>The settings type name for this package</summary>
  23. /// <value>Settings type name</value>
  24. string SettingsTypeName { get; }
  25. /// <summary>Package initialization key</summary>
  26. /// <value>The init key for the package</value>
  27. string PackageInitKey { get; }
  28. /// <summary>Initialize package settings</summary>
  29. /// <param name="obj">The scriptable object instance to initialize</param>
  30. /// <returns>True if successful, false if not.</returns>
  31. bool PopulateSettingsOnInitialization(ScriptableObject obj);
  32. }
  33. [InitializeOnLoad]
  34. class AdaptivePerformancePackageInitializationBootstrap
  35. {
  36. static AdaptivePerformancePackageInitializationBootstrap()
  37. {
  38. if (!EditorApplication.isPlayingOrWillChangePlaymode)
  39. {
  40. EditorApplication.update += BeginPackageInitialization;
  41. }
  42. EditorApplication.playModeStateChanged += PlayModeStateChanged;
  43. }
  44. private static void PlayModeStateChanged(PlayModeStateChange state)
  45. {
  46. switch (state)
  47. {
  48. case PlayModeStateChange.EnteredPlayMode:
  49. BeginPackageInitialization();
  50. break;
  51. case PlayModeStateChange.EnteredEditMode:
  52. BeginPackageInitialization();
  53. break;
  54. }
  55. }
  56. internal static void BeginPackageInitialization()
  57. {
  58. EditorApplication.update -= BeginPackageInitialization;
  59. foreach (var t in TypeLoaderExtensions.GetAllTypesWithInterface<IAdaptivePerformancePackage>())
  60. {
  61. if (t.IsInterface || t.FullName.Contains("UnityEditor.AdaptivePerformance.TestPackage") || t.FullName.Contains("UnityEditor.AdaptivePerformance.Editor.Metadata.AdaptivePerformanceKnownPackages"))
  62. continue;
  63. IAdaptivePerformancePackage package = Activator.CreateInstance(t) as IAdaptivePerformancePackage;
  64. if (package == null)
  65. {
  66. Debug.LogError($"Unable to find an implementation for expected package type {t.FullName}.");
  67. continue;
  68. }
  69. InitPackage(package);
  70. }
  71. foreach (var t in TypeLoaderExtensions.GetAllTypesWithInterface<AdaptivePerformancePackageInitializationBase>())
  72. {
  73. if (t.IsInterface)
  74. continue;
  75. AdaptivePerformancePackageInitializationBase packageInit = Activator.CreateInstance(t) as AdaptivePerformancePackageInitializationBase;
  76. if (packageInit == null)
  77. {
  78. Debug.LogError($"Unable to find an implementation for expected package type {t.FullName}.");
  79. continue;
  80. }
  81. InitPackage(packageInit);
  82. }
  83. if (AdaptivePerformanceSettingsManager.Instance != null)
  84. AdaptivePerformanceSettingsManager.Instance.ResetUi = true;
  85. }
  86. internal static void InitPackage(IAdaptivePerformancePackage package)
  87. {
  88. var packageMetadata = package.metadata;
  89. if (packageMetadata == null)
  90. {
  91. Debug.LogError($"Package {package.GetType().Name} has a package definition but has no metadata. Skipping initialization.");
  92. return;
  93. }
  94. AdaptivePerformancePackageMetadataStore.AddPluginPackage(package);
  95. if (!InitializePackageFromMetadata(package, packageMetadata))
  96. {
  97. Debug.LogWarning(
  98. 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 Adaptive Performance Provider Package.", packageMetadata.packageName));
  99. }
  100. }
  101. static bool InitializePackageFromMetadata(IAdaptivePerformancePackage package, IAdaptivePerformancePackageMetadata packageMetadata)
  102. {
  103. bool ret = true;
  104. ret = ret && InitializeLoaderFromMetadata(packageMetadata.packageName, packageMetadata.loaderMetadata);
  105. ret = ret && InitializeSettingsFromMetadata(package, packageMetadata.packageName, packageMetadata.settingsType, packageMetadata.licenseURL);
  106. return ret;
  107. }
  108. static bool InitializeLoaderFromMetadata(string packageName, List<IAdaptivePerformanceLoaderMetadata> loaderMetadatas)
  109. {
  110. if (String.IsNullOrEmpty(packageName))
  111. return false;
  112. if (loaderMetadatas == null || loaderMetadatas.Count == 0)
  113. {
  114. Debug.LogWarning($"Package {packageName} has no loader metadata. Skipping loader initialization.");
  115. return true;
  116. }
  117. bool ret = true;
  118. foreach (var loader in loaderMetadatas)
  119. {
  120. bool hasInstance = EditorUtilities.AssetDatabaseHasInstanceOfType(loader.loaderType);
  121. if (!hasInstance)
  122. {
  123. var obj = EditorUtilities.CreateScriptableObjectInstance(loader.loaderType,
  124. EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultLoaderPath));
  125. hasInstance = (obj != null);
  126. if (!hasInstance)
  127. {
  128. Debug.LogError($"Error creating instance of loader {loader.loaderName} for package {packageName}");
  129. }
  130. }
  131. ret |= hasInstance;
  132. }
  133. return ret;
  134. }
  135. static bool InitializeSettingsFromMetadata(IAdaptivePerformancePackage package, string packageName, string settingsType, string licenseURL)
  136. {
  137. if (String.IsNullOrEmpty(packageName))
  138. return false;
  139. if (settingsType == null)
  140. {
  141. Debug.LogWarning($"Package {packageName} has no settings metadata. Skipping settings initialization.");
  142. return true;
  143. }
  144. bool ret = EditorUtilities.AssetDatabaseHasInstanceOfType(settingsType);
  145. if (!ret)
  146. {
  147. var obj = EditorUtilities.CreateScriptableObjectInstance(settingsType,
  148. EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultSettingsPath));
  149. ret = package.PopulateNewSettingsInstance(obj);
  150. }
  151. return ret;
  152. }
  153. static void InitPackage(AdaptivePerformancePackageInitializationBase packageInit)
  154. {
  155. if (!InitializeLoaderInstance(packageInit))
  156. {
  157. Debug.LogWarning(
  158. String.Format("{0} Loader Initialization not completed. You will need to create an instance of the loader manually before you can use the intended Adaptive Performance Provider Package.", packageInit.PackageName));
  159. }
  160. if (!InitializeSettingsInstance(packageInit))
  161. {
  162. Debug.LogWarning(
  163. 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));
  164. }
  165. }
  166. static bool InitializeLoaderInstance(AdaptivePerformancePackageInitializationBase packageInit)
  167. {
  168. bool ret = EditorUtilities.AssetDatabaseHasInstanceOfType(packageInit.LoaderTypeName);
  169. if (!ret)
  170. {
  171. var obj = EditorUtilities.CreateScriptableObjectInstance(packageInit.LoaderFullTypeName,
  172. EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultLoaderPath));
  173. ret = (obj != null);
  174. }
  175. return ret;
  176. }
  177. static bool InitializeSettingsInstance(AdaptivePerformancePackageInitializationBase packageInit)
  178. {
  179. bool ret = EditorUtilities.AssetDatabaseHasInstanceOfType(packageInit.SettingsTypeName);
  180. if (!ret)
  181. {
  182. var obj = EditorUtilities.CreateScriptableObjectInstance(packageInit.SettingsFullTypeName,
  183. EditorUtilities.GetAssetPathForComponents(EditorUtilities.s_DefaultSettingsPath));
  184. ret = packageInit.PopulateSettingsOnInitialization(obj);
  185. }
  186. return ret;
  187. }
  188. }
  189. }