Files
Studio/Assets/Scripts/MouseObserver.cs

66 lines
1.7 KiB
C#
Raw Normal View History

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>();
}
2025-07-01 09:24:38 +09:00
private void OnEnable()
{
if (rectTransform == null)
2025-07-01 09:24:38 +09:00
{
Debug.Log($"Not Found RectTransform in MouseObserver. {gameObject}");
Destroy(this);
}
}
2025-07-01 09:24:38 +09:00
private void Update()
{
// <20><><EFBFBD><EFBFBD><ECBDBA> <20>ڽ<EFBFBD><DABD><EFBFBD> UI(RectTransform) <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20>ִ<EFBFBD><D6B4><EFBFBD> <20>Ǻ<EFBFBD>
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<55><49> ī<>޶<EFBFBD> <20><>ȯ (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 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;
}
}
}