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

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;


public class TestTouch : MonoBehaviour {


    public Text text;


    void Update() 
    {
        if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
#if UNITY_ANDROID || UNITY_IPHONE
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
            if (EventSystem.current.IsPointerOverGameObject())
#endif
                
                text.text = "当前触摸在UI上";
            else
                text.text = "当前没有触摸在UI上";
        }
    }


}

你可能感兴趣的:(unity 判断鼠标或者手指是否点击在UI上)