Author Topic: Tweens  (Read 44941 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: Tweens
« Reply #30 on: October 24, 2016, 07:13:50 AM »
Yeah I tend to use ?? quite a bit, NGUI is older code though. Thanks, I like that approach.

Edit: After further testing I realized that the ?? operator won't work in the editor properly after a widget has been destroyed. Unity uses a dummy object to facilitate the "trying to use XYZ but it has been destroyed" exceptions, so ?? returns 'true' on the left side, never reaching the right since ?? checks references not being null, rather than running a (bool) conversion or != check on the object. The bool conversion and "!= null" comparison would evaluate as 'false'.
« Last Edit: April 22, 2017, 12:46:24 PM by ArenMook »

The-Arrival

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Tweens
« Reply #31 on: November 05, 2016, 08:08:54 AM »
Ran into an issue trying to re-play a tween with code, I was trying to replay the tween with...
  1. tween.ResetToBeginning();
  2. tween.PlayForward();
This worked, but only if I set ignoreTimeScale off for some reason. Otherwise the tween would complete instantly.
From looking at UIPlayTween the following is correct. (And indeed works fine)
  1. tween.PlayForward();
  2. tween.ResetToBeginning();
Thought I'd point this out as it didn't seem obvious to me.

Thanks... this info was very helpful!

Also if you want to manually (re)set a tween to it´s from state, use: tween.Sample(0, true);

The-Arrival

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Tweens
« Reply #32 on: April 16, 2017, 06:09:07 PM »
Hey Aren,

i have the following situation:
Unity 5.5.1f3
NGUI 3.11.2

I´m making a TowerDefense Game

i have a pool of sprites in my UI, all of them carry a tweenposition (disabled by default, so they not play instantly). When a sprite is requested, it gets setup (which sprite to show), the tween gets configured (start/end pos and duration) and the tween is Played forward.
OnTweenFinish the tween gets back into the bool (which means mainly it gets flagged as pooled and the sprite gets disabled.
The tween is set to NOT ignore timeScale (since we have time warp effects and the UI also should animate faster for this very panel)

Most of the time that works fine, but at some point the tweens start to play instantatiously, even the setup is correct (start != end, duration != 0)

As stated in my last post/quote i also tried differen versions of the play/rest combinations.

i used tween.PlayForward() as well as tween.Play(true)
i tried different versions of resetting the tween, like right before or after the play call as well as in the onTweenFinished Callback.

The bug allways happens at the same time, right after the first Wave is finished... in the second Wave the Bug allready is there. So it might be that it´s my fault, but i´m checking my code since hours and i can´t find the difference between wave1 and wave2 and my usage of the tweens.

Any idea what i might do wrong or could that actually be a NGUI bug?

Thanks in advance!

EDIT:

after some additional tests it became more likely that it´s an issue with NGUI.

In my usecase the poolsize was 10 and i reused more or less the same 3 items. at the time the bug occured i was reusing an item whos tween got finished right before it got claimed and used again. So what i did is to make sure that the whole pool got used, before reusing the same item again.
That time the tweens work for much longer (more waves, so it was nothing which happened between wave 1 and 2).
But it got locked again after the first item got reused about 7-8 times... maybe some racing conditions which only sometimes occure?
« Last Edit: April 16, 2017, 07:26:19 PM by The-Arrival »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: Tweens
« Reply #33 on: April 22, 2017, 12:41:46 PM »
Did you try calling ResetToBeginning()? If it's already in progress, then it will continue to play from where it was when you play it forward again.

Edit: Noticed this is a continuation of our previous conversation from a few months back and you did use ResetToBeginning there. I assume you still do? Which order are you calling them in this time? Re-reading the post I assume "rest" actually meant "reset", which would imply that the answer is 'yes'. Post some code, that might help also.

P.S. Did you try printing the time and deltaTime that's retrieved at the top of DoUpdate() function? The only difference between ignoring timescale and not ignoring it are the values retrieved there.
« Last Edit: April 22, 2017, 12:49:54 PM by ArenMook »

The-Arrival

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Tweens
« Reply #34 on: April 26, 2017, 01:28:51 AM »
Hi,

yes i did call ResetToBeginning() and tried it on different positions. Right before PlayForward(), right after PlayForward() and in a callback method triggered by the OnFinished Event from your tweener.
I also tried myTransform.localPosition = startPosition in combination with myTweener.SetStartToCurrentValue();

Here are the three main methods currently used. i left the commented calls in, so you get an idea at which positions i tried resets:

  1. public void Setup(PublicEnums.Enemys enemy, bool shield, bool armor, Vector3 startPos, float duration) {
  2.         useCounter++;
  3.  
  4.         //Debug.Log("Setup Sprite: " + myID);
  5.         myRoot.localPosition = startPos;
  6.         mySprite.spriteName = SpriteUtils.getEnemyIconName(enemy);
  7.        
  8.         //Shield and Armor
  9.         if (armor) {
  10.             mySprite.color = Managers.PrefabManager.ui_armorColor;
  11.         }
  12.         if (shield) {
  13.             myShield.SetActive(true);
  14.         }
  15.  
  16.         isUsed = true;
  17.         mySprite.enabled = true;
  18.         myTweener.SetStartToCurrentValue();
  19.         myTweener.duration = duration;
  20.         myTweener.from = startPos;
  21.         myTweener.to = myEndPos;
  22.         //myTweener.ResetToBeginning();
  23.         //Debug.Log("Setup " + myID + ": start: " + startPos + ", duration: " + duration);
  24.     }
  25.     public void StartTween() {
  26.        
  27.         Debug.Log("Setup " + myID + ", pos: " + myRoot.localPosition + ": start: " + myTweener.from + ", end : " + myTweener.to + ", duration: " + myTweener.duration);
  28.         //myTweener.ResetToBeginning();
  29.         myTweener.PlayForward();
  30.         //myTweener.ResetToBeginning();
  31.     }
  32.  
  33.     public void onTweenFinished() {
  34.         myTweener.ResetToBeginning();
  35.         Debug.Log("OnTweenFinished: " + myID);
  36.         isUsed = false;
  37.         mySprite.enabled = false;
  38.         myRoot.localPosition = Vector3.zero;
  39.         myShield.SetActive(false);
  40.         mySprite.color = Color.white;
  41.     }
  42.  

Besides the Debuglogs you can see in the code samples i also added the ones you requested and another one when a tween gets reused from the pool (which is basically invisible and flagged tweens). In the attached screenshot you can see a debuglog output at the time the bug occures.

we use and setup a tween with the ID -47456. It has the right pos, start, end and duration values. three DoUpdates() later the OnFinished Events happen and my OnTweenFinished event is called

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: Tweens
« Reply #35 on: April 26, 2017, 07:09:01 AM »
In the log I see 2 log entries have the same time, yet different delta... why is that? I also don't see where that debug line is called. You should also keep the tween disabled. Calling PlayForward will already enable it, and that's something that should be done at the end after setting everything up. When you call Play/PlayForward/PlayReverse, it sets the "mStarted" flag -- but only if the tween is disabled. That's important.
« Last Edit: May 04, 2017, 05:25:45 PM by ArenMook »

The-Arrival

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Tweens
« Reply #36 on: May 02, 2017, 02:20:44 PM »
Log Entrys and Debug.Log calls:
I was adding the debug.log call directly in to the DoUpdate funktion in the UItweener... right after delat and time get set

  1. protected void DoUpdate ()
  2.         {
  3.                 float delta = ignoreTimeScale && !useFixedUpdate ? Time.unscaledDeltaTime : Time.deltaTime;
  4.                 float time = ignoreTimeScale && !useFixedUpdate ? Time.unscaledTime : Time.time;
  5.  
  6.         Debug.Log("delta: " + delta + ", time: " + time);
  7.  
  8.  

There are two identical entrys due to the fact that there is more then one tween active and posting their logs.

Enable/Disable:

As you can see in the code i don´t enable/disable the tween component, i only enable/disable a sprite-component and a child gameobject.

Sorry for the little delay in my answers but i´m currently jumping between projects. I´ll answer more frequently from now on.

One strange behaviour i found is, that i get a few of those Debug.Log calls with time and delta, whenever i hover over an UI Element. That might be due to the fact that you use the UITweener class also for button animations? In any case that facts makes it hard for me to use the IDE-Debugger to step through the code

Did you run some tests on your own with reusing/pooling the same tween?
« Last Edit: May 02, 2017, 03:13:25 PM by The-Arrival »

The-Arrival

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Tweens
« Reply #37 on: May 03, 2017, 03:34:58 PM »
I´m one step closer i guess and i exactly witnessed the situation in were it happens:

The last time that bugging tween worked was in a special situation. it was set to start roughly equals and and duration 0. As soon as the tween gets started it pretty much immediatly ends. That is on purpose and fine so far!
The next time that tween gets used though i starts to bu and instantly ends it´s animation, even it gets setup with start NOT equals end and duration = 5f;
It seems we have some weired racing condition going on here. My Debug.Log states the correct setup. I was even able to move to the exact frame that tween gets setup and started using the frame-by-frame debugging. In the frame it gets started, the inspector shows the duration of 5f, but my guess is, that it´s still starting with 0f from the last time. If i go one frame further it immediatly ends, calling the OnFinish callback.

I added a little offset to my durations of 0.1f to avoid the 0f duration and voilá, the bug is gone and the tween is behaving correctly.

I hope that helps you tracking down whats going wrong here
« Last Edit: May 03, 2017, 03:49:00 PM by The-Arrival »