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;
}
剛遇到一個問題,我的UI點擊包括3D物體點擊都是用的EventSystem,也就是上面的方法,這時用
UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()這個方法去判斷鼠標(biāo)是否在UI上,就會出現(xiàn)鼠標(biāo)在3D物體上也會拿到返回值,(沒有去研究傳參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)過一個物體時出現(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)點擊位置以及UI物體的屏幕坐標(biāo)
鼠標(biāo)點擊位置:
直接訪問Input.mousePosition屬性,返回一個三維屏幕坐標(biāo),即鼠標(biāo)的坐標(biāo)。
UI物體的屏幕坐標(biāo):
RectTransformUtility.WordToScreenPoint(Camera.main, rectTransform.position),返回的是二維屏幕坐標(biāo)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
WPF利用TabControl控件實現(xiàn)拖拽排序功能
在UI交互中,拖拽操作是一種非常簡單友好的交互,這篇文章主要為大家介紹了WPF如何利用TabControl控件實現(xiàn)拖拽排序功能,需要的小伙伴可以參考一下2023-10-10
Unity的BuildPlayerProcessor實用案例深入解析
這篇文章主要為大家介紹了Unity的BuildPlayerProcessor實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
基于mvc5+ef6+Bootstrap框架實現(xiàn)身份驗證和權(quán)限管理
最近剛做完一個項目,項目架構(gòu)師使用mvc5+ef6+Bootstrap,用的是vs2015,數(shù)據(jù)庫是sql server2014。下面小編把mvc5+ef6+Bootstrap項目心得之身份驗證和權(quán)限管理模塊的實現(xiàn)思路分享給大家,需要的朋友可以參考下2016-06-06

