RayCastItem.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using HighlightingSystem;
  5. public class RayCastItem : MonoBehaviour
  6. {
  7. private Highlighter highlighter;
  8. /// <summary>
  9. /// 自身及父级列表
  10. /// </summary>
  11. private List<RayCastItem> parentList;
  12. /// <summary>
  13. /// 射线检测活跃状态
  14. /// </summary>
  15. private bool activeState = false;
  16. public bool ActiveState
  17. {
  18. get
  19. {
  20. return activeState;
  21. }
  22. set
  23. {
  24. if (activeState != value)
  25. {
  26. activeState = value;
  27. InitChildCollider();
  28. foreach (var item in childCollider)
  29. {
  30. item.gameObject.layer = activeState ? CameraRayCastManager.RayCastActivelayer : localLayer;
  31. }
  32. }
  33. }
  34. }
  35. public bool HighlighterConstant
  36. {
  37. get
  38. {
  39. if (highlighter == null)
  40. return false;
  41. else
  42. return highlighter.constant;
  43. }
  44. }
  45. private int localLayer;
  46. private Collider[] childCollider;
  47. private void Awake()
  48. {
  49. highlighter = this.GetComponent<Highlighter>() == null
  50. ? this.gameObject.AddComponent<Highlighter>()
  51. : this.GetComponent<Highlighter>();
  52. highlighter.overlay = true;
  53. localLayer = this.gameObject.layer;
  54. }
  55. private void InitChildCollider()
  56. {
  57. if (childCollider == null)
  58. {
  59. childCollider = this.GetComponentsInChildren<Collider>();
  60. }
  61. }
  62. public void InitRayCastParents()
  63. {
  64. if (parentList == null)
  65. {
  66. CreatParentLists();
  67. }
  68. }
  69. public void CreatParentLists()
  70. {
  71. parentList = new List<RayCastItem>();
  72. parentList.AddRange(this.transform.GetComponentsInParent<RayCastItem>());
  73. }
  74. public bool Contains(RayCastItem item)
  75. {
  76. if (parentList == null)
  77. {
  78. return this == item;
  79. }
  80. return parentList.Contains(item);
  81. }
  82. public void OpenHighlighter(Color color)
  83. {
  84. foreach (var item in this.GetComponentsInChildren<Highlighter>())
  85. {
  86. item.ConstantOn(color, 0);
  87. }
  88. }
  89. public void CloseHighlighter()
  90. {
  91. foreach (var item in this.GetComponentsInChildren<Highlighter>())
  92. {
  93. item.ConstantOff(0);
  94. }
  95. }
  96. public void OpenHighlighterFlashing()
  97. {
  98. foreach (var item in this.GetComponentsInChildren<Highlighter>())
  99. {
  100. //item.FlashingOn(Color.blue, Color.cyan, 2);
  101. item.tweenDuration = 0.3f;
  102. item.tweenRepeatCount = 2; //闪烁次数
  103. item.tweenGradient.SetKeys(
  104. new GradientColorKey[]
  105. {
  106. new GradientColorKey { color=new Color(1,0,0),time=0.5f},
  107. new GradientColorKey { color=new Color(0,1,0),time=1.5f}
  108. },
  109. new GradientAlphaKey[]
  110. {
  111. new GradientAlphaKey{alpha=255},
  112. new GradientAlphaKey{alpha=255,time=1.5f}
  113. });
  114. item.TweenStart();//闪烁
  115. }
  116. }
  117. public void CloseHighlighterFlashing()
  118. {
  119. foreach (var item in this.GetComponentsInChildren<Highlighter>())
  120. {
  121. //item.FlashingOff();
  122. item.TweenStop();
  123. }
  124. }
  125. }