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

Trying to get "clickable.clicked" from button.

Discussion in 'UI Toolkit' started by martinusprim, Apr 13, 2020.

  1. martinusprim

    martinusprim

    Joined:
    Jul 25, 2019
    Posts:
    11
    Hi, I'm trying to get my button clickable but I didn't manage to make it work. I tried the exemple of this thread https://forum.unity.com/threads/ui-builder-how-to-make-buttons-interactable.786701/ by uMathieu. What am I doing wrong ? Thank you for your help in advance.

    Code (CSharp):
    1. //Attatch this script to a Button GameObject
    2. using System;
    3. using System.IO;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6. using UnityEngine.UIElements;
    7. using Button = UnityEngine.UIElements.Button;
    8.  
    9. public class buttonScript : MonoBehaviour
    10. {
    11.     public VisualTreeAsset visualTreeAsset;
    12.     public StyleSheet defautStyleSheet;
    13.  
    14.     public void Start()
    15.     {
    16.         var root = new VisualElement();
    17.         visualTreeAsset.CloneTree(root);
    18.         root.styleSheets.Add(defautStyleSheet);
    19.  
    20.         var button = root.Q<Button>();
    21.         Debug.Log(button.name);
    22.         Debug.Log(button.text);
    23.         button.clickable.clicked += () => Debug.Log("Clicked!");
    24.     }
    25. }
    26.  
    27.  


    upload_2020-4-13_19-38-34.png
    upload_2020-4-13_19-40-56.png
     
  2. WizByteGames

    WizByteGames

    Joined:
    Mar 28, 2013
    Posts:
    70
    Add the button to the root object and that should fix it.

    Edit: Just realized You did that. have you trued ClickedWithEventInfo? Does that work for you?
     
    Last edited: Apr 15, 2020
  3. martinusprim

    martinusprim

    Joined:
    Jul 25, 2019
    Posts:
    11
    Hi, firstly thank you for your answer. So I've just tried to do that with your advice
    Code (CSharp):
    1.  public void Start()
    2.     {
    3.         var root = new VisualElement();
    4.         visualTreeAsset.CloneTree(root);
    5.         root.styleSheets.Add(defautStyleSheet);
    6.  
    7.         button = root.Q<Button>();
    8.         Debug.Log(button.name);
    9.         Debug.Log(button.text);
    10.         button.clickable.clickedWithEventInfo += (evt) => Debug.Log("clicked 2");
    11.         button.clickable.clicked += () => Debug.Log("Clicked!");
    12.  
    13.     }
    But this too didn't do anything. I've got the last version of UIElements Runtime 0.0.4 so I don't really understand what's going on.
     
  4. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    198
    Hi martinusprim,

    You've just created a clone of your uxml file, which has the proper clickable events registered, but it's not that one that you're seeing on your screen. That's because your uxml file is also set on your PanelRenderer, which already takes care of instantiating it. For example, try adding a label to your root (in your code), you'll see that it won't show up on your screen. Either you should query the button directly from
    Code (CSharp):
    1. button = GetComponent<PanelRenderer>().visualTree.Q<Button>();
    2. button.clickable.clicked += () => Debug.Log("Clicked!");
    , or you should add your new root to the PanelRenderer and not assign the uxml file in the Inspector.
     
  5. pegorari

    pegorari

    Joined:
    Nov 19, 2009
    Posts:
    59
    Try add an EventSystem component to your scene.

    upload_2020-4-15_13-4-50.png
     
  6. martinusprim

    martinusprim

    Joined:
    Jul 25, 2019
    Posts:
    11

    I thank both of you a lot, it works now. I've added the visualTree that was created with the PanelRenderer. I was also lacking an EventSystem.
     
  7. unity_TGAQ3M7QNLgzdg

    unity_TGAQ3M7QNLgzdg

    Joined:
    Jan 15, 2021
    Posts:
    1
    Hi martinusprim,

    Could you post your solution or edit the previous post? I can't get it to work even though I used the visualTree from the PanelRenderer and added an EventSystem. I'm using 2019.14.18f1 btw.