Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Register Call Back Question

Discussion in 'UI Toolkit' started by joe-cabb, Mar 29, 2020.

  1. joe-cabb

    joe-cabb

    Joined:
    May 28, 2017
    Posts:
    6
    Hello.
    I've gotten stuck with registering callback functions for buttons in the run time (I'm seeing topics relative to the Editor, but hey aren't working).

    I'm using Unity 2020.1.0b3.3385
    I created a Panel Object and attached a script to it. (In the Panel Object, it has both a PanelRenderer and an EventSystem component, based on feedback I've seen elsewhere).

    I'm able to get the UXML UI to render and can access its elements via C# script, but I can't get the callbacks registration to work. I'd appreciate any advice.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UIElements;
    4. using Unity.UIElements.Runtime;
    5.  
    6.  
    7. public class SamplePanelScript : MonoBehaviour
    8. {
    9.     public GameObject thePanel;
    10.     private VisualElement theVisualTree;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         PanelRenderer theRenderer = thePanel.GetComponent<PanelRenderer>();
    16.         theVisualTree = theRenderer.visualTree;
    17.        
    18.         Button button = theVisualTree.Q<Button>("test-button");
    19.         //This debug statement works
    20.         Debug.Log("button.text: " + button.text);
    21.  
    22.         button.RegisterCallback<MouseDownEvent> (OnMouseDown);                
    23.     }
    24.  
    25.     void OnMouseDown(MouseDownEvent e)
    26.     {
    27.         //This debug is not getting fired when I click the button during play mode
    28.         Debug.Log("OnMouseDown called");
    29.     }
    30. }
    31.  
    I originally had this in OnEnable, but theRenderer.visualTree property was null in that context.

    Thank you.
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,890
    I think it uses a 'clicked' callback inside the Button class. So something like
    button.clicked += OnMouseDown;
    .
     
  3. joe-cabb

    joe-cabb

    Joined:
    May 28, 2017
    Posts:
    6
  4. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    386
    Button is a special snowflake. Its clickable manipulator stops propagation of MouseDown/Up events. Use Button.clicked += ...
     
  5. joe-cabb

    joe-cabb

    Joined:
    May 28, 2017
    Posts:
    6
    uMathieu,
    Thank you for the pointer. I never would have figured that out. I modified my code to per your suggestion, and it didn't originally work. However, I figured it out. I needed to get a reference to the right visual tree from the PanelRenderer.

    Code (CSharp):
    1.  
    2.  
    3. void OnEnable()
    4.     {
    5.         PanelRenderer theRenderer = thePanel.GetComponent<PanelRenderer>();
    6.         if (theRenderer.visualTree == null)
    7.         {
    8.             theRenderer.RecreateUIFromUxml();
    9.         }
    10.         theVisualTree = theRenderer.visualTree;
    11.  
    12.  
    13.  
    14.         Button button = theVisualTree.Q<Button>("test-button");
    15.         //This debug statement works
    16.         Debug.Log("button.text: " + button.text);
    17.  
    18.         button.clicked += () => Debug.Log("Button clicked");
    19.  
    20.     }
    21.  
    22.  
     
  6. VisionEEG

    VisionEEG

    Joined:
    Aug 29, 2018
    Posts:
    28
    MouseUpEvent works for buttons, MouseDownEvent don't. Using Version 1.0.0-preview.14 - March 30, 2021.
     
  7. gregcodercw

    gregcodercw

    Joined:
    Dec 5, 2017
    Posts:
    15
    Is there a work around for capturing button click on mouse down?
     
  8. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    184
    I don't think there is anything that prevents You should be able to capture mouse clicks on Buttons, but make sure Button.clickable = null before, then you can register to MouseDownEvent or any other event without seeing your events eaten before your callback can be called.
     
    CodeKiwi likes this.
  9. CoryMaklin

    CoryMaklin

    Joined:
    Oct 10, 2020
    Posts:
    29
    I'm using Unity 2021.1.21f1. I have UI Builder & UI Toolkit versions 1.0.0-preview.17 installed. I have the Input System version 1.0.2 installed. When I listen for clicked events on a button created in UI Builder, it does not fire when running in Play mode with a UI Document. I've had to resort to listening to MouseDownEvent. Is it possible there's a bug with the Input System Event System (UI Toolkit) script?
     
  10. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    230
    Are you using the
    button.clicked
    action or registering for the `ClickEvent` callback? I think we've had some issues recently with ClickEvents, but clicked action was working fine.