RayCastItem.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using EPOOutline;
  2. using HighlightingSystem;
  3. using QFramework;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. public class RayCastItem : MonoBehaviour
  8. {
  9. private Outlinable outLinable;
  10. /// <summary>
  11. /// 自身及父级列表
  12. /// </summary>
  13. private List<RayCastItem> parentList;
  14. /// <summary>
  15. /// 射线检测活跃状态
  16. /// </summary>
  17. private bool activeState = false;
  18. public bool ActiveState
  19. {
  20. get
  21. {
  22. return activeState;
  23. }
  24. set
  25. {
  26. if (activeState != value)
  27. {
  28. activeState = value;
  29. InitChildCollider();
  30. foreach (var item in childCollider)
  31. {
  32. item.gameObject.layer = activeState ? CameraRayCastManager.RayCastActivelayer : localLayer;
  33. }
  34. }
  35. }
  36. }
  37. public bool HighlighterConstant
  38. {
  39. get
  40. {
  41. if (outLinable == null)
  42. return false;
  43. else
  44. return outLinable.isActiveAndEnabled;
  45. }
  46. }
  47. private int localLayer;
  48. private Collider[] childCollider;
  49. private void Awake()
  50. {
  51. outLinable = this.GetComponent<Outlinable>() == null
  52. ? this.gameObject.AddComponent<Outlinable>()
  53. : this.GetComponent<Outlinable>();
  54. outLinable.AddAllChildRenderersToRenderingList();
  55. outLinable.enabled = false;
  56. localLayer = this.gameObject.layer;
  57. }
  58. private void InitChildCollider()
  59. {
  60. if (childCollider == null)
  61. {
  62. childCollider = this.GetComponentsInChildren<Collider>();
  63. }
  64. }
  65. public void InitRayCastParents()
  66. {
  67. if (parentList == null)
  68. {
  69. CreatParentLists();
  70. }
  71. }
  72. public void CreatParentLists()
  73. {
  74. parentList = new List<RayCastItem>();
  75. parentList.AddRange(this.transform.GetComponentsInParent<RayCastItem>());
  76. }
  77. public bool Contains(RayCastItem item)
  78. {
  79. if (parentList == null)
  80. {
  81. return this == item;
  82. }
  83. return parentList.Contains(item);
  84. }
  85. public void OpenHighlighter(OutLineType outLineType)
  86. {
  87. foreach (var item in this.GetComponentsInChildren<Outlinable>())
  88. {
  89. //根据现况类型切换高亮预设
  90. OutLineManager.Instance.ResetOutLinableByType(item, outLineType);
  91. item.enabled = true;
  92. }
  93. }
  94. public void OpenHighlight(Color color)
  95. {
  96. foreach (var item in this.GetComponentsInChildren<Highlighter>())
  97. {
  98. item.ConstantOn(color);
  99. }
  100. }
  101. public void CloseHighlighter()
  102. {
  103. foreach (var item in this.GetComponentsInChildren<Outlinable>())
  104. {
  105. item.enabled = false;
  106. }
  107. }
  108. }