docs.unity.cn
Search Results for

    Show / Hide Table of Contents

    Method IsPointerOverGameObject

    IsPointerOverGameObject(int)

    Is the pointer with the given ID over an EventSystem object?

    Declaration
    public override bool IsPointerOverGameObject(int pointerId)
    Parameters
    Type Name Description
    int pointerId

    ID of the XR device pointer, mouse pointer or touch registered with the UIInputModule. Meaning this should correspond to either PointerEventData.pointerId or TrackedDeviceEventData.pointerId.

    Returns
    Type Description
    bool

    Returns true if the given pointer is currently hovering over a GameObject. Otherwise, returns false.

    Overrides
    BaseInputModule.IsPointerOverGameObject(int)
    Remarks

    The pointer IDs are generated at runtime by the UIInputModule as devices are registered. Calling this method without any parameters will attempt to use the Left Mouse Button and will likely result in unexpected behavior. A negative pointerId value will be interpreted as "any pointer" and will return true if any XR pointer is currently over a GameObject. Note: The IDs used to check for interaction are not the same as standard InputDevice device IDs.

    Examples
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.XR.Interaction.Toolkit.UI;
    
    public class ClickExample : MonoBehaviour
    {
        [SerializeField]
        UIInputModule inputModule;
    
        private void OnEnable()
        {
            if (inputModule != null)
            {
                inputModule.pointerClick += OnDeviceButtonClick;
            }
        }
    
        private void OnDisable()
        {
            if (inputModule != null)
            {
                inputModule.pointerClick -= OnDeviceButtonClick;
            }
        }
    
        // This method will fire after registering with the UIInputModule callbacks. The UIInputModule will
        // pass the PointerEventData for the device responsible for triggering the callback and can be used to
        // find the pointerId registered with the EventSystem for that device-specific event.
        private void OnDeviceButtonClick(GameObject selected, PointerEventData pointerData)
        {
            if (EventSystem.current.IsPointerOverGameObject(pointerData.pointerId))
            {
                Debug.Log($"Clicked on {EventSystem.current.currentSelectedGameObject}", this);
            }
        }
    }
    See Also
    GetCurrentGameObject(int)
    In This Article
    Back to top
    Copyright © 2023 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)