RayCastItem.cs 2.6 KB

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