60 lines
1.6 KiB
C#
60 lines
1.6 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 Update()
|
|||
|
|
{
|
|||
|
|
if (rectTransform == null)
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
// <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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|