UGUI判断鼠标或者手指是否点击在UI上

2023-08-24 14:28:56 浏览数 (2)

代码语言:javascript复制
#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上");
        }
    }
}

移动端与pc端的判断函数不同

0 人点赞