Author Topic: Requesting help with setting up a tween  (Read 4080 times)

gribbly

  • Guest
Requesting help with setting up a tween
« on: February 02, 2013, 02:26:20 AM »
Hi, I am trying to move a panel using TweenPosition. I want it to move in smooth increments, i.e., something like:

(0,0,0) ---tween--> (-10, 0 ,0) ---tween--> (-20, 0, 0) ---etc.-->

However the problem I am having is that my tween correctly moves the panel from (0, 0, 0) to (-10, 0, 0). But when I repeat the process the panel pops back to (0, 0, 0) and repeats the same movement, rather than continuing on to (-20, 0, 0).

The tween is triggered by a button select event that calls a function that looks like this (I've simplified it by omitting some game-specific logic that isn't related to the tween):

  1. public void OnButtonSelect(int i){
  2.         //add a TweenPosition component to the panel (buttonPanel, assigned in inspector)
  3.     //if it doesn't already have one...
  4.         TweenPosition tp = buttonPanel.GetComponent<TweenPosition>();
  5.         if( tp == null){
  6.                 tp = buttonPanel.gameObject.AddComponent<TweenPosition>();
  7.         }
  8.        
  9.         Vector3 scrollFactor = new Vector3(-10.0f, 0, 0);
  10.         tp.from = buttonPanel.transform.position;
  11.         tp.to = tp.from + scrollFactor;
  12.        
  13.         tp.eventReceiver = this.gameObject;
  14.         tp.callWhenFinished = "OnPanelScrollDone";
  15.         tp.method = UITweener.Method.EaseInOut;
  16.         tp.duration = 1.5f;
  17.         tp.Play(true);
  18. }
  19.  
  20. public void OnPanelScrollDone(int i){
  21.         TweenPosition tp = buttonPanel.GetComponent<TweenPosition>();
  22.         Destroy(tp);
  23. }
  24.  
  25.  

Why does buttonPanel.transform.position reset every time? Why doesn't it stay where it was at the end of the tween?

BTW I tried to use NGUITools.AddWidget<> to add the TweenPosition, but couldn't work out how to do it - please correct me if using AddComponent isn't the right way to do this!

I also chose not to use TweenPosition.Begin because I couldn't see a way to configure the method (I want ease in/out).

Thanks for any help!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Requesting help with setting up a tween
« Reply #1 on: February 02, 2013, 08:29:18 AM »
You need to use the Begin method to activate a tween.
  1. TweenPosition tp = TweenPosition.Begin(targetGameObject, ...);
  2. tp.DoWhateverYouWant

gribbly

  • Guest
Re: Requesting help with setting up a tween
« Reply #2 on: February 02, 2013, 12:19:02 PM »
Thanks, I made that change and it simplifies my code. However the behavior doesn't change - the position of the panel object is reset every time I activate the tween.

Desired behavior:
//note we continue from where we stopped last activation
(0,0,0) ---tween--> (-10,0,0)
(-10,0,0) ---tween--> (-20,0,0)
(-20,0,0) ---tween--> (-30,0,0)

Current behavior:
//note we reset position at the start of each activation
(0,0,0) ---tween--> (-10,0,0)
(0,0,0) ---tween--> (-10,0,0)
(0,0,0) ---tween--> (-10,0,0)

Additional issue:
I noticed while debugging/loggging this problem that I'm getting two "OnSelect" trigger events from UIButtonTween. I have it configured to call a function "OnButtonSelect", and that function gets called twice every time a button becomes selected.

I wonder if that has something to do with why the panel position is getting reset?


EDIT: This duplicate event problem turned out to be correct behavior (I think) - each tweener was sending events. I just added some filtering in the receiver so I only react to one and this is no longer a problem.
« Last Edit: February 02, 2013, 07:21:47 PM by gribbly »

gribbly

  • Guest
Re: Requesting help with setting up a tween
« Reply #3 on: February 02, 2013, 02:27:40 PM »
OK hang on, I am checking out Example 7 - Scroll View... that looks very much like what I want. I will try to understand/copy the implementation there.

I'll report back how I go!

gribbly

  • Guest
Re: Requesting help with setting up a tween
« Reply #4 on: February 02, 2013, 06:06:00 PM »
Not making much progress here... there's a lot to wrap my head around  :-\

For example, Example 7 uses UIGrid, but it's really not clear to me how that script works. EDIT: I figured it out!

ArenMook, could you possibly give me some hints on the high level approach here. How would you set this up? I feel like I'm looking at it wrong.

I basically want the Scrolling Panel from Example 7, with the following difference:

* It needs to be controller driven, not mouse driven!
* It needs to handle an arbitrary number of items (I scan a list of levels at startup, and add one item per level, this could range from five to 50)

My approach so far has been to have an array of buttons, using UIButtonKeys to set next/prev and handle input. Then I use the OnSelect event to trigger selection logic. That seems like a good place to scroll the panel so that the currently selected button (or whatever) is centered.

Any guidance appreciated. I'll keep trying to figure out how Example 7 is working.
« Last Edit: February 02, 2013, 06:50:15 PM by gribbly »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Requesting help with setting up a tween
« Reply #5 on: February 03, 2013, 08:27:57 AM »
Did you look at how skyrim UI works? (video tutorial) -- seems to be closer to what you are trying to do, no?

gribbly

  • Guest
Re: Requesting help with setting up a tween
« Reply #6 on: February 03, 2013, 01:39:56 PM »
Ah, OK thankyou that was the final thing I needed! I now have it working as intended.

I learned a lot about how NGUI works in the process. I like it!