Search Unity

Detecting mouse over UI element with preview package UIElement runtime?

Discussion in 'UI Toolkit' started by ryanzec, Jan 12, 2020.

  1. ryanzec

    ryanzec

    Joined:
    Jun 10, 2008
    Posts:
    696
    Does anyone know if there is a similar way to know if the mouse is over a UI element with the new UIElement runtime preview package as you can do with:
    Code (csharp):
    1. EventSystem.current.IsPointerOverGameObject()
    with the current UI system (so that if I click a UI element, I don't trigger any game logic that might be tied to the same mouse button)?
     
    Plaximo, RKar and ams61822 like this.
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    ams61822 likes this.
  3. whiteclouds2016

    whiteclouds2016

    Joined:
    Sep 22, 2017
    Posts:
    18
    Code (CSharp):
    1. #region Events
    2.         //
    3.         //
    4.         //
    5.  
    6.         inlineVE.Q<TextField>().RegisterCallback<MouseEnterEvent>(
    7.             e => (e.target as VisualElement)
    8.                 .style.backgroundColor = Color.yellow);
    9.  
    10.         inlineVE.Q<TextField>().RegisterCallback<MouseLeaveEvent>(
    11.             e => (e.target as VisualElement)
    12.                 .style.backgroundColor = Color.clear);
    13.  
    14.         var textFieldList = textFields.ToList();
    15.         foreach (var field in textFieldList)
    16.             field.RegisterCallback<ChangeEvent<string>>(
    17.                 e => m_Tank.tankName =
    18.                     (e.target as TextField).value);
    19.  
    20.         integerFields.ForEach(field =>
    21.             field.RegisterValueChangedCallback(
    22.                 e => m_Tank.tankSize = e.newValue));
    I can't find the solution how to detect the click event is in ui or in game object after I check the demo code.
    In my game, I have a character script also listen the click event and I don't want to it will trigger when I click the UI.
    Is it any other suggest code?
     
    Plaximo and RKar like this.
  4. RKar

    RKar

    Joined:
    Mar 6, 2019
    Posts:
    22
  5. PaoloAm

    PaoloAm

    Joined:
    Jul 15, 2019
    Posts:
    28
    I am curious too about how to check this without the need to implement the event listeners in all ui elements.
     
  6. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    I believe this is what Panel.Pick() does. I've not tried it, but I think it would be something like this:

    Code (CSharp):
    1. IPanel panel = rootElement.panel;
    2. Vector2 panelPosition = RuntimePanelUtils.ScreenToPanel(panel, mousePosition);
    3.  
    4. VisualElement topmost = panel.Pick(panelPosition);
    5. Debug.Log($"The top-most element mouse is over is {ve.name}");
    6.  
    7. List<VisualElement> listForAll = new(); // should store this in a field and reuse it
    8. panel.PickAll(panelPosition, listForAll);
    9. foreach(VisualElement ve in listForAll)
    10.     Debug.Log($"Mouse is over {ve.name}");
     
    Last edited: Aug 3, 2021
    jjbish likes this.
  7. PaoloAm

    PaoloAm

    Joined:
    Jul 15, 2019
    Posts:
    28
    Yes it could but it would require a lot of wrapping, especially if you have a complex UI. They said the other day they are almost ready to release a new API that should solve this. Here