Author Topic: Bug or UIWrapContent doesn't quite work with scroll bars?  (Read 9273 times)

Redd

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 1
  • Posts: 10
    • View Profile
Bug or UIWrapContent doesn't quite work with scroll bars?
« on: July 17, 2014, 06:04:10 PM »

I created a limited scroll view by specifying a Range Limit and attached a scroll bar to it.  The scroll bar works to an extent but the foreground stutters when you move the contents from one end to the other end.  I will be grateful for any help anyone can provide.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Bug or UIWrapContent doesn't quite work with scroll bars?
« Reply #1 on: July 18, 2014, 06:19:16 AM »
Wrap content never worked with scroll bars, as what the script does is repositions its children, creating the illusion of an endless scroll view. Scroll bars use the bounds of the content and know nothing about the repositioning logic. So in short, wrap content = virtual content. Scroll bars only work with actual content.

Wontak

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Bug or UIWrapContent doesn't quite work with scroll bars?
« Reply #2 on: July 30, 2014, 06:37:15 AM »
Scrollbar is affected internally by NGUITools::CalculateRelativeWidgetBounds function. When calculating bounds, it use UIWidget. So you can use UIWrapContent and UIScrollbar altogether by using this logic.

It really simple trick. Sudo code attached below.

* UIScrollBounds.cs

using UnityEngine;
using System.Collections;

public class UIScrollBounds : MonoBehaviour
{
   public UIWrapContent wrapContent;

   // Use this for initialization
   void Start()
   {
      UIWidget widget = gameObject.GetComponent<UIWidget>();
      if (widget == null)
         widget = gameObject.AddComponent<UIWidget>();

      widget.height = wrapContent.itemSize * wrapContent.transform.childCount;
      widget.pivot = UIWidget.Pivot.Top;

      transform.localPosition = new Vector3(0f, wrapContent.itemSize/2f, 0f);
   }
}

* This is a simple example. It should be edited as necessary.
* The attached image shows 'Endless Scroll Views.unity' by using UIScrollBounds.
« Last Edit: July 30, 2014, 08:09:27 AM by Wontak »

davitosan

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Bug or UIWrapContent doesn't quite work with scroll bars?
« Reply #3 on: August 10, 2016, 04:33:08 PM »
Posting here because it's the top google serach.

I was able to get UIScrollBar working with UIWrapContent. Do not reference the scroll bar in your scroll view script.

The only assumption in the posted code is that your vertical scrollbars go top to bottom and your horizontal scroll bars go left to right. I'm sure you can easily modify if this assumption isn't true to you.

do this code when the scrollview moves. EX: scrollView.onMomentumMove += UpdateScrollbar;

_svStartLocalPos = _scrollView.transform.localPosition;

  1. UpdateScrollbar()
  2. {
  3.    float pct = 0.0f;
  4.    float endPos = 0.0f;
  5.    if(_scrollView.movement == UIScrollView.Movement.Vertical)
  6.    {
  7.       endPos = (_wrapContent.itemSize * (NUM_ITEMS / (float)NUM_COLUMNS) - _uiPanel.GetViewSize().y) + _svStartLocalPos.y;
  8.       pct = Mathf.Clamp((_scrollView.transform.localPosition.y - _svStartLocalPos.y) / (endPos - _svStartLocalPos.y), 0.0f, 100.0f);
  9.    }
  10.    else if(_scrollView.movement == UIScrollView.Movement.Horizontal)
  11.    {
  12.       endPos = (_wrapContent.itemSize * (NUM_ITEMS / (float)NUM_ROWS) - _uiPanel.GetViewSize().x) - Mathf.Abs(_svStartLocalPos.x);
  13.       pct = Mathf.Clamp((_scrollView.transform.localPosition.x - _svStartLocalPos.x) / (_svStartLocalPos.x - endPos), 0.0f, 100.0f);
  14.    }
  15.  
  16.    _scrollBar.value = Mathf.Lerp(_scrollBar.value, pct, 0.1f);
  17. }
  18.  

If you want to drag the scroll bar or click on the background to jump
  1. if(_scrollBar.backgroundWidget != null)
  2. {
  3.    UIEventListener bgl = UIEventListener.Get(_scrollBar.backgroundWidget.gameObject);
  4.    if(bgl != null)
  5.    {
  6.       bgl.onPress += OnScrollBarPressed;
  7.       bgl.onDrag += OnScrollBarDragged;
  8.    }
  9. }
  10.  
  11. void OnScrollBarPressed(GameObject go, bool isPressed)
  12. {
  13.    if(!isPressed)
  14.    {
  15.       OnScrollBarChanged();
  16.    }
  17. }
  18.  
  19. void OnScrollBarDragged(GameObject go, Vector2 delta)
  20. {
  21.    OnScrollBarChanged();
  22. }
  23.  
  24. void OnScrollBarChanged()
  25. {
  26.    Vector3 newLocalPos = _scrollView.gameObject.transform.localPosition;
  27.    if(_scrollView.movement == UIScrollView.Movement.Vertical)
  28.    {
  29.         newLocalPos.y = _scrollBar.value * (_wrapContent.itemSize * (NUM_ITEMS / (float)NUM_COLUMNS) - _uiPanel.GetViewSize().y) + _svStartLocalPos.y;
  30.    }
  31.    else if(_scrollView.movement == UIScrollView.Movement.Horizontal)
  32.    {
  33.       newLocalPos.x = -_scrollBar.value * (_wrapContent.itemSize * (NUM_ITEMS / (float)NUM_ROWS) - _uiPanel.GetViewSize().x) + _svStartLocalPos.x;
  34.    }
  35.    SpringPanel.Begin(_scrollView.panel.cachedGameObject, newLocalPos, 8).onFinished = OnSpringPanelFinished;
  36. }
  37.  
  38. void OnSpringPanelFinished()
  39. {
  40.    _scrollView.RestrictWithinBounds(false);
  41. }
  42.  
« Last Edit: August 10, 2016, 11:09:50 PM by davitosan »