OutlineRendererCollection.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (C) 2019-2021 Alexander Bogarsukov. All rights reserved.
  2. // See the LICENSE.md file in the project root for more information.
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. namespace UnityFx.Outline
  8. {
  9. internal class OutlineRendererCollection : ICollection<Renderer>
  10. {
  11. #region data
  12. private readonly List<Renderer> _renderers = new List<Renderer>();
  13. private readonly GameObject _go;
  14. #endregion
  15. #region interface
  16. internal OutlineRendererCollection(GameObject go)
  17. {
  18. Debug.Assert(go);
  19. _go = go;
  20. }
  21. internal IReadOnlyList<Renderer> GetList()
  22. {
  23. return _renderers;
  24. }
  25. internal void Reset(bool includeInactive)
  26. {
  27. _go.GetComponentsInChildren(includeInactive, _renderers);
  28. }
  29. internal void Reset(bool includeInactive, int ignoreLayerMask)
  30. {
  31. _renderers.Clear();
  32. if (ignoreLayerMask != 0)
  33. {
  34. var renderers = _go.GetComponentsInChildren<Renderer>(includeInactive);
  35. foreach (var renderer in renderers)
  36. {
  37. if (((1 << renderer.gameObject.layer) & ignoreLayerMask) == 0)
  38. {
  39. _renderers.Add(renderer);
  40. }
  41. }
  42. }
  43. else
  44. {
  45. _go.GetComponentsInChildren(includeInactive, _renderers);
  46. }
  47. }
  48. #endregion
  49. #region ICollection
  50. public int Count => _renderers.Count;
  51. public bool IsReadOnly => false;
  52. public void Add(Renderer renderer)
  53. {
  54. Validate(renderer);
  55. _renderers.Add(renderer);
  56. }
  57. public bool Remove(Renderer renderer)
  58. {
  59. return _renderers.Remove(renderer);
  60. }
  61. public void Clear()
  62. {
  63. _renderers.Clear();
  64. }
  65. public bool Contains(Renderer renderer)
  66. {
  67. return _renderers.Contains(renderer);
  68. }
  69. public void CopyTo(Renderer[] array, int arrayIndex)
  70. {
  71. _renderers.CopyTo(array, arrayIndex);
  72. }
  73. #endregion
  74. #region IEnumerable
  75. public IEnumerator<Renderer> GetEnumerator()
  76. {
  77. return _renderers.GetEnumerator();
  78. }
  79. IEnumerator IEnumerable.GetEnumerator()
  80. {
  81. return _renderers.GetEnumerator();
  82. }
  83. #endregion
  84. #region implementation
  85. private void Validate(Renderer renderer)
  86. {
  87. if (renderer is null)
  88. {
  89. throw new ArgumentNullException(nameof(renderer));
  90. }
  91. if (!renderer.transform.IsChildOf(_go.transform))
  92. {
  93. throw new ArgumentException(string.Format("Only children of the {0} are allowed.", _go.name), nameof(renderer));
  94. }
  95. }
  96. #endregion
  97. }
  98. }