比如戰斗場景,UI和3D場景同時都需要響應觸摸事件,如果同時響應可能就會出現觸摸UI的時候影響到了3D部分。為了解決這個問題在判斷3D響應之前要先判斷手指是否點擊在UI上。 以前NGUI的時候都是自己來發送射線判斷,現在UGUI好了系統提供了更為簡便的方法。
#if UNITY_ANDROID && !UNITY_EDITOR #define ANDROID #endif #if UNITY_IPHONE && !UNITY_EDITOR #define IPHONE #endif using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.EventSystems; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start() { } void Update() { if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)) { #if IPHONE || ANDROID if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) #else if (EventSystem.current.IsPointerOverGameObject()) #endif Debug.Log("當前觸摸在UI上"); else Debug.Log("當前沒有觸摸在UI上"); } } }