CameraRayCastManager.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using Sirenix.OdinInspector;
  6. using QFramework;
  7. public class CameraRayCastManager : MonoSingleton<CameraRayCastManager>
  8. {
  9. [LabelText("模型类型检测")]
  10. [SerializeField]
  11. private bool m_IsEventSystem = true;
  12. //活跃状态检测物体
  13. public static int RayCastActivelayer = 25;
  14. private Camera mainCamera;
  15. public Color hoverColor = Color.blue;
  16. public RayCastItem currentRayCastItem;
  17. private void Awake()
  18. {
  19. mainCamera = Camera.main;
  20. }
  21. private void Update()
  22. {
  23. if (m_IsEventSystem)
  24. {
  25. if (EventSystem.current.IsPointerOverGameObject()) return;
  26. }
  27. RaycastHit hit;
  28. //活跃状态RaycastItem检测
  29. if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 50, 1 << RayCastActivelayer))
  30. {
  31. CheckRayCastItem(hit,true);
  32. }
  33. //非活跃状态检测
  34. else if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 50))
  35. {
  36. CheckRayCastItem(hit,false);
  37. }
  38. else if (currentRayCastItem)
  39. {
  40. currentRayCastItem.CloseHighlighter();
  41. currentRayCastItem = null;
  42. }
  43. }
  44. private void CheckRayCastItem(RaycastHit hit,bool openHighlighter)
  45. {
  46. if (hit.collider.GetComponentInParent<RayCastItem>() != null)
  47. {
  48. if (currentRayCastItem)
  49. {
  50. currentRayCastItem.CloseHighlighter();
  51. }
  52. currentRayCastItem = hit.collider.GetComponentInParent<RayCastItem>();
  53. currentRayCastItem.InitRayCastParents();
  54. if (openHighlighter)
  55. {
  56. currentRayCastItem.OpenHighlighter(hoverColor);
  57. }
  58. }
  59. }
  60. public bool CheckCurrentHoverItem(RayCastItem item)
  61. {
  62. if (m_IsEventSystem)
  63. {
  64. if (EventSystem.current.IsPointerOverGameObject()) return false;
  65. }
  66. if (currentRayCastItem == null)
  67. return false;
  68. else
  69. return currentRayCastItem.Contains(item);
  70. }
  71. }