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. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question How do I update a VisualElement when a value changes in the game?

Discussion in 'UI Toolkit' started by BenjaminAlbrecht, Sep 19, 2022.

  1. BenjaminAlbrecht

    BenjaminAlbrecht

    Joined:
    Apr 27, 2019
    Posts:
    4
    I have a custom VisualElement that displays some values. These values are pulled from a singleton that stores some globally accessible information. The values can change as the game runs.

    I can get the initial values in the singleton from within the VisualElement's constructor. But, VisualElements do not contain an Update method. So I don't have an easy way to check if any of the values have changed. What is the correct way to update the visual element when the values change?

    I'm sorry if this is a stupid question. I'm new to UI Toolkit and I could not find any answers in the Documentation or by Googling. Maybe I just missed something.
     
  2. pierre_10

    pierre_10

    Unity Technologies

    Joined:
    Apr 1, 2016
    Posts:
    33
    We'll be releasing data binding very soon that's going to do exactly that. wait for announcements. Meanwhile, you can create your own update method using UITK schedulers, look it up in the docs. you basically create a scheduler that can call a method looped and start at any given time.
     
  3. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I do it by using one of these methods: one is to suscribe to an event, the second is to use a setter. I've never heard of schedulers before.

    Code (csharp):
    1.  
    2.      private UnityEvent<int> scoreChanged;
    3.      ...
    4.      scoreChanged = new UnityEvent<int>();
    5.      ....
    6.      // when the score changes:
    7.      scoreChanged.Invoke(newValue);
    8.  
    9.      // the other script adds a listener
    10.      private VisualElement scoreLabel;
    11.     ...
    12.      scoreChanged.AddListener(scoreChange);
    13.      ...
    14.      private void scoreChange(int newValue) {
    15.          scoreLabel.text = newValue.ToString();
    16.      }
    17.  
    Code (csharp):
    1.  
    2. private VisualElement scoreLabel;
    3. private int score;
    4. public int Score {
    5.    get {
    6.         return score;
    7.    }
    8.    set {
    9.       score = value;
    10.       scoreLabel.text = score.ToString();
    11.    }
    12. }
    13.  
    14. ...
    15. // setting Score will update the label
    16.  Score  = newValue;
    17.  
    You can also use both methods at the same time.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    Are you able to provide a code example of this?

    The absence of examples in the docs makes it really unclear how this potentially useful feature is meant to be used.