66 lines
1.7 KiB
C#
66 lines
1.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace Studio
|
|
{
|
|
public class MouseObserver : MonoBehaviour
|
|
{
|
|
public Action onMouseExitClick;
|
|
|
|
public bool isPointerOverSelf;
|
|
|
|
private RectTransform rectTransform;
|
|
private bool prevPointerOverSelf = false;
|
|
|
|
private void Awake()
|
|
{
|
|
rectTransform = GetComponent<RectTransform>();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (rectTransform == null)
|
|
{
|
|
Debug.Log($"Not Found RectTransform in MouseObserver. {gameObject}");
|
|
Destroy(this);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// 마우스가 자신의 UI(RectTransform) 영역 위에 있는지 판별
|
|
bool isOver = RectTransformUtility.RectangleContainsScreenPoint(
|
|
rectTransform,
|
|
Input.mousePosition,
|
|
GetEventCamera()
|
|
);
|
|
|
|
if (isOver && !prevPointerOverSelf)
|
|
{
|
|
isPointerOverSelf = true;
|
|
}
|
|
else if (!isOver && prevPointerOverSelf)
|
|
{
|
|
isPointerOverSelf = false;
|
|
}
|
|
|
|
prevPointerOverSelf = isOver;
|
|
|
|
if (Input.GetMouseButtonDown(0) && !isPointerOverSelf)
|
|
{
|
|
onMouseExitClick?.Invoke();
|
|
}
|
|
}
|
|
|
|
// UI용 카메라 반환 (없으면 null)
|
|
private Camera GetEventCamera()
|
|
{
|
|
if (EventSystem.current != null && EventSystem.current.currentInputModule is StandaloneInputModule sim)
|
|
{
|
|
return sim.inputOverride?.mousePresent == true ? Camera.main : null;
|
|
}
|
|
return Camera.main;
|
|
}
|
|
}
|
|
} |