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. Dismiss Notice

UI Toolkit: How can I add clickable to non-clickable elements?

Discussion in 'Scripting' started by ironfroggy, Jun 8, 2022.

  1. ironfroggy

    ironfroggy

    Joined:
    Jan 19, 2015
    Posts:
    1
    For example, I have a GroupBox with some text and other non-interactables in it. I want the whole groupbox to be clickable to popup some options. GroupBox doesn't have a Clickable property, so I can't bind to click events for it so... how can I do that? is there a way to attach a clickable to an arbitrary element? Do I need to make my own "ClickableGroupBox" subclass?
     
  2. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    218
    Did you find the solution? I have a similar problem where I want a Text and an Image to be clickable but didn't find any info for it
     
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
  4. ThunderThortle

    ThunderThortle

    Joined:
    Dec 11, 2020
    Posts:
    7
    I am doing it this way and works like a charm :). It also allows the visual element to have an active state and a focus state so you can apply custom styles for such cases.

    Code (CSharp):
    1. myVisualElement.AddManipulator(new Clickable(evt => {
    2.     // TODO : your onClick code here
    3. }));
     
    Suduckgames likes this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,848
    Isn't that just the same as using a button, but with more steps?
     
  6. ThunderThortle

    ThunderThortle

    Joined:
    Dec 11, 2020
    Posts:
    7
    Yesn't, the goal is the same but the components are different, with buttons you do :

    Code (CSharp):
    1. positiveButton.clicked += () => {
    2.    // TODO : onClick code here
    3. }
    Sadly, the clicked api is not available in regular VisualElements, but the same can be achieved with the AddManipulator method I mentioned before.
    BTW, AddManipulator can be used to handle other types of events, not just click events.
    In this case, I am using it for the same use case as @Suduckgames in a VisualElement containing text and an image.
     
    Suduckgames likes this.