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"); } } //其它平臺(tái) 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"); } } } //其它平臺(tái) 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)文章
Unity實(shí)現(xiàn)物體跟隨鼠標(biāo)移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體跟隨鼠標(biāo)移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01使用C#程序驗(yàn)證系統(tǒng)登錄用戶與目錄權(quán)限
這篇文章主要介紹了使用C#程序驗(yàn)證系統(tǒng)登錄用戶與目錄權(quán)限,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12WPF實(shí)現(xiàn)自定義Panel面板的示例詳解
WPF中的Panel(面板),是繼承自FrameworkElement的抽象類,表示一個(gè)可以用來排列子元素的面板,本文主要來和大家聊聊WPF如何實(shí)現(xiàn)自定義Panel,感興趣的可以了解下2023-09-09