Search Unity

Is it possible to bind a method call or Action to a button element

Discussion in 'UI Toolkit' started by tmhbrts, Oct 29, 2019.

  1. tmhbrts

    tmhbrts

    Joined:
    Dec 24, 2013
    Posts:
    13
    Hi,

    I was wondering if it is possible to bind a method call or Action to a button element.

    Imagine the following:
    Code (USS):
    1.  
    2. <UXML xmlns:ui="UnityEngine.UIElements" xmlns:ue="UnityEditor.UIElements">
    3.   <ui:VisualElement class="toolbar">
    4.     <ui:VisualElement class="toolbar-container">
    5.       <Style path="Assets/Editor/Resources/ToolbarStyle.uss" />
    6.       <ui:Label class="toolbar-element" text="Number of Blocks" />
    7.       <ue:IntegerField class="toolbar-element integer" binding-path="NumberOfBlocks" />
    8.       <ui:Button class="toolbar-element" text="Create Tower" binding-path="CreateTower" />
    9.     </ui:VisualElement>
    10.   </ui:VisualElement>
    11. </UXML>
    VisualElement with class 'toolbar-container' is bound to a class with a public int NumberOfBlocks and a static void CreateTower.
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    We don't have any immediate plans to support action bindings in UXML but it's something we'll keep in mind as we work on a runtime-capable binding system for UIElements.
     
  3. PeppeJ2

    PeppeJ2

    Joined:
    May 13, 2014
    Posts:
    43
    Bump. Has there been any progress on supporting this with the new releases? If it is supported already, where can I find docs?
     
  4. sebastiend-unity

    sebastiend-unity

    Unity Technologies

    Joined:
    Nov 9, 2015
    Posts:
    184
    Still not possible however we are working on a data bindings solution as we speak. I can't guarantee we will have event/action/callback bindings too but I'm adding your voice to the requests already made. Happy Holidays!
     
    PeppeJ2 likes this.