如何通过 Unity3D 中的指针获取 UI 元素? [英] How can I get UI element over which pointer is in Unity3D?

查看:26
本文介绍了如何通过 Unity3D 中的指针获取 UI 元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过 EventSystem.current.IsPointerOverGameObject() 检测鼠标是否悬停在任何 UI 元素上.

I can detect if mouse is hovering any UI element by EventSystem.current.IsPointerOverGameObject().

但是我怎么知道它到底是哪个 GameObjest?

But how do I know which GameObjest it is exactly?

我试过了:

    if (EventSystem.current.IsPointerOverGameObject())
        foreach (GameObject go in new PointerEventData(EventSystem.current).hovered)
            print(go.name);

但是无论我是否悬停,new PointerEventData(EventSystem.current).hovered 都是空的.

But in every moment new PointerEventData(EventSystem.current).hovered is empty whether I hover or not.

我可以使用 print(EventSystem.current);:

<b>Selected:</b>


<b>Pointer Input Module of type: </b>UnityEngine.EventSystems.StandaloneInputModule
<B>Pointer:</b> -1
<b>Position</b>: (746.0, 535.0)
<b>delta</b>: (60.0, -44.0)
<b>eligibleForClick</b>: False
<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
<b>pointerPress</b>: 
<b>lastPointerPress</b>: 
<b>pointerDrag</b>: 
<b>Use Drag Threshold</b>: True
<b>Current Rayast:</b>
Name: cursorInfoText (UnityEngine.GameObject)
module: Name: Canvas (UnityEngine.GameObject)
eventCamera: 
sortOrderPriority: 0
renderOrderPriority: 0
module camera: null
distance: 0
index: 0
depth: 1
worldNormal: (0.0, 0.0, 0.0)
worldPosition: (0.0, 0.0, 0.0)
screenPosition: (746.0, 535.0)
module.sortOrderPriority: 0
module.renderOrderPriority: 0
sortingLayer: 0
sortingOrder: 0
<b>Press Rayast:</b>


<B>Pointer:</b> -2
<b>Position</b>: (746.0, 535.0)
<b>delta</b>: (60.0, -44.0)
<b>eligibleForClick</b>: False
<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
<b>pointerPress</b>: 
<b>lastPointerPress</b>: 
<b>pointerDrag</b>: 
<b>Use Drag Threshold</b>: True
<b>Current Rayast:</b>
Name: cursorInfoText (UnityEngine.GameObject)
module: Name: Canvas (UnityEngine.GameObject)
eventCamera: 
sortOrderPriority: 0
renderOrderPriority: 0
module camera: null
distance: 0
index: 0
depth: 1
worldNormal: (0.0, 0.0, 0.0)
worldPosition: (0.0, 0.0, 0.0)
screenPosition: (746.0, 535.0)
module.sortOrderPriority: 0
module.renderOrderPriority: 0
sortingLayer: 0
sortingOrder: 0
<b>Press Rayast:</b>


<B>Pointer:</b> -3
<b>Position</b>: (746.0, 535.0)
<b>delta</b>: (60.0, -44.0)
<b>eligibleForClick</b>: False
<b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
<b>pointerPress</b>: 
<b>lastPointerPress</b>: 
<b>pointerDrag</b>: 
<b>Use Drag Threshold</b>: True
<b>Current Rayast:</b>
Name: cursorInfoText (UnityEngine.GameObject)
module: Name: Canvas (UnityEngine.GameObject)
eventCamera: 
sortOrderPriority: 0
renderOrderPriority: 0
module camera: null
distance: 0
index: 0
depth: 1
worldNormal: (0.0, 0.0, 0.0)
worldPosition: (0.0, 0.0, 0.0)
screenPosition: (746.0, 535.0)
module.sortOrderPriority: 0
module.renderOrderPriority: 0
sortingLayer: 0
sortingOrder: 0
<b>Press Rayast:</b>

顺便说一下,为什么有 3 个指针(-1、-2、-3),这是什么意思?我在文档的某个地方读过,pointer -1 是鼠标左键,但我什么也没点击,所以这很奇怪.

By the way why are there 3 pointers (-1, -2, -3) and what does this mean? I've read somewhere in docs, pointer -1 is left mouse button, but I click nothing, so this is weird.

我可以看到 <b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)Name: cursorInfoText (UnityEngine.GameObject) 这是我需要的.但是我如何提取这些信息呢?我试过 print(new PointerEventData(EventSystem.current).pointerEnter);,但 Null 与我输入或悬停无关.我在文档中没有看到其他合适的方法或属性,尽管我可以看到存储了信息.我错过了什么?

I can see <b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject) and Name: cursorInfoText (UnityEngine.GameObject) which is what I need. But how do I extract this info? I have tried print(new PointerEventData(EventSystem.current).pointerEnter);, but it is Null doesn't matter I enter or hover. And I see no other appropriate methods or properties in docs, although I can see that info is stored. What have I missed?

我的目标是检测鼠标是否在某些条件下悬停 UI 元素(忽略少量 UI 元素(可能通过标签)并且不要忽略其余(大多数元素)),所以我必须获得 GameObject 本身.如果鼠标没有悬停在 UI 元素上(除了那几个),脚本就不会与 UI 的东西相关.但是不要忽略它是否同时悬停在少数几个和其他之一中.

My goal is to detect if mouse is hovering UI element with some conditions (ignore small amount of UI elements (likely by tag) and don't ignore the rest (most of elements)), so I have to get GameObject itself. And if mouse doesn't hover UI element (except those few), script does unrelated to UI stuff. But do not ignore if it hovers one of those few and one of the rest at the same time.

推荐答案

以下是我针对类似案例所做的:

Here is what I did for a similar case:

using UnityEngine;
using UnityEngine.UI;

public class whatever : MonoBehaviour
{    
    private void Update()
    {
        Rect position = this.gameObject.GetComponent<RectTransform>().GetGlobalPosition();
        if (position.Contains(Input.mousePosition)) Debug.Log("Yes, it is");

        if(this.gameObject.GetComponent<RectTransform>().isMouseOverUI()) Debug.Log("It still is");
    }
}


namespace UnityEngine
{
    namespace UI
    {
        public static class ExtensionMethods
        {
            public static Rect GetGlobalPosition(this RectTransform rectTransform)
            {
                Vector3[] corners = new Vector3[4];
                rectTransform.GetWorldCorners(corners);
                return new Rect(corners[0].x, corners[0].y, corners[2].x - corners[0].x, corners[2].y - corners[0].y);
            }

            public static bool isMouseOverUI(this RectTransform rectTransform)
            {
                Rect position = rectTransform.GetGlobalPosition();
                return position.Contains(Input.mousePosition);
            }
        }
    }
}

所以基本上我所做的是创建了一个扩展方法来获取任何 UI 的 RectTransform 位置.现在我可以询问鼠标现在是否悬停在该 UI 元素上.例如,您可以做的是创建一个数组/列表并检查该列表中的每个元素,如果鼠标当前悬停在它上面(isMouseOverUI).

So basically what I did is I created an extension method for getting any UI's RectTransform position. Now I am able to ask if the mouse is hovering that UI element right now or not. So for example what you could do is create an array/list and check for every element of that list if the mouse is currently hovering it (isMouseOverUI).

我知道这不是您想要的完美解决方案,但我希望它会有所帮助.

I know this is not the perfectly solution you desired but I hope it might help.

这篇关于如何通过 Unity3D 中的指针获取 UI 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆