VolumeStack.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.Rendering
  4. {
  5. /// <summary>
  6. /// Holds the state of a Volume blending update. A global stack is
  7. /// available by default in <see cref="VolumeManager"/> but you can also create your own using
  8. /// <see cref="VolumeManager.CreateStack"/> if you need to update the manager with specific
  9. /// settings and store the results for later use.
  10. /// </summary>
  11. public sealed class VolumeStack : IDisposable
  12. {
  13. // Holds the state of _all_ component types you can possibly add on volumes
  14. internal Dictionary<Type, VolumeComponent> components;
  15. internal VolumeStack()
  16. {
  17. }
  18. internal void Reload(IEnumerable<Type> baseTypes)
  19. {
  20. if (components == null)
  21. components = new Dictionary<Type, VolumeComponent>();
  22. else
  23. components.Clear();
  24. foreach (var type in baseTypes)
  25. {
  26. var inst = (VolumeComponent)ScriptableObject.CreateInstance(type);
  27. components.Add(type, inst);
  28. }
  29. }
  30. /// <summary>
  31. /// Gets the current state of the <see cref="VolumeComponent"/> of type <typeparamref name="T"/>
  32. /// in the stack.
  33. /// </summary>
  34. /// <typeparam name="T">A type of <see cref="VolumeComponent"/>.</typeparam>
  35. /// <returns>The current state of the <see cref="VolumeComponent"/> of type <typeparamref name="T"/>
  36. /// in the stack.</returns>
  37. public T GetComponent<T>()
  38. where T : VolumeComponent
  39. {
  40. var comp = GetComponent(typeof(T));
  41. return (T)comp;
  42. }
  43. /// <summary>
  44. /// Gets the current state of the <see cref="VolumeComponent"/> of the specified type in the
  45. /// stack.
  46. /// </summary>
  47. /// <param name="type">The type of <see cref="VolumeComponent"/> to look for.</param>
  48. /// <returns>The current state of the <see cref="VolumeComponent"/> of the specified type,
  49. /// or <c>null</c> if the type is invalid.</returns>
  50. public VolumeComponent GetComponent(Type type)
  51. {
  52. components.TryGetValue(type, out var comp);
  53. return comp;
  54. }
  55. /// <summary>
  56. /// Cleans up the content of this stack. Once a <c>VolumeStack</c> is disposed, it souldn't
  57. /// be used anymore.
  58. /// </summary>
  59. public void Dispose()
  60. {
  61. foreach (var component in components)
  62. CoreUtils.Destroy(component.Value);
  63. components.Clear();
  64. }
  65. }
  66. }