1.判斷是否點擊 UGUI界面:
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; public class UiClickTest : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { if (EventSystem.current.IsPointerOverGameObject()) { Debug.Log("點擊到UGUI的UI界面,會返回true"); } else { Debug.Log("如果沒點擊到UGUI上的任何東西,就會返回false"); } } } }
2.判斷是否點擊了NGUI界面:
void OnClick () { // NGUI 3.8之前 點擊空白地方返回為null // NGUI 3.8之后 點擊空白地方返回為UIRoot if (UICamera.hoveredObject.name != "UIRoot") { Debug.Log ("我現在點擊的不是空白"); } } void Update() { // 返回值是bool變量,如果鼠標在UI上返回true(記得加碰撞器) Debug.Log(UICamera.Raycast(Input.mousePosition)); }