Tweens in Sequences: A quick pointer in the right direction!
« on: November 13, 2015, 02:00:00 PM »
Hello and thanks for making this awesome tool so awesome! :)

I have been using DOTween alot but have run into some problems wrapping my head around how to use and control tweens in sequences.

I want to be able to define tweens and then insert them into a Sequence to manage them all at once using .Kill() / .Pause() / etc on the sequence rather than having to do that on every tween. Not being a very good programmer I need to trial and error myself forward and sometimes it works sometimes it don't, - my problem is I don't know why?

So if someone could enlighten me why, for example, is the below code not performing as 'intended' (and instead ignoring the AppendIntervals & Not Pausing)?

Code: [Select]
     Sequence mySequence = DOTween.Sequence();

     Tween anchorMinTween = null;
     Tween anchorMaxTween = null;
     Tween anchorPositionTween = null;
     Tween scaleTween = null;

    void PlayTween()
    {       
        anchorMaxTween = DOTween.To(() => rectTrans.anchorMax, v => rectTrans.anchorMax = v, new Vector2(0.5f,0.5f), 1f)
           // .Pause()
           .SetDelay(2f)
            ;

        anchorMinTween = DOTween.To(() => rectTrans.anchorMin, v => rectTrans.anchorMin = v, new Vector2(0.5f, 0.5f), 1f)
           // .Pause()
           .SetDelay(2f)
            ;

        anchorPositionTween = DOTween.To(() => rectTrans.anchoredPosition, v => rectTrans.anchoredPosition = v, Vector2.zero, 1f)
            //.Pause()
            .SetDelay(2f)
            ;

        scaleTween = DOTween.To(() => rectTrans.localScale, v => rectTrans.localScale = v, Vector3.zero, 1f)
            //.Pause()
            .SetDelay(2f)
            ;

        mySequence
            .AppendInterval(5f)
            .Append(anchorMaxTween)
            .Join(anchorMinTween)
            .Join(anchorPositionTween)
            .AppendInterval(2f)
            .Append(scaleTween)
            //.SetDelay(2f)
            //.Play()
            .Pause()
            ;
    }
}

I'm probably missing something fundamental, but I have been looking to find a solution to what's wrong in my thinking for days and feel I need some pro directions :)

Thanks again!

Cheers!!

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: Tweens in Sequences: A quick pointer in the right direction!
« Reply #1 on: November 13, 2015, 04:35:41 PM »
Hi, glad you like DOTween :)

I have a couple of doubts about your code. Are you paying attention not to reuse the same Sequence multiple times (that can't be done)? Also, are you using the latest version of DOTween (1.1.060)?

Cheers,
Daniele

Re: Tweens in Sequences: A quick pointer in the right direction!
« Reply #2 on: November 13, 2015, 05:44:51 PM »
It's super! Great work :)

What you see there is pretty much all there is, so I am not using the Sequence multiple times.

Here is the complete code:

Code: [Select]
using UnityEngine;
using System.Collections;
using DG.Tweening;
using UnityEngine.UI;


public class TweenScript : MonoBehaviour {

    RectTransform rectTrans = null;

    void Start()
    {
        DOTween.Init();
        rectTrans = GetComponent<RectTransform>();
        PlayTween();
    }

    Sequence mySequence = DOTween.Sequence();

     Tween anchorMinTween = null;
     Tween anchorMaxTween = null;
     Tween anchorPositionTween = null;
     Tween scaleTween = null;

    void PlayTween()
    {       
        anchorMaxTween = DOTween.To(() => rectTrans.anchorMax, v => rectTrans.anchorMax = v, new Vector2(0.5f,0.5f), 1f)
           // .Pause()
           .SetDelay(2f)
            ;

        anchorMinTween = DOTween.To(() => rectTrans.anchorMin, v => rectTrans.anchorMin = v, new Vector2(0.5f, 0.5f), 1f)
           // .Pause()
           .SetDelay(2f)
            ;

        anchorPositionTween = DOTween.To(() => rectTrans.anchoredPosition, v => rectTrans.anchoredPosition = v, Vector2.zero, 1f)
            //.Pause()
            .SetDelay(2f)
            ;

        scaleTween = DOTween.To(() => rectTrans.localScale, v => rectTrans.localScale = v, Vector3.zero, 1f)
            //.Pause()
            .SetDelay(2f)
            ;

        mySequence
            .AppendInterval(5f)
            .Append(anchorMaxTween)
            .Join(anchorMinTween)
            .Join(anchorPositionTween)
            .AppendInterval(2f)
            .Append(scaleTween)
            //.SetDelay(2f)
            //.Play()
            .Pause()
            ;
    }
}

I am using the latest version 1.1.0.60 of DOTween. I couldn't attach a zip of the example project (too large), but to replicate just drag the above code to a Raw Image object with a UI Canvas Parent.

Where do you think I might be doing things wrong?

Thanks!

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: Tweens in Sequences: A quick pointer in the right direction!
« Reply #3 on: November 14, 2015, 05:37:42 PM »
Hello!

Took the time to try it out. First one note: you can't initialize the Sequence variable by calling DOTween.Sequence. You have to do that inside a method (Start or whatever) or you'll get a thread-related error. I don't think that was your case though, since that happens only if the script is called in the first frame of your project.

Everything else works perfectly here, which means:
- The Sequence starts
- After 7" anchorMax plays (interval of 5" plus 2" delay set on tween)
- After 2" more anchorMin plays (join with previous plus 2" delay)
- After 2" more anchorPos plays (join with previous plus 2" delay)
- After 5" more scale plays (append after the end of the previous - which lasts 1" - plus 2" interval plus 2" delay)

Also, the Pause, if set, is correctly working too. Let me know.

Cheers,
Daniele