Unity 如何獲取鼠標(biāo)停留位置下的物體
根據(jù)UGUI的射線檢測(cè)機(jī)制獲取當(dāng)前鼠標(biāo)下的UI:
/// <summary> /// 獲取鼠標(biāo)停留處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為鼠標(biāo)下UI的列表。
不僅適用于UGUI,可以在攝像機(jī)上添加PhysicsRaycaster組件,傳參為攝像機(jī),這樣就可以獲取3D物體。
/// <summary> /// 獲取鼠標(biāo)停留處物體 /// </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; }
剛遇到一個(gè)問(wèn)題,我的UI點(diǎn)擊包括3D物體點(diǎn)擊都是用的EventSystem,也就是上面的方法,這時(shí)用
UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()這個(gè)方法去判斷鼠標(biāo)是否在UI上,就會(huì)出現(xiàn)鼠標(biāo)在3D物體上也會(huì)拿到返回值,(沒(méi)有去研究傳參index的用法),直接選擇了上面獲取UI的獲取方法。
腳本:
/************************************************************ * 版本聲明:v1.0.0 * 類(lèi) 名 稱(chēng):MouseOverController.cs * 創(chuàng)建日期:2019/8/10 16:10:44 * 作者名稱(chēng):末零 * 功能描述:獲取鼠標(biāo)停留處的物體 ************************************************************/ using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace LastZero.Utility { public class MouseOverController { /// <summary> /// 獲取鼠標(biāo)停留處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> /// 獲取鼠標(biāo)停留處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; } } }
補(bǔ)充:unity中鼠標(biāo)經(jīng)過(guò)一個(gè)物體時(shí)出現(xiàn)提示
首先被檢測(cè)的物體要有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"); } } }
補(bǔ)充:Unity中UGUI中獲取鼠標(biāo)點(diǎn)擊位置以及UI物體的屏幕坐標(biāo)
鼠標(biāo)點(diǎn)擊位置:
直接訪問(wèn)Input.mousePosition屬性,返回一個(gè)三維屏幕坐標(biāo),即鼠標(biāo)的坐標(biāo)。
UI物體的屏幕坐標(biāo):
RectTransformUtility.WordToScreenPoint(Camera.main, rectTransform.position),返回的是二維屏幕坐標(biāo)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
WPF利用TabControl控件實(shí)現(xiàn)拖拽排序功能
在UI交互中,拖拽操作是一種非常簡(jiǎn)單友好的交互,這篇文章主要為大家介紹了WPF如何利用TabControl控件實(shí)現(xiàn)拖拽排序功能,需要的小伙伴可以參考一下2023-10-10C#網(wǎng)站生成靜態(tài)頁(yè)面的實(shí)例講解
今天小編就為大家分享一篇關(guān)于C#網(wǎng)站生成靜態(tài)頁(yè)面的實(shí)例講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01C#中幾個(gè)未知的Visual Studio編碼技巧分享
用了多年的Visual Studio,今天才發(fā)現(xiàn)這個(gè)編碼技巧,真是慚愧,分享出來(lái),算是拋磚引玉吧,需要的朋友可以參考下2012-11-11Unity的BuildPlayerProcessor實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity的BuildPlayerProcessor實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05基于mvc5+ef6+Bootstrap框架實(shí)現(xiàn)身份驗(yàn)證和權(quán)限管理
最近剛做完一個(gè)項(xiàng)目,項(xiàng)目架構(gòu)師使用mvc5+ef6+Bootstrap,用的是vs2015,數(shù)據(jù)庫(kù)是sql server2014。下面小編把mvc5+ef6+Bootstrap項(xiàng)目心得之身份驗(yàn)證和權(quán)限管理模塊的實(shí)現(xiàn)思路分享給大家,需要的朋友可以參考下2016-06-06