Author Topic: [Solved] Setting the position of UIDraggablePanel  (Read 5252 times)

Aki Kanerva

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
[Solved] Setting the position of UIDraggablePanel
« on: February 27, 2013, 04:12:25 AM »
I am developing a level selection menu that's essentially a long, scrollable list of items using UIDraggablePanel and UIGrid. I'm trying to find a way to initialize the scroll position so that the last played level is visible. However, calling SetDragAmount() on the UIDraggablePanel seems to do weird things: it moves the entire panel as well as the contents.

To test this, open NGUI/Examples/Scenes/Example 7 - Scroll View (Panel).unity, then add the following script to the Click Me button (and disable the UIButton Tween component):

TestDragPanel.cs
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TestDragPanel : MonoBehaviour {
  5.     void Start() {
  6.         UIEventListener.Get(this.gameObject).onClick += onButtonClick;
  7.     }
  8.  
  9.     void onButtonClick(GameObject button) {
  10.         GameObject gob = GameObject.Find("UIPanel (Clipped View)");
  11.         UIDraggablePanel draggablePanel = gob.GetComponent<UIDraggablePanel>();
  12.         draggablePanel.SetDragAmount(0.25f, 0f, true);
  13.     }
  14. }

I would assume that the code would move the scroll position to 25%. And that seems to happen, since in the second picture the scrollbar has moved to the right - but so has the panel itself. See the attachments for screenshots before and after calling the method.

I'm using NGUI 2.3.4 and Unity 4.0.1f2.

Edit: Marked as solved, and clarified subject to match with the solution.
« Last Edit: February 28, 2013, 10:07:18 AM by Aki Kanerva »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIDraggablePanel.SetDragAmount() works in an unexpected way
« Reply #1 on: February 27, 2013, 09:54:24 AM »
To move a panel, you need to adjust its position, then adjust its clipping rect by the inverse of the position.

Look at what I do in SpringPanel -- which, coincidently, is pretty much the script you want to be using in your case anyway.

Aki Kanerva

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: UIDraggablePanel.SetDragAmount() works in an unexpected way
« Reply #2 on: February 28, 2013, 10:06:12 AM »
Okay, thanks, that got it working. Bit of a roundabout way of doing it though - I was looking for a shorthand to avoid having to do calculations with the panel's bounds.

Now I'm curious, though. What is SetDragAmount() used for?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Solved] Setting the position of UIDraggablePanel
« Reply #3 on: February 28, 2013, 12:14:03 PM »
Callbacks used by scroll bars.

Aki Kanerva

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: [Solved] Setting the position of UIDraggablePanel
« Reply #4 on: March 04, 2013, 11:23:04 AM »
Ah, that makes sense. Thanks again for the speedy help!

taikuukaits

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: [Solved] Setting the position of UIDraggablePanel
« Reply #5 on: August 23, 2015, 11:50:33 PM »
Hey Aki,

I was wondering if you could share your final solution for saving the scroll position of the UIScrollView. I am trying to do the same thing and automatically scroll it to the last level you selected.

Thanks!

taikuukaits

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: [Solved] Setting the position of UIDraggablePanel
« Reply #6 on: August 23, 2015, 11:52:03 PM »
I just realized this is really old and is using UIDraggable instead of UIScrollView. I was looking for UIScrollView so if your code no longer applies, disregard! ;D