判断鼠标是否移到指定UI上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Tmouse : MonoBehaviour
{
    private RectTransform image;
    public bool mouse;
    void Start()
    {
        mouse=false;
        image = GetComponent<RectTransform>();
    }
    void Update()
    {
        mouse = RectTransformUtility.RectangleContainsScreenPoint(image, Input.mousePosition);
        Debug.Log("鼠标在图片上");
    }
}

将此代码挂载到被检测的UI上,注意我用的UI是RawImage格式的。UI失效即可以不再检测。

20220803_211104

猜你喜欢

转载自blog.csdn.net/qq_52058429/article/details/126109385