Unity 点击屏幕与UGUI的区分
来源:互联网 发布:淘宝客转链工具app 编辑:程序博客网 时间:2024/06/11 04:08
UGUI - 判断是否点击在UI 上 Bug,IsPointerOverGameObject()在移动端检测失败
原文链接:这里写链接内容
UGUI 提供了一个检测是否点击在UI上的方法
EventSystem.current.IsPointerOverGameObject();
但是该方法在PC上检测正常,结果拿到Android真机测试上,永远检测不到。
在网上找了一些大神的解决方案
using UnityEngine;using System.Collections;using System.Collections.Generic;using UnityEngine.UI;using UnityEngine.EventSystems;public class ClickIsOverUI { public static ClickIsOverUI Instance = new ClickIsOverUI(); public ClickIsOverUI() { } //方法一, 使用该方法的另一个重载方法,使用时给该方法传递一个整形参数 // 该参数即使触摸手势的 id // int id = Input.GetTouch(0).fingerId; public bool IsPointerOverUIObject(int fingerID) { return EventSystem.current.IsPointerOverGameObject(fingerID); } //方法二 通过UI事件发射射线 //是 2D UI 的位置,非 3D 位置 public bool IsPointerOverUIObject(Vector2 screenPosition) { //实例化点击事件 PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); //将点击位置的屏幕坐标赋值给点击事件 eventDataCurrentPosition.position = new Vector2(screenPosition.x, screenPosition.y); List<RaycastResult> results = new List<RaycastResult>(); //向点击处发射射线 EventSystem.current.RaycastAll(eventDataCurrentPosition, results); return results.Count > 0; } //方法三 通过画布上的 GraphicRaycaster 组件发射射线 public bool IsPointerOverUIObject(Canvas canvas, Vector2 screenPosition) { //实例化点击事件 PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current); //将点击位置的屏幕坐标赋值给点击事件 eventDataCurrentPosition.position = screenPosition; //获取画布上的 GraphicRaycaster 组件 GraphicRaycaster uiRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>(); List<RaycastResult> results = new List<RaycastResult>(); // GraphicRaycaster 发射射线 uiRaycaster.Raycast(eventDataCurrentPosition, results); return results.Count > 0; }}
使用如下
using UnityEngine;using System.Collections;public class RayCastUI : MonoBehaviour { public Transform target; // Update is called once per frame void Update () {#if true //UNITY_ANDROID || UNITY_IPHONE if (Input.touchCount > 0) { //使用方法一:传递触摸手势 ID if (ClickIsOverUI.Instance.IsPointerOverUIObject(Input.GetTouch(0).fingerId)) { Debug.Log("方法一: 点击在UI上"); if (target != null) { target.transform.rotation = Quaternion.Euler(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0)); } } //使用方法二:传递触摸手势坐标 if (ClickIsOverUI.Instance.IsPointerOverUIObject(Input.GetTouch(0).position)) { Debug.Log("方法二: 点击在UI 上"); } //使用方法三:传递画布组件,传递触摸手势坐标 if (ClickIsOverUI.Instance.IsPointerOverUIObject(GetComponent<Canvas>(), Input.GetTouch(0).position)) { Debug.Log("方法三: 点击在UI 上"); } }#endif }}
经过测试上面三种方法均能成功
1 0
- Unity 点击屏幕与UGUI的区分
- Unity-UGUI的点击接口
- Unity 4.6 uGUI的点击事件
- unity ugui 与不同屏幕进行像素适配的问题
- 【UGUI】屏幕区域点击检测
- Unity UGUI与NGUI的比较,NGUI完胜UGUI
- Unity UGUI:Button点击响应
- Unity中NGUI与UGUI的区别
- unity ugui对场景中鼠标点击事件的遮挡
- UNITY之UGUI屏幕适配
- Unity UGUI 是否点击在UI上
- Unity UGUI不规则图片点击事件处理
- Unity UGUI按钮动态添加点击事件
- [Unity UGUI]点击和长按组件
- Unity UGUI点击、拖动等事件
- Unity-屏幕适配UGUI与3D/2D场景
- [Unity][UGUI]UGUI的饼状统计图
- Unity中使用UGUI与ScrollView的练习
- RocketMQ总结整理
- PAT甲级练习1005. Spell It Right (20)
- PackageManagerService启动流程源码解析
- c++中的动态绑定
- java学习记录----面向对象(三)
- Unity 点击屏幕与UGUI的区分
- 关于数据库索引
- Java动态代理深入解析
- 这是我的第一篇博客
- 欢迎使用CSDN-markdown编辑器
- 深入理解JVM虚拟机学习笔记(三)内存分配和回收策略
- 一起Talk Android吧(第一回:概述)
- Apache Commons Compress
- POJ 1426 Find The Multiple dfs or 暴力