ColliderEventVR.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ColliderEventVR : MonoBehaviour
  5. {
  6. private void Awake()
  7. {
  8. ColliderEventVR.isTouch = false;
  9. ColliderEventVR.isActive = true;
  10. ColliderEventVR.colliderList = new List<GameObject>();
  11. }
  12. private void Update()
  13. {
  14. if (!GameMain.Instance.VRMode)
  15. {
  16. if (NInput.GetMouseButtonDown(0))
  17. {
  18. if (UICamera.Raycast(Input.mousePosition))
  19. {
  20. return;
  21. }
  22. Ray ray = GameMain.Instance.MainCamera.camera.ScreenPointToRay(Input.mousePosition);
  23. RaycastHit raycastHit = default(RaycastHit);
  24. if (!Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, LayerMask.GetMask(new string[]
  25. {
  26. LayerMask.LayerToName(base.gameObject.layer)
  27. })) || raycastHit.transform != base.transform)
  28. {
  29. return;
  30. }
  31. if (this.onMouseDown != null)
  32. {
  33. this.onMouseDown();
  34. }
  35. return;
  36. }
  37. else if (NInput.GetMouseButtonUp(0))
  38. {
  39. if (UICamera.Raycast(Input.mousePosition))
  40. {
  41. return;
  42. }
  43. Ray ray2 = GameMain.Instance.MainCamera.camera.ScreenPointToRay(Input.mousePosition);
  44. RaycastHit raycastHit2 = default(RaycastHit);
  45. if (!Physics.Raycast(ray2, out raycastHit2, float.PositiveInfinity, LayerMask.GetMask(new string[]
  46. {
  47. LayerMask.LayerToName(base.gameObject.layer)
  48. })) || raycastHit2.transform != base.transform)
  49. {
  50. return;
  51. }
  52. if (this.onMouseUp != null)
  53. {
  54. this.onMouseUp();
  55. }
  56. return;
  57. }
  58. }
  59. }
  60. private void FixedUpdate()
  61. {
  62. if (GameMain.Instance.VRMode && ColliderEventVR.isActive && ColliderEventVR.isTouch && ColliderEventVR.colliderList.Count > 0 && ColliderEventVR.colliderList[0] == base.gameObject)
  63. {
  64. ColliderEventVR.isActive = false;
  65. if (this.onMouseDown != null)
  66. {
  67. this.onMouseDown();
  68. }
  69. }
  70. }
  71. private void OnTriggerEnter(Collider collider)
  72. {
  73. if (!ColliderEventVR.colliderList.Contains(base.gameObject))
  74. {
  75. ColliderEventVR.colliderList.Add(base.gameObject);
  76. }
  77. if (GameMain.Instance.VRMode && !ColliderEventVR.isTouch && collider.gameObject.layer == LayerMask.NameToLayer("OvrGrabHand"))
  78. {
  79. ColliderEventVR.isTouch = true;
  80. }
  81. }
  82. private void OnTriggerExit(Collider collider)
  83. {
  84. if (ColliderEventVR.colliderList.Contains(base.gameObject))
  85. {
  86. ColliderEventVR.colliderList.Remove(base.gameObject);
  87. }
  88. if (ColliderEventVR.colliderList.Count <= 0)
  89. {
  90. ColliderEventVR.isActive = true;
  91. ColliderEventVR.isTouch = false;
  92. }
  93. }
  94. public Action onMouseDown;
  95. public Action onMouseUp;
  96. private static bool isTouch;
  97. private static bool isActive = true;
  98. private static List<GameObject> colliderList;
  99. }