Search Unity

UI element binding not working anymore

Discussion in 'UI Toolkit' started by fomafomitch, Sep 27, 2021.

  1. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    89
    Hello,

    I did a binding with a Boss health bar and a UI Elements yesterday. It was working yesterday, I didn't change anything and today it's not working.

    Script on my Game object in game "BossHealth.cs":
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. [System.Serializable]
    7. public class BossHealth : MonoBehaviour
    8. {
    9.     public int maxHealth;
    10.     public int currentHealth;
    11.     public float percentHealth;
    12.  
    13.     private void Start()
    14.     {
    15.         maxHealth = 3000;
    16.         currentHealth = 2000;
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         percentHealth = ((float)currentHealth / maxHealth) * 100;
    22.     }
    23.  
    24. }
    Script for my UI
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using UnityEngine.UIElements;
    6. using UnityEditor.UIElements;
    7. using System;
    8.  
    9. public class bind_boss_bar : MonoBehaviour
    10. {
    11.     private VisualElement m_Root;
    12.     private Label m_BossName;
    13.     private ProgressBar m_BossLife;
    14.     private VisualElement m_BossVisualElement;
    15.  
    16.     public GameObject bossScript;
    17.     public float bossCurrentHealth;
    18.     public float bossMaxHealth;
    19.     public float bossPercentHealth;
    20.  
    21.     void OnEnable()
    22.     {
    23.         m_Root = GetComponent<UIDocument>().rootVisualElement;
    24.         m_BossName = m_Root.Q<Label>("raid-boss-name");
    25.         m_BossLife = m_Root.Q<ProgressBar>("raid-boss-life");
    26.         m_BossVisualElement = m_Root.Q<VisualElement>("raid-boss-progress-bar");
    27.  
    28.         //Access game object and get value
    29.         bossScript = GameObject.Find("Boss");
    30.         bossCurrentHealth = bossScript.GetComponent<BossHealth>().currentHealth;
    31.         bossMaxHealth = bossScript.GetComponent<BossHealth>().maxHealth;
    32.  
    33.         //Update progress bar
    34.         m_BossLife.value = ((float)bossCurrentHealth / bossMaxHealth)*100;
    35.  
    36.         SerializedObject so = new SerializedObject(bossScript.GetComponent<BossHealth>());
    37.         m_Root.Bind(so);
    38.         m_BossLife.bindingPath = "bossPercentHealth";
    39.     }
    40.  
    41. }
    And in my UI Elements I set :
    Binding path : "bossPercentHealth"

    It only works if I add this code on my script for my UI , but it's not a binding at all...
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         bossCurrentHealth = bossScript.GetComponent<BossHealth>().currentHealth;
    4.         bossMaxHealth = bossScript.GetComponent<BossHealth>().maxHealth;
    5.         m_BossLife.value = ((float)bossCurrentHealth / bossMaxHealth)*100;
    6.     }
     
    Last edited: Sep 27, 2021
  2. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @fomafomitch ,

    At the moment, data binding is not supported for runtime use cases like yours. Data binding is currently limited to inspecting Unity objects in the context of the Editor UI (such as inspectors).

    In the future, we plan to expand the support for data binding by supporting binding from a more varied set of data sources to any Visual Element property (not just the “value” property), in a way that works in the Player.
     
    Lintukori likes this.
  3. fomafomitch

    fomafomitch

    Joined:
    Nov 22, 2020
    Posts:
    89
    Ok, so I need to set up "event callback" for my UI ?

    Do you have an ETA for this feature ?

    Thank you for your work.