CameraRayCastManager.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using EPOOutline;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. public class CameraRayCastManager : MonoSingleton<CameraRayCastManager>
  7. {
  8. //活跃状态检测物体
  9. public static int RayCastActivelayer = 25;
  10. private Camera mainCamera;
  11. public RayCastItem currentRayCastItem;
  12. private void Awake()
  13. {
  14. Camera[] camera = FindObjectsOfType<Camera>();
  15. for (int i = 0; i < camera.Length; i++)
  16. {
  17. if (camera[i].isActiveAndEnabled && camera[i].tag == "MainCamera")
  18. {
  19. mainCamera = camera[i];
  20. break;
  21. }
  22. }
  23. if (mainCamera == null) Debug.LogError("未查找到主摄像机");
  24. }
  25. private void Update()
  26. {
  27. if (mainCamera == null) return;
  28. //if (EventSystem.current.IsPointerOverGameObject()) return;
  29. RaycastHit hit;
  30. //活跃状态RaycastItem检测
  31. if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 50, 1 << RayCastActivelayer))
  32. {
  33. CheckRayCastItem(hit);
  34. }
  35. //非活跃状态检测
  36. else if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 50))
  37. {
  38. CheckRayCastItem(hit);
  39. }
  40. else if (currentRayCastItem)
  41. {
  42. currentRayCastItem.CloseHighlighter();
  43. currentRayCastItem = null;
  44. }
  45. }
  46. private void CheckRayCastItem(RaycastHit hit)
  47. {
  48. if (hit.collider.GetComponentInParent<RayCastItem>() != null)
  49. {
  50. if (currentRayCastItem)
  51. {
  52. currentRayCastItem.CloseHighlighter();
  53. }
  54. currentRayCastItem = hit.collider.GetComponentInParent<RayCastItem>();
  55. currentRayCastItem.InitRayCastParents();
  56. //考核模式关闭划过高亮
  57. if (OperateSetting.Instance.m_CurrentOperationMode == OperationMode.Exam)
  58. {
  59. return;
  60. }
  61. currentRayCastItem.OpenHighlighter(OutLineType.hover);
  62. }
  63. else if (currentRayCastItem)
  64. { //考核模式关闭划过高亮
  65. if (OperateSetting.Instance.m_CurrentOperationMode == OperationMode.Exam)
  66. {
  67. currentRayCastItem = null;
  68. return;
  69. }
  70. currentRayCastItem.CloseHighlighter();
  71. currentRayCastItem = null;
  72. }
  73. }
  74. public bool CheckCurrentHoverItem(RayCastItem item)
  75. {
  76. if (currentRayCastItem == null)
  77. return false;
  78. else
  79. return currentRayCastItem.Contains(item);
  80. }
  81. }