unity UGUI判断点击在UI上和3D物体上的解决方案

1.通过EventSystem.current.IsPointerOverGameObject()的监听

/****************************************************
	文件:ISOnclickUI.cs
	作者:
	邮箱: 1686506972@qq.com
	日期:2019/08/23 8:56   	
	功能:判断是否点击在UI上
*****************************************************/
using UnityEngine;
using UnityEngine.EventSystems;

public class ISOnclickUI : MonoBehaviour
{
    // Start is called before the first frame update
    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上");
        }
    }
}

2.UI添加GraphicRaycaster组件,检测是否点击UI

在UI添加GraphicRaycaster组件, 在update里检测是否在ui上

1.先获取点击
        PointerEventData data = new PointerEventData(EventSystem.current);
        data.pressPosition = Input.mousePosition;
        data.position = Input.mousePosition;
2.点击检测_raycaster是本物体的射线检测组件
        List<RaycastResult> results = new List<RaycastResult>();
        _raycaster.Raycast(data, results);

 /****************************************************
   文件:ISOnclickUIRaycast.cs
   作者:
   邮箱: 1686506972@qq.com
   日期:2019/08/23 9:29   	
   功能:UI添加GraphicRaycaster组件,检测是否点击UI
*****************************************************/
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ISOnclickUIRaycast : MonoBehaviour
{
    private GraphicRaycaster _raycaster;
    // Start is called before the first frame update
    void Start()
    {
        _raycaster = FindObjectOfType<GraphicRaycaster>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0) ){
            if (IsUI())
            {
                Debug.Log("在UI上");
            }
            else
            {
                Debug.Log("不在UI上");
            }
        }
    }
    private bool IsUI()
    {
        //获取点击
        PointerEventData data = new PointerEventData(EventSystem.current);
        data.pressPosition = Input.mousePosition;
        data.position = Input.mousePosition;
        //点击检测_raycaster是本物体的射线检测组件
        List<RaycastResult> results = new List<RaycastResult>();
        _raycaster.Raycast(data, results);
        return results.Count > 0;
    }
    
}

3.主摄像机添加PhysicsRaycaster,检测点击UI还是3D物体

需要检测的对象上添加IPointerClickHandler的实现,然后拿到PhysicsRaycaster的所有检测对象,名字是谁就检测到谁

/****************************************************
	文件:IsOnclickUIor3D.cs
	作者:
	邮箱: 1686506972@qq.com
	日期:2019/08/23 10:02   	
	功能:主摄像机添加PhysicsRaycaster,检测点击UI还是3D物体,或者
        同时点击
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class IsOnclickUIor3D : MonoBehaviour, IPointerClickHandler
{
    void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
    {
        
        ExecuteAll(eventData);
    }

  

    public void ExecuteAll(PointerEventData eventData)
    {
        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, results);
        foreach (RaycastResult result in results)
        {
            if (result.gameObject != gameObject)
            {
                //点击传递给3d物体的点击事件监听
                ExecuteEvents.Execute(result.gameObject, eventData, ExecuteEvents.pointerClickHandler);
                Debug.Log("在UI上"+"传递"+ result.gameObject);
            }
            else
            {
                Debug.Log("在UI上");
            }
        }
    }
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值