Unity 如何获取鼠标停留位置下的物体
作者:末零 时间:2023-09-05 17:17:14
根据UGUI的射线检测机制获取当前鼠标下的UI:
/// <summary>
/// 获取鼠标停留处UI
/// </summary>
/// <param name="canvas"></param>
/// <returns></returns>
public GameObject GetOverUI(GameObject canvas)
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
List<RaycastResult> results = new List<RaycastResult>();
gr.Raycast(pointerEventData, results);
if (results.Count != 0)
{
return results[0].gameObject;
}
return null;
}
其中,results为鼠标下UI的列表。
不仅适用于UGUI,可以在摄像机上添加PhysicsRaycaster组件,传参为摄像机,这样就可以获取3D物体。
/// <summary>
/// 获取鼠标停留处物体
/// </summary>
/// <param name="raycaster"></param>
/// <returns></returns>
public GameObject GetOverGameObject(GameObject raycaster)
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
PhysicsRaycaster pr = raycaster.GetComponent<PhysicsRaycaster>();
List<RaycastResult> results = new List<RaycastResult>();
pr.Raycast(pointerEventData, results);
if (results.Count != 0)
{
return results[0].gameObject;
}
return null;
}
刚遇到一个问题,我的UI点击包括3D物体点击都是用的EventSystem,也就是上面的方法,这时用
UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()这个方法去判断鼠标是否在UI上,就会出现鼠标在3D物体上也会拿到返回值,(没有去研究传参index的用法),直接选择了上面获取UI的获取方法。
脚本:
/************************************************************
* 版本声明:v1.0.0
* 类 名 称:MouseOverController.cs
* 创建日期:2019/8/10 16:10:44
* 作者名称:末零
* 功能描述:获取鼠标停留处的物体
************************************************************/
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace LastZero.Utility
{
public class MouseOverController
{
/// <summary>
/// 获取鼠标停留处UI
/// </summary>
/// <param name="canvas"></param>
/// <returns></returns>
public static GameObject GetOverUI(GameObject canvas)
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
List<RaycastResult> results = new List<RaycastResult>();
gr.Raycast(pointerEventData, results);
if (results.Count != 0)
{
return results[0].gameObject;
}
return null;
}
/// <summary>
/// 获取鼠标停留处UI
/// </summary>
/// <param name="canvas"></param>
/// <returns></returns>
public static GameObject GetOverGameObject(GameObject camera)
{
if (camera.GetComponent<PhysicsRaycaster>() == null)
camera.AddComponent<PhysicsRaycaster>();
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
PhysicsRaycaster gr = camera.GetComponent<PhysicsRaycaster>();
List<RaycastResult> results = new List<RaycastResult>();
gr.Raycast(pointerEventData, results);
if (results.Count != 0)
{
return results[0].gameObject;
}
return null;
}
}
}
补充:unity中鼠标经过一个物体时出现提示
首先被检测的物体要有collider
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
// public Transform cube;
bool isShowTip;
// // Use this for initialization
void Start () {
isShowTip=false;
}
void OnMouseEnter () {
isShowTip=true;
//Debug.Log (cube.name);//可以得到物体的名字
}
void OnMouseExit () {
isShowTip=false;
}
void OnGUI () {
if (isShowTip){
GUI.Label(new Rect(Input.mousePosition.x,Screen.height-Input.mousePosition.y,100,40),"afdasdfasdf");
}
}
}
补充:Unity中UGUI中获取鼠标点击位置以及UI物体的屏幕坐标
鼠标点击位置:
直接访问Input.mousePosition属性,返回一个三维屏幕坐标,即鼠标的坐标。
UI物体的屏幕坐标:
RectTransformUtility.WordToScreenPoint(Camera.main, rectTransform.position),返回的是二维屏幕坐标。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/n_moling/article/details/81380782
标签:Unity,鼠标,停留位置,物体
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Android更改APP语言模式的实现过程
2023-05-18 16:56:35
![](https://img.aspxhome.com/file/2023/2/138522_0s.gif)
Android实现网易Tab分类排序控件实现
2023-10-31 22:20:56
![](https://img.aspxhome.com/file/2023/5/126715_0s.png)
zookeeper概述图文详解
2021-12-07 15:46:03
![](https://img.aspxhome.com/file/2023/6/71946_0s.png)
Java向上转型和向下转型实例解析
2022-08-12 01:59:28
Spring boot外部配置(配置中心化)详解
2022-07-11 23:13:26
轻松掌握Java建造者模式
2023-11-06 15:24:38
java 数据类型有哪些取值范围多少
2023-01-18 06:13:44
android 点击EditText始终不弹出软件键盘实现代码
2022-05-23 06:37:19
C#处理Paint事件的方法
2022-01-12 02:02:51
java常用工具类之DES和Base64加密解密类
2023-12-20 17:48:31
浅谈Java中的Queue家族
2021-12-23 08:36:49
![](https://img.aspxhome.com/file/2023/8/125578_0s.jpg)
C#设计模式之工厂模式
2023-07-10 13:43:33
![](https://img.aspxhome.com/file/2023/4/83224_0s.jpg)
mybatis报错元素内容必须由格式正确的字符数据或标记组成异常的解决办法
2023-01-10 15:46:37
C#图像处理之图像平移的方法
2021-12-16 08:38:37
Java基础之异常处理详解
2021-07-14 03:03:49
![](https://img.aspxhome.com/file/2023/8/88518_0s.webp)
初步认识C#中的Lambda表达式和匿名方法
2023-07-03 00:23:28
Maven build 命令介绍的使用详解
2023-08-20 18:09:35
![](https://img.aspxhome.com/file/2023/7/125517_0s.png)
Elasticsearch percolate 查询示例详解
2021-07-15 04:57:54
基于Spring Security前后端分离的权限控制系统问题
2022-10-16 21:32:39
![](https://img.aspxhome.com/file/2023/4/66254_0s.png)
Java框架设计灵魂之反射的示例详解
2023-06-13 21:34:34