Unity 如何获取鼠标停留位置下的物体

这篇文章主要介绍了Unity 如何获取鼠标停留位置下的物体,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

根据UGUI的射线检测机制获取当前鼠标下的UI:

 ///  /// 获取鼠标停留处UI ///  ///  ///  public GameObject GetOverUI(GameObject canvas) { PointerEventData pointerEventData = new PointerEventData(EventSystem.current); pointerEventData.position = Input.mousePosition; GraphicRaycaster gr = canvas.GetComponent(); List results = new List(); gr.Raycast(pointerEventData, results); if (results.Count != 0) { return results[0].gameObject; } return null; }

其中,results为鼠标下UI的列表。

不仅适用于UGUI,可以在摄像机上添加PhysicsRaycaster组件,传参为摄像机,这样就可以获取3D物体。

 ///  /// 获取鼠标停留处物体 ///  ///  ///  public GameObject GetOverGameObject(GameObject raycaster) { PointerEventData pointerEventData = new PointerEventData(EventSystem.current); pointerEventData.position = Input.mousePosition; PhysicsRaycaster pr = raycaster.GetComponent(); List results = new List(); 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 { ///  /// 获取鼠标停留处UI ///  ///  ///  public static GameObject GetOverUI(GameObject canvas) { PointerEventData pointerEventData = new PointerEventData(EventSystem.current); pointerEventData.position = Input.mousePosition; GraphicRaycaster gr = canvas.GetComponent(); List results = new List(); gr.Raycast(pointerEventData, results); if (results.Count != 0) { return results[0].gameObject; } return null; } ///  /// 获取鼠标停留处UI ///  ///  ///  public static GameObject GetOverGameObject(GameObject camera) { if (camera.GetComponent() == null) camera.AddComponent(); PointerEventData pointerEventData = new PointerEventData(EventSystem.current); pointerEventData.position = Input.mousePosition; PhysicsRaycaster gr = camera.GetComponent(); List results = new List(); 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),返回的是二维屏幕坐标。

以上就是Unity 如何获取鼠标停留位置下的物体的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程