DOTween and for's
« on: October 15, 2015, 08:03:13 PM »
Hey,

I've looked around but couldn't find anyone with this issue so maybe I am doing something wrong. My game uses NGUI for practically everything UI-wise, and I was until recently using HOTween to animate my buttons colors and multiple properties really. It was working nicely but I decided to upgrade to DOTween as its better, accordingly to you =P.

However, apparently it is not being able to tween the color of NGUI Widgets for some reason. Here is the call I am using:
_tweenColor = DOTween.To(() => _childWidgets.color, x => _childWidgets.color = x, _pressedColor , _currentAnimationDuration).SetAs(_tweenParams);

And the _tweenParams in this occasion:
_tweenParams.SetEase (Ease.InOutSine).SetUpdate(true);

In short, its just not working =/ Meanwhile, this other call is working as expected:
_tweenHighlight = DOTween.To(() => HighlightSprite.alpha, x => HighlightSprite.alpha = x, 1f, _currentAnimationDuration).SetAs(_tweenParams);

So... any idea on what might be causing this issue? The color doesn't get tweened at all, not even changed =/.

PS: I already checked, the code is being called.

Thanks in advance,
Allan
« Last Edit: November 09, 2015, 01:29:38 AM by Schwarzenego »

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: DOTween and NGUI
« Reply #1 on: October 15, 2015, 08:57:13 PM »
Hi,

Yes, DOTween is so much better than HOTween, you did good in changing :)

But! What is this issue? Your code seems perfect, and that color must change. The only thing that comes to my mind is that something else is resetting those colors between each update for some reason. Could you make a test, adding an OnUpdate callback to see if the _childWidgets.color is actually changing? If it is, that confirms that something between the DOTween update and the camera render is changing those colors back. If it isn't... well, that can't be :B
Code: [Select]
_tween.Color.OnUpdate(()=> Debug.Log(_childWidgets.color));

Cheers,
Daniele

Re: DOTween and NGUI
« Reply #2 on: October 15, 2015, 09:25:50 PM »
Hey Daniele, thanks for the quick reply!

I've made the following test:
Code: [Select]
Debug.Log ("onClick TweenColor");
_tweenColor[i] = DOTween.To(() => _childWidgets[i].color, x => _childWidgets[i].color = x, _pressedColor [i], 1).SetAs(_callbackRegistered ? _tweenParams : _tweenParamsCallback);
_tweenColor[i].OnUpdate(()=> Debug.Log(_childWidgets[i].color));

It does output "onClick TweenColor", but nothing else =(. For some reason it seems that OnUpdate is not getting called...

Let me go into more detail in this class of mine. I create the "tweenparams" on Start, and use it repeatedly on multiple, tons of tweens depending on each button configuration. These tweens are triggered by button events such as clicks, and everytime they are triggered, I run a method that basically looks at all running tweeners and kill them. So basically in the same frame I have a call to Kill (if it exists), and later in the same frame I run DOTween.To()... is that a problem?

Anyway, any more ideas of what could be causing it? =(

Anyway, thanks for the attention!

Re: DOTween and NGUI
« Reply #3 on: October 15, 2015, 09:33:03 PM »
So, I've decided to make a smarter test =P

I put the following code into a pseudo button:
Code: [Select]
using UnityEngine;
using System.Collections;
using DG.Tweening;

public class UIButtonTest : MonoBehaviour
{
private UISprite _childWidget;
private Tweener _tweenColor;
private TweenParams _tweenParams;

private void Start ()
{
_childWidget = GetComponent<UISprite> ();
_tweenParams = new TweenParams ();
_tweenParams.SetEase (Ease.InOutSine).SetUpdate (true);
}

protected virtual void OnClick()
{
Debug.Log ("onClick TweenColor");
_tweenColor = DOTween.To(() => _childWidget.color, x => _childWidget.color = x, Color.gray, 1).SetAs(_tweenParams);
_tweenColor.OnUpdate(()=> Debug.Log(_childWidget.color));
}
}

When clicked, output is only "onClick TweenColor" =(. Any ideas why?

Re: DOTween and NGUI
« Reply #4 on: October 15, 2015, 10:17:52 PM »
Ok apparently I was being really dumb at some point. I entered the preferences panel and turned on "AutoPlay" and it worked -.-. Sorry for the extra bother there! However, even now, on my previous code it doesn't work, even though I have basically the same setup only in a much more complex class. Only thing I can imagine is the "Kill" command happening in the same frame as the "Dotween.To" might be causing some kind of problem... is that even possible?

Thanks!

Re: DOTween and NGUI
« Reply #5 on: October 15, 2015, 11:40:25 PM »
Update: I removed the "Kill" calls in the same frame... I forced a "Play" call right after "DOTween.To"... no effect =(, whats strange is that at times I have multiple tweens running at the same time, for example, the color, scale and a highlight sprite tweening alpha, and they all work except for the color tween... considering the code is very much the same except for it being a color... I don't understand how that can not be working, when in the example I posted a few minutes ago it does work =(.

Re: DOTween and NGUI
« Reply #6 on: October 16, 2015, 12:25:44 AM »
Update 2:

So, apparently calling this works:
Code: [Select]
_tweenColor [0] = DOTween.To (() => _childWidgets [0].color, x => _childWidgets [0].color = x, _disabledColor [0], _currentAnimationDuration).SetAs(_tweenParams);
But calling this doesn't work:
Code: [Select]
for (byte i = 0; i < _tweenColor.Length; i++)
_tweenColor [i] = DOTween.To (() => _childWidgets [i].color, x => _childWidgets [i].color = x,_disabledColor [i], _currentAnimationDuration).SetAs(_tweenParams);

That is testing in the same object, same script, same place, same everything... which means _tweenColor and all others count is greater than one, and the code is being called... so it leads me to believe that the "for" is whats causing this trouble... which makes no sense to me =P.

Re: DOTween and NGUI
« Reply #7 on: October 16, 2015, 02:10:39 AM »
Update 3:

This works:
Code: [Select]
DOTween.To(() => _childWidget[0].color, x => _childWidget[0].color = x, Color.gray, 1).SetAs(_tweenParams);
DOTween.To(() => _childWidget[1].color, x => _childWidget[1].color = x, Color.gray, 1).SetAs(_tweenParams);
DOTween.To(() => _childWidget[2].color, x => _childWidget[2].color = x, Color.gray, 1).SetAs(_tweenParams);

This doesnt:
Code: [Select]
for (int i = 0; i < _childWidget.Length; i++)
DOTween.To(() => _childWidget[i].color, x => _childWidget[i].color = x, Color.gray, 1).SetAs(_tweenParams);

I don't even understand how that could possibly be even a bug... hahah how is it possible that just because its inside a loop it doesnt work? =(
Anyways, I think enough of my spam now, but please, if anyone has a clue, let me know!

Re: DOTween and NGUI
« Reply #8 on: October 16, 2015, 04:06:21 AM »
Alright, promise, last update =P:

This works (not sure if surprisingly or unsurprisingly):
Code: [Select]
protected virtual void OnClick()
{
for (int i = 0; i < _childWidget.Length; i++)
Animate (i);
}

private void Animate(int i)
{
DOTween.To(() => _childWidget[i].color, x => _childWidget[i].color = x, Color.gray, 1).SetAs(_tweenParams);
}

So yeah... "for"s are evil -.-

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: DOTween and NGUI
« Reply #9 on: October 17, 2015, 02:45:20 PM »
Hi, and sorry for the delay (but great tests you made there :)).

Woah, that seems this same issue here (due to Unity's old NET implementation), but it should happen only with foreach loops, not simple for. I'm gonna inspect that more, though in the meantime you can still use a for loop by redeclaring the variable inside it:
Code: [Select]
// Won't work
for (int i = 0; i < _childWidget.Length; i++)
DOTween.To(() => _childWidget[i].color, x => _childWidget[i].color = x, Color.gray, 1).SetAs(_tweenParams);
// Should work
for (int i = 0; i < _childWidget.Length; i++)
        ChildWidgetType c = _childWidget[i]; // Replace with the correct type of _childWidget
DOTween.To(() => c.color, x => c.color = x, Color.gray, 1).SetAs(_tweenParams);

Cheers!
Daniele

Re: DOTween and for's
« Reply #10 on: November 09, 2015, 01:31:53 AM »
Hey,

Don't worry about the delay, I delayed in answering myself =P

Thanks for the hint! Right now Im going with the "method call" solution as it was working for me and since there isnt much difference performance wise from declaring new variable to calling a method I will leave it at that, too much work for too little haha, but anyway, thanks for the help!

I editted the post's title so it is more informative to people in the future, it was previously "DOTween and NGUI" but that is not the issue right?

Anyway, thanks for the support and for the great, awesome plugin!