Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by howlmortar · Apr 29, 2021 at 05:48 PM · styletweenuitoolkittweening

Using DOTween to animate the 'Opacity' property in a VisualElement

For anyone who's familiar with 'DOTween' and/or UIElements, I'm trying to tween the opacity of a VisualElement from 0 -> 1, 1 -> 0 and it doesn't seem to be working. Is this because of my code, DOTween, or because this is not yet supported in UIElements? This is the function I made:

 void MenuTransition(VisualElement menu, float t)
 {
     float opacity = menu.style.opacity.value;
     if(menu.style.opacity.value == 0)
     {
         DOTween.To(() => opacity, x => opacity = x, 1f, t);
         menu.focusable = true;
     }
     else if(menu.style.opacity.value == 1)
     {
         menu.focusable = false;
         DOTween.To(() => opacity, x => opacity = x, 0f, t);
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Apr 29, 2021 at 10:38 PM 0
Share

UIElements/UIToolkit supports all kinds of tweens. Just the ones that work.

 float opacity = menu.style.opacity.value;

float opacity is a stack-level copy that becomes captured by value into these lambda expression objects (completely separate heap allocations). So reading and changing it's value in a lambda is futile. In other words this:

  DOTween.To(() => opacity, x => opacity = x, 1f, t);

Will be converted into +- this:

 class lambda_getter_123456
 {
     public float field1;
     public float Get =>  this.field1;
 }
 class lambda_setter_342738
 {
     public float field1;
     public float Set =>  this.field1 = value;
 }
  DOTween.To(
       new lambda_getter_123456{field1=opacity} ,
       new lambda_setter_342738{field1=opacity} ,
       1f , t
  );

What you may want to do instead is something like this:

 var style = menu.style;
 DOTween.To( () => style.opacity , x => style.opacity = x , 1f , t );

where style references a memory location that this ui system actually reads.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Dec 01, 2021 at 12:10 PM

UI Toolkit ships with a dedicated tweening api

(experimental but works fine at the time of writing this post)

ui toolkit elements tweening easing

 using UnityEngine;
 using UnityEngine.UIElements;
 
 public class RuntimeUiAnimator : MonoBehaviour
 {
     [SerializeField] UIDocument _uiDocument = null;
     void OnEnable ()
     {
         var ROOT = _uiDocument.rootVisualElement;
 
         ROOT.Query("animated").ForEach( (next) => {
             var zeroToOne = next.experimental.animation
                 .Start( 0f , 1f , 1000 , (ve,value) => ve.style.opacity = value )
                 .Ease( UnityEngine.UIElements.Experimental.Easing.InOutQuad )
                 .KeepAlive();
             var oneToZero = next.experimental.animation
                 .Start( 1f , 0f , 1000 , (ve,value) => ve.style.opacity = value )
                 .Ease( UnityEngine.UIElements.Experimental.Easing.InOutQuad )
                 .KeepAlive();
             var idle = next.experimental.animation
                 .Start( 1f , 1f , 1000 , (ve,value) => {} )
                 .KeepAlive();
             
             zeroToOne.OnCompleted( ()=> idle.Start() );
             idle.OnCompleted( ()=> oneToZero.Start() );
             oneToZero.OnCompleted( ()=> zeroToOne.Start() );
             
             zeroToOne.Start();
         } );
     }
 }



gif-01122021-13-09-16.gif (16.0 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image danielmahon · Dec 01, 2021 at 01:56 PM 1
Share

If this was in response to my recent comment (which I deleted shortly after posting because it turned out to be user error) thanks for responding.

DOTween seems to work fine for animating UI elements, including opacity. USS transitions works as well as does the experimental UI. Though DOTween just makes animation composition easier.

I got stuck for awhile because I had accidentally added a USS transition to my element via UI Builder. So it was interfering with DOTween. Once I removed it worked fine.

avatar image andrew-lukasik danielmahon · Dec 01, 2021 at 02:19 PM 0
Share

Yes, since this thread received a small ping from you so I thought I just may as well update my answer to point ppl toward built-in solution that will hopefully become something easier to start toying with.

Oh I didn't even noticed that USS transitions became available already, thx.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

256 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

DOTween, tk2dTextMesh and float 0 Answers

LeanTween, Tween canceled but still true when check isTweening. 0 Answers

transform position and dotween 0 Answers

Is there in Unity something like Tween-effects? 1 Answer

How do I update an image on a visual element at runtime in ui toolkit? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges