OutlineBuilder.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /// <summary>
  10. /// A helper behaviour for managing content of <see cref="OutlineLayerCollection"/> via Unity Editor.
  11. /// </summary>
  12. public sealed class OutlineBuilder : MonoBehaviour
  13. {
  14. #region data
  15. [Serializable]
  16. internal class ContentItem
  17. {
  18. public GameObject Go;
  19. public int LayerIndex;
  20. }
  21. #pragma warning disable 0649
  22. [SerializeField, Tooltip(OutlineResources.OutlineLayerCollectionTooltip)]
  23. private OutlineLayerCollection _outlineLayers;
  24. [SerializeField, HideInInspector]
  25. private List<ContentItem> _content;
  26. #pragma warning restore 0649
  27. #endregion
  28. #region interface
  29. internal List<ContentItem> Content { get => _content; set => _content = value; }
  30. /// <summary>
  31. /// Gets or sets a collection of layers to manage.
  32. /// </summary>
  33. public OutlineLayerCollection OutlineLayers { get => _outlineLayers; set => _outlineLayers = value; }
  34. /// <summary>
  35. /// Clears content of all layers.
  36. /// </summary>
  37. /// <seealso cref="OutlineLayers"/>
  38. public void Clear()
  39. {
  40. _outlineLayers?.ClearLayerContent();
  41. }
  42. #endregion
  43. #region MonoBehaviour
  44. private void OnEnable()
  45. {
  46. if (_outlineLayers && _content != null)
  47. {
  48. foreach (var item in _content)
  49. {
  50. if (item.LayerIndex >= 0 && item.LayerIndex < _outlineLayers.Count && item.Go)
  51. {
  52. _outlineLayers.GetOrAddLayer(item.LayerIndex).Add(item.Go);
  53. }
  54. }
  55. }
  56. }
  57. #if UNITY_EDITOR
  58. private void Reset()
  59. {
  60. var effect = GetComponent<OutlineEffect>();
  61. if (effect)
  62. {
  63. _outlineLayers = effect.OutlineLayersInternal;
  64. }
  65. }
  66. private void OnDestroy()
  67. {
  68. _outlineLayers?.ClearLayerContent();
  69. }
  70. #endif
  71. #endregion
  72. }
  73. }