年后剛上班,就有不幸的事情發(fā)生,項(xiàng)目界面要把NGUI推翻,用UGUI來(lái)做,真是蛋都碎了,但是沒(méi)辦法,在做的過(guò)程中遇UI穿透和點(diǎn)擊的是否是UI,查資料,問(wèn)朋友,弄出來(lái),跟大家分享:

1.UGUI自帶的防穿透代碼:
if (EventSystem.current.IsPointerOverGameObject()) { return;//為真,則點(diǎn)擊在UI上 } 好像漏了點(diǎn)東西,沒(méi)有寫(xiě)全,上面的只是Unity_Editor 下或者電腦上能運(yùn)行,移動(dòng)端是 if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) { return;//為真,則點(diǎn)擊在UI上 } 總結(jié): #if IPHONE || ANDROID if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) #else if (EventSystem.current.IsPointerOverGameObject()) #endif Debug.Log("當(dāng)前觸摸在UI上"); else Debug.Log("當(dāng)前沒(méi)有觸摸在UI上");2.查資料,自己寫(xiě)了一個(gè)判斷
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using System.Collections.Generic; using UnityEngine.UI; public class CheckUGUI : MonoBehaviour { public static bool CheckGuiRaycastObjects(EventSystem eventSystem, GraphicRaycaster graphicRayCaster) { PointerEventData eventData = new PointerEventData(eventSystem); #if UNITY_EDITOR eventData.pressPosition = Input.mousePosition; eventData.position = Input.mousePosition; #endif #if UNITY_ANDROID || UNITY_IPHONE if (Input.touchCount > 0) { eventData.pressPosition = Input.GetTouch(0).position; eventData.position = Input.GetTouch(0).position; } #endif List<RaycastResult> list = new List<RaycastResult>(); graphicRayCaster.Raycast(eventData, list); return list.Count > 0; } }第二種,我把它寫(xiě)成了一個(gè)類(lèi),用到的調(diào)用就行
public EventSystem eventSystem; public GraphicRaycaster graphicRaycaster; if (CheckUGUI.CheckGuiRaycastObjects(eventSystem, graphicRaycaster)) return;//為真,就點(diǎn)擊的是UI。希望大家能用的上。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁(yè)標(biāo)題:UGUI防止穿透和判斷點(diǎn)擊的是否是UI-創(chuàng)新互聯(lián)
鏈接地址:http://chinadenli.net/article2/dghpic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、App開(kāi)發(fā)、網(wǎng)站排名、響應(yīng)式網(wǎng)站、網(wǎng)站制作、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容