EventSystem.current.IsPointerOverGameObject(); //返回一個布爾值,進入了UI上就返回true,用的時候要 using UnityEngine.EventSystems;
1.寫一個腳本掛到相機上,如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class Test : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0) && EventSystem.current.IsPointerOverGameObject() == false) { Debug.Log("右鍵沒有點在UI上..."); } else if (Input.GetMouseButtonDown(0) && EventSystem.current.IsPointerOverGameObject() == true) { Debug.Log("右鍵點在了UI上..."); } } }
2.新建一個Image ,如圖:
3.運行,試着用右鍵點擊空白處,再試着點擊Image試試;把Image的RaycastTarget鈎子去掉再試試.