暫無描述
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.

DebugDisplaySettings.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.Rendering
  4. {
  5. /// <summary>
  6. /// Templated class for <see cref="IDebugDisplaySettings"/>
  7. /// </summary>
  8. /// <typeparam name="T">The specific type of Debug Display Settings that is inheriting from the IDebugDisplaySettings interface.</typeparam>
  9. public abstract class DebugDisplaySettings<T> : IDebugDisplaySettings
  10. where T : IDebugDisplaySettings, new()
  11. {
  12. class IDebugDisplaySettingsDataComparer : IEqualityComparer<IDebugDisplaySettingsData>
  13. {
  14. public bool Equals(IDebugDisplaySettingsData x, IDebugDisplaySettingsData y)
  15. {
  16. if (ReferenceEquals(x, y))
  17. return true;
  18. if (x == null || y == null)
  19. return false;
  20. return x.GetType() == y.GetType();
  21. }
  22. public int GetHashCode(IDebugDisplaySettingsData obj)
  23. {
  24. // Define your custom hashing logic based on the properties you want to include.
  25. unchecked
  26. {
  27. int hash = 17;
  28. hash = hash * 23 + obj.GetType().GetHashCode();
  29. return hash;
  30. }
  31. }
  32. }
  33. /// <summary>
  34. /// The set of <see cref="IDebugDisplaySettingsData"/> containing the settings for this debug display
  35. /// </summary>
  36. protected readonly HashSet<IDebugDisplaySettingsData> m_Settings = new HashSet<IDebugDisplaySettingsData>(new IDebugDisplaySettingsDataComparer());
  37. private static readonly Lazy<T> s_Instance = new Lazy<T>(() =>
  38. {
  39. var instance = new T();
  40. instance.Reset();
  41. return instance;
  42. });
  43. /// <summary>
  44. /// The singleton instance that contains the current settings of Rendering Debugger.
  45. /// </summary>
  46. public static T Instance => s_Instance.Value;
  47. #region IDebugDisplaySettingsQuery
  48. /// <summary>
  49. /// Returns true if any of the debug settings are currently active.
  50. /// </summary>
  51. public virtual bool AreAnySettingsActive
  52. {
  53. get
  54. {
  55. foreach (IDebugDisplaySettingsData setting in m_Settings)
  56. {
  57. if (setting.AreAnySettingsActive)
  58. return true;
  59. }
  60. return false;
  61. }
  62. }
  63. /// <summary>
  64. /// Checks whether the current state of these settings allows post-processing.
  65. /// </summary>
  66. public virtual bool IsPostProcessingAllowed
  67. {
  68. get
  69. {
  70. // Only enable post-processing if we aren't using certain debug-views.
  71. bool postProcessingAllowed = true;
  72. foreach (IDebugDisplaySettingsData setting in m_Settings)
  73. postProcessingAllowed &= setting.IsPostProcessingAllowed;
  74. return postProcessingAllowed;
  75. }
  76. }
  77. /// <summary>
  78. /// Returns true if lighting is active for current state of debug settings.
  79. /// </summary>
  80. public virtual bool IsLightingActive
  81. {
  82. get
  83. {
  84. bool lightingActive = true;
  85. foreach (IDebugDisplaySettingsData setting in m_Settings)
  86. lightingActive &= setting.IsLightingActive;
  87. return lightingActive;
  88. }
  89. }
  90. #endregion
  91. /// <summary>
  92. /// Adds a new <see cref="TData"/> to this settings
  93. /// </summary>
  94. /// <typeparam name="TData">The type of <see cref="TData"/> to be added</typeparam>
  95. /// <param name="newData">The <see cref="TData"/> to be added</param>
  96. /// <returns>The type of <see cref="TData"/> that has been added</returns>
  97. protected TData Add<TData>(TData newData)
  98. where TData : IDebugDisplaySettingsData
  99. {
  100. m_Settings.Add(newData);
  101. return newData;
  102. }
  103. /// <inheritdoc/>
  104. IDebugDisplaySettingsData IDebugDisplaySettings.Add(IDebugDisplaySettingsData newData)
  105. {
  106. m_Settings.Add(newData);
  107. return newData;
  108. }
  109. /// <summary>
  110. /// Executes an action for each element
  111. /// </summary>
  112. /// <param name="onExecute">The action to be executed on each element in the Debug Display Settings data.</param>
  113. public void ForEach(Action<IDebugDisplaySettingsData> onExecute)
  114. {
  115. foreach (IDebugDisplaySettingsData setting in m_Settings)
  116. {
  117. onExecute(setting);
  118. }
  119. }
  120. /// <summary>
  121. /// Reset the stored debug settings
  122. /// </summary>
  123. public virtual void Reset()
  124. {
  125. m_Settings.Clear();
  126. }
  127. /// <summary>
  128. /// Attempts to get the color that should be used to clear the screen according to current debug settings.
  129. /// </summary>
  130. /// <param name="color">A reference to the screen clear color to use.</param>
  131. /// <returns>True if the color reference was updated, and false otherwise.</returns>
  132. public virtual bool TryGetScreenClearColor(ref Color color)
  133. {
  134. foreach (IDebugDisplaySettingsData setting in m_Settings)
  135. {
  136. if (setting.TryGetScreenClearColor(ref color))
  137. return true;
  138. }
  139. return false;
  140. }
  141. }
  142. }