using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using Sirenix.OdinInspector; using QFramework; public class CameraRayCastManager : MonoSingleton { [LabelText("模型类型检测")] [SerializeField] private bool m_IsEventSystem = true; //活跃状态检测物体 public static int RayCastActivelayer = 25; private Camera mainCamera; public Color hoverColor = Color.blue; public RayCastItem currentRayCastItem; private void Awake() { mainCamera = Camera.main; } private void Update() { if (m_IsEventSystem) { if (EventSystem.current.IsPointerOverGameObject()) return; } RaycastHit hit; //活跃状态RaycastItem检测 if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 50, 1 << RayCastActivelayer)) { CheckRayCastItem(hit,true); } //非活跃状态检测 else if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 50)) { CheckRayCastItem(hit,false); } else if (currentRayCastItem) { currentRayCastItem.CloseHighlighter(); currentRayCastItem = null; } } private void CheckRayCastItem(RaycastHit hit,bool openHighlighter) { if (hit.collider.GetComponentInParent() != null) { if (currentRayCastItem) { currentRayCastItem.CloseHighlighter(); } currentRayCastItem = hit.collider.GetComponentInParent(); currentRayCastItem.InitRayCastParents(); if (openHighlighter) { currentRayCastItem.OpenHighlighter(hoverColor); } } } public bool CheckCurrentHoverItem(RayCastItem item) { if (m_IsEventSystem) { if (EventSystem.current.IsPointerOverGameObject()) return false; } if (currentRayCastItem == null) return false; else return currentRayCastItem.Contains(item); } }