Unity 如何獲取鼠標(biāo)停留位置下的物體
根據(jù)UGUI的射線檢測機(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è)問題,我的UI點(diǎn)擊包括3D物體點(diǎn)擊都是用的EventSystem,也就是上面的方法,這時(shí)用
UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()這個(gè)方法去判斷鼠標(biāo)是否在UI上,就會(huì)出現(xiàn)鼠標(biāo)在3D物體上也會(huì)拿到返回值,(沒有去研究傳參index的用法),直接選擇了上面獲取UI的獲取方法。
腳本:
/************************************************************
* 版本聲明:v1.0.0
* 類 名 稱:MouseOverController.cs
* 創(chuàng)建日期:2019/8/10 16:10:44
* 作者名稱:末零
* 功能描述:獲取鼠標(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)過一個(gè)物體時(shí)出現(xiàn)提示
首先被檢測的物體要有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)擊位置:
直接訪問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)文章
C# Oracle批量插入數(shù)據(jù)進(jìn)度條的實(shí)現(xiàn)代碼
這篇文章主要介紹了C# Oracle批量插入數(shù)據(jù)進(jìn)度條的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-04-04
Unity Shader實(shí)現(xiàn)3D翻頁效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)3D翻頁效果,Plane實(shí)現(xiàn)翻頁效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
c#動(dòng)態(tài)調(diào)用Webservice的兩種方法實(shí)例
這篇文章介紹了c#動(dòng)態(tài)調(diào)用Webservice的兩種方法實(shí)例,有需要的朋友可以參考一下2013-08-08
C#判斷指定驅(qū)動(dòng)器是否已經(jīng)準(zhǔn)備就緒的方法
這篇文章主要介紹了C#判斷指定驅(qū)動(dòng)器是否已經(jīng)準(zhǔn)備就緒的方法,涉及C#針對硬件IO操作的技巧,需要的朋友可以參考下2015-04-04
C#設(shè)計(jì)模式實(shí)現(xiàn)之迭代器模式
迭代器模式把對象的職責(zé)分離,職責(zé)分離可以最大限度減少彼此之間的耦合程度,從而建立一個(gè)松耦合的對象,這篇文章主要給大家介紹了關(guān)于C#設(shè)計(jì)模式實(shí)現(xiàn)之迭代器模式的相關(guān)資料,需要的朋友可以參考下2021-08-08
c#實(shí)現(xiàn)sqlserver事務(wù)處理示例
這篇文章主要介紹了c#實(shí)現(xiàn)sqlserver事務(wù)處理的示例,大家參考使用吧2014-01-01

