Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    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,914
  4. ThunderThortle

    ThunderThortle

    Joined:
    Dec 11, 2020
    Posts:
    11
    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. }));
     
    foxdelis and Suduckgames like this.
  5. spiney199

    spiney199

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

    ThunderThortle

    Joined:
    Dec 11, 2020
    Posts:
    11
    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.