广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >Unity 如何获取鼠标停留位置下的物体
  • 1018
分享到

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

2024-04-02 19:04:59 1018人浏览 八月长安
摘要

根据UGUI的射线检测机制获取当前鼠标下的UI: /// <summary> /// 获取鼠标停留处UI /// </summary>

根据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的获取方法。

脚本:


 
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),返回的是二维屏幕坐标。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。如有错误或未考虑完全的地方,望不吝赐教。

--结束END--

本文标题: Unity 如何获取鼠标停留位置下的物体

本文链接: https://www.lsjlt.com/news/123414.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Unity 如何获取鼠标停留位置下的物体
    根据UGUI的射线检测机制获取当前鼠标下的UI: /// <summary> /// 获取鼠标停留处UI /// </summary> ...
    99+
    2022-11-12
  • Unity怎么获取鼠标停留位置下的物体
    这篇文章给大家分享的是有关Unity怎么获取鼠标停留位置下的物体的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。根据UGUI的射线检测机制获取当前鼠标下的UI:/// <summary>&nb...
    99+
    2023-06-14
  • 如何利用Python获取鼠标的实时位置
    目录安装pyautogui鼠标操作样例Python获取鼠标实时位置具体实现结果展示总结使用Python的第三方库pyautogui,PyAutoGUI是一个纯Python的GUI自动...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作