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

Question VisualElement with Update function?

Discussion in 'UI Toolkit' started by TheCelt, May 18, 2023.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    I want to animate my custom VisualElement by calling a function in the script of my own and have it animate to a value every frame.

    But the VisualElement does not have access to Update/LateUpdate so how exactly do I implement animations in scripts such as loading a progress bar to a specified % over a specific animation time with our own custom easing function ? I'm trying to avoid using monobehaviours because that kinda defeats the benefit of UI Toolkit if i have to make a ton of scripts in monobehaviours again.
     
  2. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    You need to run a coroutine typically (which runs on monobehaviour). Monobehaviour is how Unity does its frame by frame operations. Or you could put it in an Update. You can have one game object that runs all your coroutines or update functions for example.
     
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    Hm I see. Would be nice if they added update method to visual element so we can do animations without monobehaviours, I like the disconnect the ui toolkit does compared to the old system
     
  4. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    743
  5. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    VisualElements do come with a schedule property that can be used to execute a method periodically. It doesn't do the whole animation for you, but if you already planned to use Update/Late Update for this, it may work for you.

    You could do something like this:
    myprogessbar.schedule.Execute(UpdateProgressBar).Every(16);

    You could run this code when initializing your UI.

    UpdateProgressBar would be a method that moves the value of your progress bar closer to your target value. It can receive a TimerState parameter that contains the current time delta if you need it. 16 is the desired number of milliseconds between each call. I think you can pass something like 1 instead if you just want it to run as fast as possible. Or you could use
    Until(() =>false)
    instead of
    Every(16)
    .
     
    spiney199 and Onigiri like this.
  6. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    721
    Thanks i will read up more on this. Where did you hear about the 16ms being desired?
     
  7. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    Sorry, I meant to say that the "16" in the example stands for the number of milliseconds you desire between each call. 16 is a commonly used number because a single frame at 60fps lasts 16.6666ms, but you can use whatever number you need.
     
    TheCelt likes this.