Unity判斷鼠標是否在UI(UGUI)上


"EventSystem.current.IsPointerOverGameObject()"

UI和3D場景同時都需要響應觸摸事件,如果同時響應可能就會出現觸摸UI的時候影響到了3D部分。為了解決這個問題在判斷3D響應之前要先判斷手指是否點擊在UI上。

 1 #if UNITY_ANDROID && !UNITY_EDITOR
 2 #define ANDROID
 3 #endif
 4  
 5  
 6 #if UNITY_IPHONE && !UNITY_EDITOR
 7 #define IPHONE
 8 #endif
 9  
10  
11  
12 using UnityEngine;
13 using UnityEngine.UI;
14 using System.Collections;
15 using UnityEngine.EventSystems;
16 public class NewBehaviourScript : MonoBehaviour {
17  
18     // Use this for initialization
19     void Start () {
20     
21     }
22     
23  
24     void Update()
25     {
26         if (Input.GetMouseButtonDown(0)||(Input.touchCount >0 && Input.GetTouch(0).phase == TouchPhase.Began))
27         {
28 #if IPHONE || ANDROID
29             if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
30 #else
31             if (EventSystem.current.IsPointerOverGameObject())
32 #endif
33                 Debug.Log("當前觸摸在UI上");
34             
35             else 
36                 Debug.Log("當前沒有觸摸在UI上");
37         }
38     }
39 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM