在Unity中,可以使用Rect类的Contains方法来判断一个点是否在矩形区域内。具体的用法如下:\n\ncsharp\nusing UnityEngine;\n\npublic class RectangleContainsScreenPoint : MonoBehaviour\n{\n public Rect rectangle;\n\n private void Update()\n {\n if (Input.GetMouseButtonDown(0))\n {\n Vector2 mousePosition = Input.mousePosition;\n if (rectangle.Contains(mousePosition))\n {\n Debug.Log("Mouse position is inside the rectangle.");\n }\n else\n {\n Debug.Log("Mouse position is outside the rectangle.");\n }\n }\n }\n}\n\n\n在上述代码中,我们定义了一个Rect类型的变量rectangle来表示矩形区域。在Update方法中,我们检测鼠标左键是否按下,如果按下了,就获取鼠标当前的屏幕坐标。然后使用rectangle的Contains方法来判断鼠标位置是否在矩形区域内。如果在矩形内,就输出"Mouse position is inside the rectangle.",否则输出"Mouse position is outside the rectangle."。\n\n你可以将上述脚本挂载到一个游戏对象上,并将rectangle的值设置为你想要的矩形区域。当你点击鼠标左键时,就会在控制台输出相应的信息。

Unity 矩形区域内点击检测 - Rect.Contains() 方法详解

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

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