在Unity中,可以使用Rect类的Contains方法来判断一个点是否在矩形区域内。具体的用法如下:

using UnityEngine;

public class RectangleContainsScreenPoint : MonoBehaviour
{
    public Rect rectangle;

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector2 mousePosition = Input.mousePosition;
            if (rectangle.Contains(mousePosition))
            {
                Debug.Log("Mouse position is inside the rectangle.");
            }
            else
            {
                Debug.Log("Mouse position is outside the rectangle.");
            }
        }
    }
}

在上述代码中,我们定义了一个Rect类型的变量rectangle来表示矩形区域。在Update方法中,我们检测鼠标左键是否按下,如果按下了,就获取鼠标当前的屏幕坐标。然后使用rectangle的Contains方法来判断鼠标位置是否在矩形区域内。如果在矩形内,就输出"Mouse position is inside the rectangle.",否则输出"Mouse position is outside the rectangle."。

你可以将上述脚本挂载到一个游戏对象上,并将rectangle的值设置为你想要的矩形区域。当你点击鼠标左键时,就会在控制台输出相应的信息

Unity RectangleContainsScreenPoint事件接口

原文地址: http://www.cveoy.top/t/topic/ixb8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录