PostProcessProfile.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.Rendering.PostProcessing
  4. {
  5. /// <summary>
  6. /// An asset holding a set of post-processing settings to use with a <see cref="PostProcessVolume"/>.
  7. /// </summary>
  8. /// <seealso cref="PostProcessVolume"/>
  9. public sealed class PostProcessProfile : ScriptableObject
  10. {
  11. /// <summary>
  12. /// A list of all settings stored in this profile.
  13. /// </summary>
  14. [Tooltip("A list of all settings currently stored in this profile.")]
  15. public List<PostProcessEffectSettings> settings = new List<PostProcessEffectSettings>();
  16. // Editor only, doesn't have any use outside of it
  17. [NonSerialized]
  18. public bool isDirty = true;
  19. void OnEnable()
  20. {
  21. // Make sure every setting is valid. If a profile holds a script that doesn't exist
  22. // anymore, nuke it to keep the profile clean. Note that if you delete a script that is
  23. // currently in use in a profile you'll still get a one-time error in the console, it's
  24. // harmless and happens because Unity does a redraw of the editor (and thus the current
  25. // frame) before the recompilation step.
  26. settings.RemoveAll(x => x == null);
  27. }
  28. /// <summary>
  29. /// Adds settings for an effect to the profile.
  30. /// </summary>
  31. /// <typeparam name="T">A type of <see cref="PostProcessEffectSettings"/></typeparam>
  32. /// <returns>The instance created from the given type</returns>
  33. /// <seealso cref="PostProcessEffectSettings"/>
  34. public T AddSettings<T>()
  35. where T : PostProcessEffectSettings
  36. {
  37. return (T)AddSettings(typeof(T));
  38. }
  39. /// <summary>
  40. /// Adds settings for an effect to the profile.
  41. /// </summary>
  42. /// <param name="type">A type of <see cref="PostProcessEffectSettings"/></param>
  43. /// <returns>The instance created from the given type</returns>
  44. /// <seealso cref="PostProcessEffectSettings"/>
  45. public PostProcessEffectSettings AddSettings(Type type)
  46. {
  47. if (HasSettings(type))
  48. throw new InvalidOperationException("Effect already exists in the stack");
  49. var effect = (PostProcessEffectSettings)CreateInstance(type);
  50. effect.hideFlags = HideFlags.HideInInspector | HideFlags.HideInHierarchy;
  51. effect.name = type.Name;
  52. effect.enabled.value = true;
  53. settings.Add(effect);
  54. isDirty = true;
  55. return effect;
  56. }
  57. /// <summary>
  58. /// Adds settings for an effect to the profile.
  59. /// </summary>
  60. /// <param name="effect">An instance of <see cref="PostProcessEffectSettings"/></param>
  61. /// <returns>The given effect instance</returns>
  62. /// <seealso cref="PostProcessEffectSettings"/>
  63. public PostProcessEffectSettings AddSettings(PostProcessEffectSettings effect)
  64. {
  65. if (HasSettings(settings.GetType()))
  66. throw new InvalidOperationException("Effect already exists in the stack");
  67. settings.Add(effect);
  68. isDirty = true;
  69. return effect;
  70. }
  71. /// <summary>
  72. /// Removes settings for an effect from the profile.
  73. /// </summary>
  74. /// <typeparam name="T">The type to look for and remove from the profile</typeparam>
  75. /// <exception cref="InvalidOperationException">Thrown if the effect doesn't exist in the
  76. /// profile</exception>
  77. public void RemoveSettings<T>()
  78. where T : PostProcessEffectSettings
  79. {
  80. RemoveSettings(typeof(T));
  81. }
  82. /// <summary>
  83. /// Removes settings for an effect from the profile.
  84. /// </summary>
  85. /// <param name="type">The type to look for and remove from the profile</param>
  86. /// <exception cref="InvalidOperationException">Thrown if the effect doesn't exist in the
  87. /// profile</exception>
  88. public void RemoveSettings(Type type)
  89. {
  90. int toRemove = -1;
  91. for (int i = 0; i < settings.Count; i++)
  92. {
  93. if (settings[i].GetType() == type)
  94. {
  95. toRemove = i;
  96. break;
  97. }
  98. }
  99. if (toRemove < 0)
  100. throw new InvalidOperationException("Effect doesn't exist in the profile");
  101. settings.RemoveAt(toRemove);
  102. isDirty = true;
  103. }
  104. /// <summary>
  105. /// Checks if an effect has been added to the profile.
  106. /// </summary>
  107. /// <typeparam name="T">The type to look for</typeparam>
  108. /// <returns><c>true</c> if the effect exists in the profile, <c>false</c> otherwise</returns>
  109. public bool HasSettings<T>()
  110. where T : PostProcessEffectSettings
  111. {
  112. return HasSettings(typeof(T));
  113. }
  114. /// <summary>
  115. /// Checks if an effect has been added to the profile.
  116. /// </summary>
  117. /// <param name="type">The type to look for</param>
  118. /// <returns><c>true</c> if the effect exists in the profile, <c>false</c> otherwise</returns>
  119. public bool HasSettings(Type type)
  120. {
  121. foreach (var setting in settings)
  122. {
  123. if (setting.GetType() == type)
  124. return true;
  125. }
  126. return false;
  127. }
  128. /// <summary>
  129. /// Returns settings for a given effect type.
  130. /// </summary>
  131. /// <typeparam name="T">The type to look for</typeparam>
  132. /// <returns>Settings for the given effect type, <c>null</c> otherwise</returns>
  133. public T GetSetting<T>() where T : PostProcessEffectSettings
  134. {
  135. foreach (var setting in settings)
  136. {
  137. if (setting is T)
  138. return setting as T;
  139. }
  140. return null;
  141. }
  142. /// <summary>
  143. /// Gets settings for a given effect type.
  144. /// </summary>
  145. /// <typeparam name="T">The type to look for</typeparam>
  146. /// <param name="outSetting">When this method returns, contains the value associated with
  147. /// the specified type, if the type is found; otherwise, this parameter will be <c>null</c>.
  148. /// This parameter is passed uninitialized.</param>
  149. /// <returns><c>true</c> if the effect exists in the profile, <c>false</c> otherwise</returns>
  150. public bool TryGetSettings<T>(out T outSetting)
  151. where T : PostProcessEffectSettings
  152. {
  153. var type = typeof(T);
  154. outSetting = null;
  155. foreach (var setting in settings)
  156. {
  157. if (setting.GetType() == type)
  158. {
  159. outSetting = (T)setting;
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. }
  166. }