| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using Sirenix.OdinInspector;
- using QFramework;
- public class CameraRayCastManager : MonoSingleton<CameraRayCastManager>
- {
- [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<RayCastItem>() != null)
- {
- if (currentRayCastItem)
- {
- currentRayCastItem.CloseHighlighter();
- }
- currentRayCastItem = hit.collider.GetComponentInParent<RayCastItem>();
- 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);
- }
- }
|