unity 如何判斷鼠標(biāo)是否在哪個(gè)UI上(兩種方法)
第一種
可以得到UI,再根據(jù)名字判斷是不是自己自己要點(diǎn)擊的UI
其中參數(shù)canvas拖入此UI的canvas
/// <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;
}
第二種就簡單了
rect 為要判斷的那個(gè)UI的RectTransform
bool isUI = RectTransformUtility.RectangleContainsScreenPoint(rect, Input.mousePosition)
補(bǔ)充:Unity中判斷鼠標(biāo)或者手指是否點(diǎn)擊在UI上(UGUI)
在Unity場景中,有時(shí)UI和游戲角色都需要響應(yīng)觸摸事件,如果同時(shí)響應(yīng)可能就會(huì)出現(xiàn)點(diǎn)擊UI的時(shí)候影響到了游戲角色。所以我們需要對所點(diǎn)擊到的東西做判斷,這里使用UGUI系統(tǒng)自帶的方法和射線檢測的方式,判斷是否點(diǎn)擊到UI上:
第一種方法,直接在Update中判斷:
void Update()
{
//判斷是否點(diǎn)擊UI
if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
//移動(dòng)端
if (Application.platform == RuntimePlatform.Android ||
Application.platform == RuntimePlatform.IPhonePlayer)
{
int fingerId = Input.GetTouch(0).fingerId;
if (EventSystem.current.IsPointerOverGameObject(fingerId))
{
Debug.Log("點(diǎn)擊到UI");
}
}
//其它平臺
else
{
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("點(diǎn)擊到UI");
}
}
}
第二種方式:射線檢測
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class NewBehaviourScript : MonoBehaviour
{
// Update is called once per frame
void Update()
{
//移動(dòng)端
if (Application.platform == RuntimePlatform.Android ||
Application.platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
if (IsPointerOverGameObject(Input.GetTouch(0).position))
{
Debug.Log("點(diǎn)擊到UI");
}
}
}
//其它平臺
else
{
if(Input.GetMouseButtonDown(0))
{
if (IsPointerOverGameObject(Input.mousePosition))
{
Debug.Log("點(diǎn)擊到UI");
}
}
}
}
/// <summary>
/// 檢測是否點(diǎn)擊UI
/// </summary>
/// <param name="mousePosition"></param>
/// <returns></returns>
private bool IsPointerOverGameObject(Vector2 mousePosition)
{
//創(chuàng)建一個(gè)點(diǎn)擊事件
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = mousePosition;
List<RaycastResult> raycastResults = new List<RaycastResult>();
//向點(diǎn)擊位置發(fā)射一條射線,檢測是否點(diǎn)擊UI
EventSystem.current.RaycastAll(eventData, raycastResults);
if (raycastResults.Count > 0)
{
return true;
}
else
{
return false;
}
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解
這篇文章主要介紹了WPF ComboBox獲取當(dāng)前選擇值的實(shí)例詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08
c#基于Redis實(shí)現(xiàn)輕量級消息組件的步驟
這篇文章主要介紹了c#基于Redis實(shí)現(xiàn)輕量級消息組件的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#進(jìn)行開發(fā),感興趣的朋友可以了解下2021-05-05
C#實(shí)現(xiàn)TreeView節(jié)點(diǎn)拖拽的方法
這篇文章主要介紹了C#實(shí)現(xiàn)TreeView節(jié)點(diǎn)拖拽的方法,涉及C#針對TreeView節(jié)點(diǎn)的動(dòng)態(tài)添加及移除技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
Unity3d實(shí)現(xiàn)Flappy Bird游戲
這篇文章主要為大家詳細(xì)介紹了Unity3d實(shí)現(xiàn)Flappy Bird游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

