CVR_TriggerTool.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. namespace ChivaXR.VR
  2. {
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public struct CVR_InteractableTriggerEventArgs
  6. {
  7. public IVR_Interactable interactable;
  8. public CVR_TriggerTool triggerTool;
  9. public CVR_ToolType toolType;
  10. public Collider[] triggerObjects;
  11. }
  12. /// <summary>
  13. /// 工具的触发检测参数
  14. /// </summary>
  15. public struct ToolTriggerInfos
  16. {
  17. public float trigger_DetectionRange; //触发检测范围
  18. public CVR_TriggerList.CheckTypes toolCheckTypes;
  19. public string[] toolCheckInfos;
  20. public CVR_ButtonPressTypes buttonPressType;
  21. public CVR_ButtonTypes buttonType;
  22. }
  23. /// <summary>
  24. /// Trigger
  25. /// </summary>
  26. /// <param name="sender">this object</param>
  27. /// <param name="e"><see cref="InteractableObjectEventArgs"/></param>
  28. public delegate void CVRInteractableTriggerEventHandler(object sender, CVR_InteractableTriggerEventArgs e);
  29. /// <summary>
  30. /// 交互工具触发器
  31. /// </summary>
  32. [RequireComponent(typeof(IVR_Interactable))]
  33. public class CVR_TriggerTool : MonoBehaviour
  34. {
  35. private IVR_Interactable interactable;
  36. private GameObject currentHand;
  37. private List<Collider> _colliders = new List<Collider>();
  38. private CVR_TriggerList triggerList;
  39. public CVR_TriggerList TriggerList
  40. {
  41. get
  42. {
  43. if (triggerList == null)
  44. {
  45. triggerList = this.gameObject.AddComponent<CVR_TriggerList>();
  46. }
  47. return triggerList;
  48. }
  49. }
  50. /// <summary>
  51. /// 触发点
  52. /// </summary>
  53. public Transform triggerPoint;
  54. /// <summary>
  55. /// 触发范围
  56. /// </summary>
  57. public float triggerRange = 0.1f;
  58. /// <summary>
  59. /// 按键触发类型
  60. /// </summary>
  61. public CVR_ButtonPressTypes buttonPressType = CVR_ButtonPressTypes.PressDown;
  62. /// <summary>
  63. /// 按键类型
  64. /// </summary>
  65. public CVR_ButtonTypes buttonType = CVR_ButtonTypes.Trigger;
  66. public event CVRInteractableTriggerEventHandler InteractableTrigger;
  67. private void Awake()
  68. {
  69. interactable = this.GetComponent<IVR_Interactable>();
  70. CVR_ToolManager.Instance.RegisterTriggerTool(interactable.GetToolType(), this);
  71. }
  72. private void Update()
  73. {
  74. if (interactable.CVR_IsGrabbed() && interactable.CVR_CheckButtonState(buttonType, buttonPressType))
  75. {
  76. if (triggerPoint == null)
  77. {
  78. Debug.LogError(this.transform.name + "未添加TriggerPoint");
  79. return;
  80. }
  81. //射线检测
  82. RayCheck(triggerPoint.position, triggerRange);
  83. }
  84. }
  85. /// <summary>
  86. /// 球形射线检测
  87. /// </summary>
  88. /// <param name="center"></param>
  89. /// <param name="radius"></param>
  90. /// <returns></returns>
  91. private void RayCheck(Vector3 center, float radius)
  92. {
  93. _colliders.Clear();
  94. Collider[] hitColliders = Physics.OverlapSphere(center, radius);
  95. for (int i = 0; i < hitColliders.Length; i++)
  96. {
  97. if (CVR_TriggerList.Check(hitColliders[i].gameObject, TriggerList))
  98. {
  99. _colliders.Add(hitColliders[i]);
  100. }
  101. }
  102. OnInteractableObjectTrigger(this,
  103. new CVR_InteractableTriggerEventArgs()
  104. {
  105. interactable = this.interactable,
  106. triggerTool = this,
  107. toolType = interactable.GetToolType(),
  108. triggerObjects = _colliders.ToArray()
  109. });
  110. }
  111. public virtual void OnInteractableObjectTrigger(object sender, CVR_InteractableTriggerEventArgs e)
  112. {
  113. if (InteractableTrigger != null)
  114. {
  115. InteractableTrigger(this, e);
  116. }
  117. }
  118. /// <summary>
  119. ///重置工具触发参数
  120. /// </summary>
  121. /// <param name="triggerToolInfo"></param>
  122. public void ResetTriggerInfo(ToolTriggerInfos triggerToolInfo)
  123. {
  124. triggerRange = triggerToolInfo.trigger_DetectionRange;
  125. TriggerList.checkType = triggerToolInfo.toolCheckTypes;
  126. TriggerList.identifiers.Clear();
  127. TriggerList.identifiers.AddRange(triggerToolInfo.toolCheckInfos);
  128. buttonPressType = triggerToolInfo.buttonPressType;
  129. buttonType = triggerToolInfo.buttonType;
  130. }
  131. }
  132. }