Author Topic: UIScrollView  (Read 198700 times)

justkevin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: UIScrollView
« Reply #75 on: June 13, 2014, 10:11:56 AM »
Thanks for the response.

Unfortunately, that doesn't work-- calling ResetPosition for the first Update moves the scrollview, but not to the origin. Calling it for the first 2-3 updates causes it to disappear completely for some reason. Only after calling it for 4 consecutive updates gets it to the origin:

  1.         if (_resetAttempts < 4)
  2.         {
  3.             UIScrollView scrollView = GetComponentInChildren<UIScrollView>();
  4.             scrollView.ResetPosition();
  5.             ++_resetAttempts;
  6.         }
  7.  

That works, but I'm concerned that without understanding why 4 is the magic number of updates it may break with some change to the contents of the scrollview.

Using a delayed coroutine didn't work. Changing timescale had no effect.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIScrollView
« Reply #76 on: June 15, 2014, 06:13:07 AM »
That doesn't make much sense to me. Might be something related to what else you're doing in there...

RDeluxe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: UIScrollView
« Reply #77 on: June 16, 2014, 08:40:22 AM »
Well, here is my code.

I'm actually not doing anything fancy.

  1. public class PlacesScreen : MonoBehaviour
  2. {
  3.     // Used to check if the screen resolution changed
  4.     // TODO : Find a better solution than this one
  5.     public float startWidth;
  6.     public float startHeight;
  7.  
  8.     List<Place> places;
  9.     UIScrollView PlacesList;
  10.     UIGrid PlacesGrid;
  11.     GameObject FileList;
  12.  
  13.     // Hardcoded for testing purposes only
  14.     string json = @"[{ blabla
  15. }]";
  16.  
  17.     // Use this for initialization
  18.     void Start()
  19.     {
  20.         places = JsonConvert.DeserializeObject<List<Place>>(json);
  21.         PlacesList = gameObject.GetComponentInChildren<UIScrollView>() as UIScrollView;
  22.         PlacesGrid = gameObject.GetComponentInChildren<UIGrid>() as UIGrid;
  23.         FileList = GameObject.Find("Grid");
  24.  
  25.         startHeight = PlacesList.gameObject.GetComponent<UIPanel>().height;
  26.         startWidth = PlacesList.gameObject.GetComponent<UIPanel>().width;
  27.  
  28.         if (PlacesGrid != null && FileList != null && PlacesList != null)
  29.         {
  30.             BetterList<Transform> childList = PlacesGrid.GetChildList();
  31.  
  32.             // Addind the places from the json to the graphic list
  33.             foreach (Place place in places)
  34.             {
  35.                 Debug.Log("Adding a place");
  36.                 GameObject placeFile = Resources.Load("prefabs/File") as GameObject;
  37.                 placeFile.GetComponent<UISprite>().width = (int)PlacesGrid.cellWidth;
  38.                 placeFile.transform.Find("ContentPanel").gameObject.transform.Find("PlaceName").gameObject.GetComponent<UILabel>().text = place.Name;
  39.                 placeFile.transform.Find("ContentPanel").gameObject.transform.Find("CityName").gameObject.GetComponent<UILabel>().text = place.City;
  40.                 placeFile.transform.Find("ContentPanel").gameObject.transform.Find("TypeName").gameObject.GetComponent<UILabel>().text = place.Type;
  41.                 NGUITools.AddChild(FileList, placeFile);
  42.                 childList.Add(placeFile.transform);
  43.             }
  44.  
  45.             // Small hack to get a nicely presented Grid even with only 1 element
  46.             // TODO : Redo this ...
  47.             if (childList.size == 1)
  48.             {
  49.                 Debug.Log("One place only, we have to had a invisible file to avoid center");
  50.                 GameObject invisibleFile = Resources.Load("prefabs/File") as GameObject;
  51.                 NGUITools.AddChild(FileList, invisibleFile);
  52.                 childList.Add(invisibleFile.transform);
  53.             }
  54.             ResizeGrid();
  55.         }
  56.     }
  57.  
  58.     // Update is called once per frame
  59.     void Update()
  60.     {
  61.         if (startHeight != PlacesList.gameObject.GetComponent<UIPanel>().height || startWidth != PlacesList.gameObject.GetComponent<UIPanel>().width)
  62.         {
  63.             ResizeGrid();
  64.             startHeight = PlacesList.gameObject.GetComponent<UIPanel>().height;
  65.             startWidth = PlacesList.gameObject.GetComponent<UIPanel>().width;
  66.         }
  67.     }
  68.  
  69.     /// <summary>
  70.     /// Resize the grid considering the screen width
  71.     /// </summary>
  72.     void ResizeGrid()
  73.     {
  74.         Debug.Log("ResizeGrid Called");
  75.         // Resizing the Grid cells' size.
  76.         PlacesGrid.cellWidth = (float)(PlacesList.gameObject.GetComponent<UIPanel>().width / 2.2);
  77.         PlacesGrid.cellHeight = (float)(PlacesGrid.cellWidth / 1.244);
  78.         foreach (Transform place in PlacesGrid.GetChildList())
  79.         {
  80.             place.gameObject.GetComponent<UISprite>().width = (int)PlacesGrid.cellWidth;
  81.         }
  82.         PlacesGrid.Reposition();
  83.         PlacesList.ResetPosition();
  84.     }
  85. }

The Resize() method is working perfectly well, resizing my elements. But when I'm coming from a wider resolution, here is what happen :




kennethljj

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: UIScrollView
« Reply #78 on: June 30, 2014, 12:27:12 AM »
Hi Aren,

I have just recently upgraded from version 2+ and I got a question regarding the limitations of the UIScrollView.
From what I can remember, in version 2+, UIDraggablePanel have a limitation on the parents' scale. If any of the parents' scale (throughout hierarchy) is not the same (eg. 1f, 0.9f, 1f), the scrolling would have a problem.

Does UIScrollView have this limitation too?
If yes, is there anyway around it?

Thanks in advance.

MartinG

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIScrollView
« Reply #79 on: July 01, 2014, 04:14:18 AM »
Hi,

after updating to version 3.6.5 of NGUI I run into the problem, that the onDragFinished on UIScrollView isn´t fired anymore. I also tested with the latest version 3.6.6., it isn´t fired.

In my script I set

scrollView.onDragFinished = myFunction;

It worked with the older versions of NGUI (last version it worked was 3.5.3)

I also tried to set it that way:
      
scrollView.onDragFinished += myFunction;

Does not work.

In your UIScrollView the onDragFinished is fired only in the Press function, when it´s pressed is false.

In the LateUpdate function it´s never fired, because imMomentum.magnitude is always zero.

So, even if your onDragFinished is fired in scrollView "pressed == falser", my function does not fire anymore.

Did I some change or doing anything wrong? Or is this a bug in the new version?

Greetings,

Martin

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIScrollView
« Reply #80 on: July 01, 2014, 05:14:52 AM »
@MartinG: What are your settings on the scroll view? I just attached a simple test script to a default scroll view and it works as expected. Although the onFinished should not be getting called in Press(false) of a momentum-using scroll view -- that should get fixed.

@kennethljj: That limitation was on panels, and it's still the case. Panels should be scaled uniformly.

MartinG

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIScrollView
« Reply #81 on: July 01, 2014, 07:57:27 AM »
@ArenMook:

My ScrollView settings are:

Content Origin : TopLeft
Movement: Horizontal
DragEffect: Momentum
ScrollWheelFactor: 0.25
Momentum Amount: 35
Restrict Wihtin Panel: yes
Cancel Drag if fits: no
Smooth Drag Start: yes
iOS Drag Emulation: yes

I also tested with DragEffect: None and MomentumAndSpring

My function, which I set to

scrollView.onDragFinished = myFunction;

is not called. I tested the new onDragStarted with another function, and this worked.

My ScrollView settings are the same as with the version 3.5.3 of NGUI, there it worked. I will test it with another ScrollView, may be this will work ... or not.

MartinG

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UIScrollView
« Reply #82 on: July 01, 2014, 09:00:01 AM »
@ArenMook:

Everything fine, I solved it.

I set the

scrollView.onDragFinished = myFunction

in the Awake() of my Behaviour. Now it´s in Start() and it works. (the setting of onDragStarted in Awake() still worked!)

I don´t know if something was changed in your UIScrollView, so that the onDragFinished may was "nulled" or "reseted" in it´s Start() Function. And may be it was just luck, that it worked within my Awake() Function in the past with NGUI 3.5.3 and older versions.

So, thanks for your help and keep up the good work! :)

lazlo

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UIScrollView
« Reply #83 on: July 04, 2014, 04:20:20 PM »
Now that there isn't any UIDraggablePanel anymore, I'm can't find how to implement mouse wheel scroll. I have "Scroll Wheel Factor" to 1 but nothing happens. Are the steps outlined in some tutorial I missed?

kitgui

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: UIScrollView
« Reply #84 on: July 05, 2014, 04:28:34 AM »
Hey ArenMook, I've been working on a scrollview which dynamically adds elements to itself, with a Vertical Grid for repositioning newly instantiated elements.

I've noticed that when the Scroll Bar option Show Condition is set to OnlyIfNeeded or WhenDragging, the scrollview will start in odd positions sometimes (scrolled down from top by about 1 element's height), but if I set it to Always, that doesn't happen.

It's not a critical bug, I can live with Always, but I thought you should know.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIScrollView
« Reply #85 on: July 05, 2014, 11:07:52 PM »
@kitgui: likely related to the order of events in your case. After instantiating objects you need to reposition the content, then reset the scroll view's position (and the scroll view's content bounds).

@lazlo: UIScrollView has a Scroll Wheel Factor and it will work assuming the Movement is set to Horizontal or Vertical. Note that you will still need UIDragScrollView in order for it to work or nothing will be receiving events.

nookie

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: UIScrollView
« Reply #86 on: July 08, 2014, 10:14:49 AM »
I had a very big problem with UIScrollView. I just can't understand how it works and why my code is not working. Simple scene:

UIScrollView, UIDragScrollView, UIGrid, UICenterOnChild
-Item (UICenterOnClick, UIDragScrollView)
-Item (UICenterOnClick, UIDragScrollView)
-Item (UICenterOnClick, UIDragScrollView)
...

Everything works fine, but I wanted to make first child to be on center when scene loads. I wrote the script:

  1. using UnityEngine;
  2.  
  3. public class CenterFirst : MonoBehaviour
  4. {
  5.     private void Update()
  6.     {
  7.         GetComponent<UICenterOnChild>().CenterOn(transform.GetChild(0));
  8.         enabled = false;
  9.     }
  10. }
  11.  

I attached this script to UIScrollView and run the scene. First element moved to the center of screen. Good! BUT when I click somewhere on scrollview it move all items to the left side of the screen! After this "jump" every next click works as expected: centers item on screen. If I run scene without CenterFirst script and click any item - everything works fine. So I think I did something wrong, but I can't understand what exactly. Where is my mistake?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIScrollView
« Reply #87 on: July 08, 2014, 01:31:22 PM »
Wrong structure. Please follow the video more carefully next time, and use the right-click context menus. They wouldn't have allowed you to mess up your structure like that.

UIPanel, UIScrollView
- UIGrid, UICenterOnChild
-- Item (collider, UIDragScrollView)
-- Item (collider, UIDragScrollView)
-- Item (collider, UIDragScrollView)

Also make sure the scroll view has a proper Origin Point set.

nookie

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: UIScrollView
« Reply #88 on: July 08, 2014, 01:47:04 PM »
Quote
Wrong structure. Please follow the video more carefully next time, and use the right-click context menus. They wouldn't have allowed you to mess up your structure like that.
I changed structure like you said, but nothing changed - "jump" still in present.

UIPanel, UIScrollView
- UIGrid, UICenterOnChild, CenterFirst
-- Item (UICenterOnClick, UIDragScrollView, collider)
-- Item (UICenterOnClick, UIDragScrollView, collider)
-- Item (UICenterOnClick, UIDragScrollView, collider)

Quote
Also make sure the scroll view has a proper Origin Point set.
I don't understand what you mean.

nookie

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: UIScrollView
« Reply #89 on: July 09, 2014, 01:28:41 AM »
I still waiting for answer and support. Or this is bug?

P.S. One more problem: if I change the size of unity player window the "jump" is occur too. But this all happends only if ScrollView centers on the several first items and have a space on the left of them. Then if you click or change windows size it seems like reset position call. It more preferable for me if screen size changed - save focus on centered item. And, like I said above, first click should not call reset position. How to reach it?

P.S.2. I found source of bug - scroll bar. If I remove scroll bar everything (clicks, window resize) works without "jump". But what if I need scroll bar? What should I do?
« Last Edit: July 09, 2014, 03:03:48 AM by nookie »