"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 }