using EPOOutline; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class CameraRayCastManager : MonoSingleton { //活跃状态检测物体 public static int RayCastActivelayer = 25; private Camera mainCamera; public RayCastItem currentRayCastItem; private void Awake() { Camera[] camera = FindObjectsOfType(); for (int i = 0; i < camera.Length; i++) { if (camera[i].isActiveAndEnabled && camera[i].tag == "MainCamera") { mainCamera = camera[i]; break; } } if (mainCamera == null) Debug.LogError("未查找到主摄像机"); } private void Update() { if (mainCamera == null) return; //if (EventSystem.current.IsPointerOverGameObject()) return; RaycastHit hit; //活跃状态RaycastItem检测 if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 50, 1 << RayCastActivelayer)) { CheckRayCastItem(hit); } //非活跃状态检测 else if (Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), out hit, 50)) { CheckRayCastItem(hit); } else if (currentRayCastItem) { currentRayCastItem.CloseHighlighter(); currentRayCastItem = null; } } private void CheckRayCastItem(RaycastHit hit) { if (hit.collider.GetComponentInParent() != null) { if (currentRayCastItem) { currentRayCastItem.CloseHighlighter(); } currentRayCastItem = hit.collider.GetComponentInParent(); currentRayCastItem.InitRayCastParents(); //考核模式关闭划过高亮 if (OperateSetting.Instance.m_CurrentOperationMode == OperationMode.Exam) { return; } currentRayCastItem.OpenHighlighter(OutLineType.hover); } else if (currentRayCastItem) { //考核模式关闭划过高亮 if (OperateSetting.Instance.m_CurrentOperationMode == OperationMode.Exam) { currentRayCastItem = null; return; } currentRayCastItem.CloseHighlighter(); currentRayCastItem = null; } } public bool CheckCurrentHoverItem(RayCastItem item) { if (currentRayCastItem == null) return false; else return currentRayCastItem.Contains(item); } }