Unity3D中判断鼠标或者手指是否点击到UI【UGUI】

  • Post author:
  • Post category:其他

这里使用UGUI系统自带的方法和射线检测的方式,判断是否点击到UI上:

第一种方法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Manager : MonoBehaviour
{
    public Button quitButton;

    // Use this for initialization
    void Start()
    {
        quitButton.onClick.AddListener(OnQuitButtonClick);
    }

    // Update is called once per frame
    void Update()
    {
        //判断是否点击UI
        if (Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
        {
            //移动端
            if (Application.platform == RuntimePlatform.Android ||
                        Application.platform == RuntimePlatform.IPhonePlayer)
            {
                int fingerId = Input.GetTouch(0).fingerId;
                if (EventSystem.current.IsPointerOverGameObject(fingerId))
                {
                    Debug.Log("点击到UI");
                }
            }
            //其它平台
            else
            {
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    Debug.Log("点击到UI");
                }
            }
        }
    }

    void OnQuitButtonClick()
    {
        Application.Quit();
    }
}

第二种方法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
 
public class NewBehaviourScript : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        //移动端
        if (Application.platform == RuntimePlatform.Android ||
                    Application.platform == RuntimePlatform.IPhonePlayer)
        {
            if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                if (IsPointerOverGameObject(Input.GetTouch(0).position))
                {
                    Debug.Log("点击到UI");
                }
            }            
        }
        //其它平台
        else
        {
            if(Input.GetMouseButtonDown(0))
            {
                if (IsPointerOverGameObject(Input.mousePosition))
                {
                    Debug.Log("点击到UI");
                }
            }            
        }
    }
 
    /// <summary>
    /// 检测是否点击UI
    /// </summary>
    /// <param name="mousePosition"></param>
    /// <returns></returns>
    private bool IsPointerOverGameObject(Vector2 mousePosition)
    {       
        //创建一个点击事件
        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.position = mousePosition;
        List<RaycastResult> raycastResults = new List<RaycastResult>();
        //向点击位置发射一条射线,检测是否点击UI
        EventSystem.current.RaycastAll(eventData, raycastResults);
        if (raycastResults.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

 


版权声明:本文为weixin_40116412原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。