UGUI EventSystem.current.IsPointerOverGameObject(),判断是否点在了UI上

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钩子去掉再试试.

猜你喜欢

转载自www.cnblogs.com/Peng18233754457/p/8964539.html
今日推荐