Search Unity

Check click on UI element

Discussion in 'UI Toolkit' started by DREBOTgamestudio, Jul 5, 2020.

  1. DREBOTgamestudio

    DREBOTgamestudio

    Joined:
    Jan 30, 2018
    Posts:
    66
    Hello.
    Previously, I used EventSystem.current.IsPointerOverGameObject() to test the click on a UI element. But with Ui Builder i have false all the time.
    EventSystem.current.IsPointerOverGameObject () completely ignores the new UI. Is there a current replacement? Or am I doing something wrong?

    I use click to move an object. Now any click on the interface sets the object in motion.
     
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    DREBOTgamestudio likes this.
  3. DREBOTgamestudio

    DREBOTgamestudio

    Joined:
    Jan 30, 2018
    Posts:
    66
    Thanks for the answer, this solved my problem. But now I am looking for the best implementation variant. Please tell me, is this the correct solution to replace EventSystem.current.IsPointerOverGameObject()?


    Code (CSharp):
    1.     public void Awake()
    2.     {
    3.         panelRenderer = GetComponent<PanelRenderer>();
    4.         panelRenderer.postUxmlReload = BindLocationScreen;
    5.     }
    6.     private IEnumerable<Object> BindLocationScreen()
    7.     {
    8.         panelRenderer.visualTree.Q<VisualElement>("TestElement").RegisterCallback<MouseEnterEvent>(MyCallbackEnter);
    9.         panelRenderer.visualTree.Q<VisualElement>("TestElement").RegisterCallback<MouseLeaveEvent>(MyCallbackLeave);
    10.     }
    11.  
    12.         public void MyCallbackEnter(MouseEnterEvent evt)
    13.     {
    14.         game.overlapUI = true;  // static value
    15.     }
    16.     public void MyCallbackLeave(MouseLeaveEvent evt)
    17.     {
    18.         game.overlapUI = false;// static value
    19.     }
    How do I better track the intersection of the cursor and all UI elements so as not to write this for all? And how to ignore specific UI elements? This is also very important.

    My character control is implemented using the new input system. And it looks something like this.

    Code (CSharp):
    1. void Awake()
    2.         {
    3. _controls.Location.ClickHoldLeftMouse.performed += ctx => buttonMovePressed = true;
    4. _controls.Location.ClickHoldLeftMouse.canceled += ctx => buttonMovePressed = false;
    5. }
    6. void LateUpdate()
    7.  {
    8.             // if (game.overlapUI) return; // I want to do a check like this.
    9.             if (buttonMovePressed)
    10.             {
    11.                 Click();
    12.             }
    13. }
     
    Last edited: Jul 6, 2020
  4. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    In your late update, you could call
    panelRenderer.visualTree.panel.Pick(mousePosition)
    . This return the top element under the mouse. If you use the current mouse position, it should return a cached value directly without performing a full pick.
     
    DREBOTgamestudio likes this.
  5. DREBOTgamestudio

    DREBOTgamestudio

    Joined:
    Jan 30, 2018
    Posts:
    66
    May be, but i have some problems.
    1. Therefore, I need to access the UI script. This links scripts. I would like to avoid this.
    Appeal example:
    Code (CSharp):
    1. void LateUpdate()
    2. { Debug.Log(UIManager.Instance.screen_Location.GetComponent<LocationScreen>().panelRenderer.visualTree.panel.Pick(_inputMouseCurrent)); }

    2. Even without UI elements, I get the parent element of the window UI. This means the cursor will always cross some kind of UI object.
    Debug.log example:
    VisualElement runtime-panel-container (x:0.00, y:0.00, width:1920.00, height:1080.00) world rect: (x:0.00, y:0.00, width:1920.00, height:1080.00)
     
  6. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    992
    Now how do you perform a click event after you find object?
    Or how do I do a custom click even for VR?
     
    seanalf likes this.