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 How to use DOTween on VisualElement using generic method?

Discussion in 'UI Toolkit' started by Mawthra, Jun 7, 2023.

  1. Mawthra

    Mawthra

    Joined:
    Apr 16, 2023
    Posts:
    8
    I'm trying to manipulate some VisualElement style attributes with DOTween in a c# script and having no luck. Basically this is what I have (below) that works for a hard coded number on the element when hitting the Play button in the GameView (it moves to 600 on its left position). I've also added "using DG.Tweening;" at the top of the script and get code completion on it, etc.

    Code (CSharp):
    1. VisualElement root = GetComponent<UIDocument>().rootVisualElement;
    2. root.Q<VisualElement>("myBox").style.left = 600;
    What I want to do is replace that "600" in that example with a tween to move the item over time. I've found this usage example of the generic method:

    Code (CSharp):
    1. DOTween.To(()=> myFloat, x=> myFloat = x, 52, 1);
    But I keep getting errors like "
    Cannot implicitly convert type 'UnityEngine.UIElements.StyleLength' to 'ulong'" when replacing myFloat with the myBox.style.left in both places

    If anyone can help point me in the right direction I'd be very grateful :)
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
  3. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    743
    Just use the .OnUpdate and set myBox.style.left = myFloat
     
  4. Mawthra

    Mawthra

    Joined:
    Apr 16, 2023
    Posts:
    8
    Thanks for the response @MousePods ... I'm super new to Unity, so when you say the .OnUpdate, is that default entry in a c# script like this?
    Code (CSharp):
    1. // Update is called once per frame
    2.     void Update()
    3.     {
    4.        
    5.     }
    Or do I call on that .OnUpdate some other way? TIA
     
  5. Mawthra

    Mawthra

    Joined:
    Apr 16, 2023
    Posts:
    8
    I saw they had a "generic" option that says "can be used to manipulate any value", which is why I tried that first, because I was using it with GameObjects on the UGUI system and was pretty straightforward, I didn't know transitions in UI Toolkit could be anything but hovers and clicks :) I'm brand new to Unity in general and trying to learn the UI tools... even reading the documentation is sometimes hard because they don't always reference examples
     
  6. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    743
  7. theunsigned

    theunsigned

    Joined:
    Nov 15, 2018
    Posts:
    32
    I'm curious about how to use .OnUpdate as well. I'm also just starting to use DoTween and am confused.

    I managed to do some basic tweening on UI Toolkit Elements but things are not working quite right. Here's what worked for me as a starting point:

    Code (CSharp):
    1.                 DOTween.To(() => button.transform.scale, x => button.transform.scale = x, new Vector3(.75f, .75f, .75f), .5f).SetEase(Ease.InOutSine).Play();
    2.