Author Topic: A small bug ?  (Read 5895 times)

thienhaflash

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
A small bug ?
« on: February 27, 2013, 10:52:58 AM »
I'm not sure if it's a bug or it's my fault but, anyway

I tried to add a TweenPosition onto a normal GameObject, and set from A to B. Now for this first time animation I want to start the animation in Reverse in stead of Forward (seems akward, but yes, I really need this: running reverse the first time), so I call Play(false) but the GameObject didn't move.

The first thing I tried is moving the GameObject else where before calling Play(false) : nothing happens.

After a while digging into the code, I found that the variable mFactor is set to 0 at the start, meaning that when I call Play(false) it will calculate mFactor += amountPerDelta * delta; which is resulted in a negative value ( as amountPerDelta is negative now ).

So that's why the tween being skip : invalid mFactor value.

The easiest fix I can think of is setting mFactor to -1 at the start then init the correct value when the tween actual starts (haven't tested). Is that right or I'm just making a terrible mistake ? Please advise.

p/s : I made a temporary fix by injecting 1 to mFactor at the start for this special case using Reflection, leaving NGUI library untouched, and seems that it's working fine.
« Last Edit: February 27, 2013, 10:55:29 AM by thienhaflash »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: A small bug ?
« Reply #1 on: February 27, 2013, 08:50:54 PM »
Playing in reverse at start... that does seem strange. Wouldn't it be easier to reverse To and From? In general I always advise using TweenPosition.Begin insitead of having an exiting tween and using a Play() method if you're going via code.

thienhaflash

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: A small bug ?
« Reply #2 on: February 27, 2013, 10:13:42 PM »
From what I see using TweenPosition.Begin still does not sort out my problem, the tween object still don't move.

I think although it does sound strange, it should work. Playing reverse the first time.

The reason I can not swap from / to for that object because I use the direction to track if the object is tweening out of view or into view and there are quite a few object tweening in and out at the same time. If I do reverse the from / to just to play in reverse I will never know which object is tweening out and which is tweening in as they are all going on Forward direction.

Tell me if it's reasonable doing so or if I'm missing something simple.

thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: A small bug ?
« Reply #3 on: February 28, 2013, 08:42:07 AM »
Are you using some old version of NGUI (like Free?)

Full version of NGUI handles Play(false) properly:
  1.         public void Play (bool forward)
  2.         {
  3.                 mAmountPerDelta = Mathf.Abs(amountPerDelta);
  4.                 if (!forward) mAmountPerDelta = -mAmountPerDelta;
  5.                 enabled = true;
  6.         }

leagueofmonkeys

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: A small bug ?
« Reply #4 on: July 10, 2013, 12:45:45 AM »
I'm having the same problem as thienhaflash.

I don't understand why Play() works the way it does...or doesn't work.  This doesn't seem to follow the KISS principle...hehe

I should be able to call Play(True) have it tween from start(from) to finish(to) or call Play(False) and have it tween from finish(to) to start(from).  This is the simplest and most logical way for this to work.

If I'm using TweenAlpha.Begin() as suggested how do I set properties of the tween like method, style, eventReceiver, callWhenFinished etc?

Thanks for reading :)

MGB

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: A small bug ?
« Reply #5 on: July 10, 2013, 07:22:58 AM »
You tried calling Reset() before the Play(false) call?

leagueofmonkeys

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: A small bug ?
« Reply #6 on: July 10, 2013, 07:51:30 AM »
Thanks for the reply.  I had tried, didn't help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: A small bug ?
« Reply #7 on: July 10, 2013, 12:54:41 PM »
TweenAlpha tw = TweenAlpha.Begin();
tw.whatever = 123

leagueofmonkeys

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: A small bug ?
« Reply #8 on: July 10, 2013, 08:12:49 PM »
Thanks for the reply ArenMook, can you please elaborate a little.  Have you successfully tested this?  I tried your suggestion and it didn't work, same problem.

I'm simply trying to setup a system for screen fading in and out of black.  I had set up my own but scrapped it when I realised I could use TweenAlpha to do it, sadly I can't get it to work in any sort of logical and consistent manner  :'(
« Last Edit: July 10, 2013, 08:33:53 PM by leagueofmonkeys »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: A small bug ?
« Reply #9 on: July 10, 2013, 08:50:44 PM »
To elaborate, if you were wanting to fade in a panel, you'd do this:
  1. UIPanel panel = GetComponent<UIPanel>();
  2. panel.alpha = 0f;
  3. TweenAlpha tw = TweenAlpha.Begin(panel.gameObject, 0.25f, 1f);
  4. tw.method = UITweener.Method.EaseInOut;
To summarize: change the current state to what it should start at, then begin the tween with what it should go to.

leagueofmonkeys

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: A small bug ?
« Reply #10 on: July 11, 2013, 12:03:01 AM »
Thanks ArenMook.  That seems to work for everything except if I try and run it too soon after a scene loads.  I have to yield for 0.1 seconds for it to work.  If I use the tween delay instead of yielding it doesn't work.  I've tried experimenting with the script execution order of my scripts to no avail.

AtomicBob

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 69
    • View Profile
Re: A small bug ?
« Reply #11 on: July 11, 2013, 11:08:25 AM »
I'm in the same boat thienhaflash was in. It's incredibly difficult trying to get a tween to play backwards the first time. It may seem obtuse at first, but I've actually run into this problem several times in the last few months.

A great example is when you have multiple, exclusive UI elements that can be switched between. You'd think you could just set up each UI element in it's intended place and set up a TweenPosition that moves it onto the screen, then call Play(false) and Play(true) to tween one out of the way and a new one in. The problem is that the UI element that starts on the screen (without having called Play(true)) will have Play(false) called first but it will fail and just jump to the final position the very first time it is called. After that, it works fine.

This seems to happen no matter what I do to set up the tween to be in the final state. Even with the GameObject in the final position, Sample(1, true) called, etc.

EDIT: Finally found a hacky way to deal with this. The problem is that mFactor is initialized to 0, so when you try to play in reverse, UITweener.Update sees it's already where it needs to be, and does nothing. UITweenerReset has this nifty line: mFactor = (mAmountPerDelta < 0f) ? 1f : 0f which gives me a way to get mFactor to 1 without tweening forward as long as mAmountPerDelta is < 0. However, it's initialized to 1. Luckily, Play(false) forces it to be negative. Thus:

  1. tweener.Play(false);    //Sets mAmountPerDelta to -1
  2. tweener.Reset();        //Sets mFactor to 1
  3. tweener.Play(false);    //Plays the tween in reverse
  4.  

actually allows you to play the tween in reverse, without ever needing to play it forward first.

That wasn't fun.
« Last Edit: July 11, 2013, 12:04:50 PM by AtomicBob »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: A small bug ?
« Reply #12 on: July 11, 2013, 07:06:05 PM »
I'd like to point out that you should simply have your windows start in the "disabled" state (faded out/disabled), then fade them in as you need them. In Starlink I use ActiveAnimation.Play to activate disabled windows and fade them in. In fact, Starlink UI kit actually has a simple to use window transition framework that makes all this quite easy and handles navigating back / keeps history.

AtomicBob

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 69
    • View Profile
Re: A small bug ?
« Reply #13 on: July 11, 2013, 07:13:48 PM »
This is something I considered. The downside is that you can't just enable objects to see them in the Editor in that case. You'd have to unfade or move them into place if you want to look at things as a whole. How do you handle this on your own projects?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: A small bug ?
« Reply #14 on: July 11, 2013, 07:17:36 PM »
Why can't you? Select the object you want to hide, click the "active" button or hit ALT+SHIFT+A. Select the object you want to show, and do the same thing.