No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdaptivePerformanceLoaderHelper.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. using UnityEngine;
  9. namespace UnityEngine.AdaptivePerformance
  10. {
  11. /// <summary>
  12. /// Adaptive Performance Loader abstract subclass used as a base class for specific provider implementations. Class provides some
  13. /// helper logic that can be used to handle subsystem handling in a typesafe manner, reducing potential boilerplate
  14. /// code.
  15. /// </summary>
  16. public abstract class AdaptivePerformanceLoaderHelper : AdaptivePerformanceLoader
  17. {
  18. /// <summary>
  19. /// Map of loaded susbsystems. Used so Unity doesn't always have to call AdaptivePerformanceManger and do a manual
  20. /// search to find the instance it loaded.
  21. /// </summary>
  22. protected Dictionary<Type, ISubsystem> m_SubsystemInstanceMap = new Dictionary<Type, ISubsystem>();
  23. /// <summary>
  24. /// Gets the loaded subsystem of the specified type. This is implementation-specific, because implementations contain data on
  25. /// what they have loaded and how best to get it.
  26. /// </summary>
  27. ///
  28. /// <typeparam name="T">Type of the subsystem to get.</typeparam>
  29. ///
  30. /// <returns>The loaded subsystem, or null if no subsystem found.</returns>
  31. public override T GetLoadedSubsystem<T>()
  32. {
  33. Type subsystemType = typeof(T);
  34. ISubsystem subsystem;
  35. m_SubsystemInstanceMap.TryGetValue(subsystemType, out subsystem);
  36. return subsystem as T;
  37. }
  38. /// <summary>
  39. /// Start a subsystem instance of a given type. Subsystem is assumed to already be loaded from
  40. /// a previous call to CreateSubsystem.
  41. /// </summary>
  42. /// <typeparam name="T">A subclass of <see cref="ISubsystem"/></typeparam>
  43. protected void StartSubsystem<T>() where T : class, ISubsystem
  44. {
  45. T subsystem = GetLoadedSubsystem<T>();
  46. if (subsystem != null)
  47. subsystem.Start();
  48. }
  49. /// <summary>
  50. /// Stop a subsystem instance of a given type. Subsystem is assumed to already be loaded from
  51. /// a previous call to CreateSubsystem.
  52. /// </summary>
  53. /// <typeparam name="T">A subclass of <see cref="ISubsystem"/></typeparam>
  54. protected void StopSubsystem<T>() where T : class, ISubsystem
  55. {
  56. T subsystem = GetLoadedSubsystem<T>();
  57. if (subsystem != null)
  58. subsystem.Stop();
  59. }
  60. /// <summary>
  61. /// Destroy a subsystem instance of a given type. Subsystem is assumed to already be loaded from
  62. /// a previous call to CreateSubsystem.
  63. /// </summary>
  64. /// <typeparam name="T">A subclass of <see cref="ISubsystem"/></typeparam>
  65. protected void DestroySubsystem<T>() where T : class, ISubsystem
  66. {
  67. T subsystem = GetLoadedSubsystem<T>();
  68. if (subsystem != null)
  69. subsystem.Destroy();
  70. }
  71. /// <summary>
  72. /// Creates a subsystem with a given list of descriptors and a specific subsystem id.
  73. /// </summary>
  74. /// <typeparam name="TDescriptor">The descriptor type being passed in.</typeparam>
  75. /// <typeparam name="TSubsystem">The subsystem type being requested.</typeparam>
  76. /// <param name="descriptors">List of TDescriptor instances to use for subsystem matching.</param>
  77. /// <param name="id">The identifier key of the particualr subsystem implementation being requested.</param>
  78. protected void CreateSubsystem<TDescriptor, TSubsystem>(List<TDescriptor> descriptors, string id)
  79. where TDescriptor : ISubsystemDescriptor
  80. where TSubsystem : ISubsystem
  81. {
  82. if (descriptors == null)
  83. throw new ArgumentNullException("descriptors");
  84. SubsystemManager.GetSubsystemDescriptors<TDescriptor>(descriptors);
  85. if (descriptors.Count > 0)
  86. {
  87. foreach (var descriptor in descriptors)
  88. {
  89. ISubsystem subsys = null;
  90. if (String.Compare(descriptor.id, id, true) == 0)
  91. {
  92. subsys = descriptor.Create();
  93. }
  94. if (subsys != null)
  95. {
  96. m_SubsystemInstanceMap[typeof(TSubsystem)] = subsys;
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// Override of <see cref="Deinitialize"/> to provide for clearing the instance map.true
  104. ///
  105. /// If you override <see cref="Deinitialize"/> in your subclass, you must call the base
  106. /// implementation to allow the instance map tp be cleaned up correctly.
  107. /// </summary>
  108. ///
  109. /// <returns>True if de-initialization was successful.</returns>
  110. public override bool Deinitialize()
  111. {
  112. m_SubsystemInstanceMap.Clear();
  113. return base.Deinitialize();
  114. }
  115. #if UNITY_EDITOR
  116. virtual public void WasAssignedToBuildTarget(BuildTargetGroup buildTargetGroup)
  117. {
  118. }
  119. virtual public void WasUnassignedFromBuildTarget(BuildTargetGroup buildTargetGroup)
  120. {
  121. }
  122. #endif
  123. }
  124. }