NGUI WrapContent与ScrollBar一起使用

上一篇讲了UISCrollView+UIWrapContent显示多行多列,今天讲讲UIScrollView+UIWrapContent+UIScrollBar的使用。

一般在做滑动展示时,我们都会加个滚动条,NGUI也提供了这个组件,但是为了优化我们经常会使用UIWrapContent来重复使用Item,可是NGUI现有的UIScrollBar的计算方式不支持,这就要我们自己实现个计算方式去计算滚动条,哈哈好消息,这个算法有人实现了原文地址:http://www.tasharen.com/forum/index.php?topic=10435.msg64592#msg64592,这里我记录下怕自己忘了,同时我做了一点小改动;OK,直接上代码

using UnityEngine;
using System.Collections;

public class MyBar : MonoBehaviour
{
    public UIScrollView ScrollView;
    public UIWrapContent WrapContent;
    public UIPanel ScrollViewPanel;
    public UIScrollBar ScrollBar;
    public int ItemNum = 60;
    public int RowNum = 1;
    private Vector3 mStartLocalPos;
    private float mScrollLegth;

    void Start()
    {
        ScrollView.onMomentumMove += UpdateScrollbar;
        mStartLocalPos = ScrollView.transform.localPosition;
        mScrollLegth = WrapContent.itemSize*Mathf.CeilToInt(ItemNum/(float) RowNum);
        Debug.LogError(ItemNum / (float)RowNum);
        Debug.LogError(mScrollLegth);
        ScrollBar.barSize = ScrollViewPanel.GetViewSize().y/mScrollLegth;
        if (null != ScrollBar.backgroundWidget)
        {
            UIEventListener bgl = UIEventListener.Get(ScrollBar.backgroundWidget.gameObject);
            if (null != bgl)
            {
                bgl.onPress += OnScrollBarPressed;
                //bgl.onDrag += OnScrollBarDragged;
            }
            UIEventListener fgl = UIEventListener.Get(ScrollBar.foregroundWidget.gameObject);
            if (null != fgl)
            {
                fgl.onDrag += OnScrollBarDragged;
            }
        }
    }
    void UpdateScrollbar()
    {
        float pct = 0.0f;
        float endPos = 0.0f;
        if (ScrollView.movement == UIScrollView.Movement.Horizontal)
        {
            endPos = (mScrollLegth - ScrollViewPanel.GetViewSize().x) -
                     Mathf.Abs(mStartLocalPos.x);
            pct = Mathf.Clamp((ScrollView.transform.localPosition.x - mStartLocalPos.x) / (mStartLocalPos.x - endPos), 0,
                100f);
        }
        else if (ScrollView.movement == UIScrollView.Movement.Vertical)
        {
            endPos = (mScrollLegth - ScrollViewPanel.GetViewSize().y) + mStartLocalPos.y;
            pct = Mathf.Clamp((ScrollView.transform.localPosition.y - mStartLocalPos.y) / (endPos - mStartLocalPos.y), 0f,
                100f);
        }
        ScrollBar.value = Mathf.Lerp(ScrollBar.value, pct, 0.1f);
    }

    void OnScrollBarPressed(GameObject go, bool isPressed)
    {
        if (!isPressed)
        {
            OnScrollBarChange();
        }
    }

    void OnScrollBarDragged(GameObject go, Vector2 delta)
    {
        OnScrollBarChange();
    }

    void OnScrollBarChange()
    {
        Vector3 newLocalPos = ScrollView.transform.localPosition;
        if (ScrollView.movement == UIScrollView.Movement.Vertical)
        {
            newLocalPos.y = ScrollBar.value*
                            (mScrollLegth - ScrollViewPanel.GetViewSize().y) +
                            mStartLocalPos.y;
        }
        else if (ScrollView.movement == UIScrollView.Movement.Horizontal)
        {
            newLocalPos.x = ScrollBar.value*
                            (mScrollLegth - ScrollViewPanel.GetViewSize().x) +
                            mStartLocalPos.x;
        }
        SpringPanel.Begin(ScrollView.panel.cachedGameObject, newLocalPos, 8).onFinished = OnSprinPanelFinished;
    }

    void OnSprinPanelFinished()
    {
        ScrollView.RestrictWithinBounds(false);
    }
}
代码比较简单,大家自己看吧!!

你可能感兴趣的:(NGUI,Unity)