[关闭]
@daaoling 2017-01-13T20:45:14.000000Z 字数 2911 阅读 1854

Unity NGUI

NGUI 3D 循环列表的制作

UIScrollView 的原理

QQ20160829-0@2x.png-146.1kB
说明2.png-244.8kB
说明1.png-330kB

2d上3d的呈现

set view rect

加一个无限循环的效果

mEndLessCoverFlow.cs

public class mEndLessCoverFlow : MonoBehaviour
{

    #region outlet-setting
    public Transform mTrans;
    public UIPanel mPanel;
    public UIScrollView mScroll;
    public List<mEndLessEntry> mChildren = new List<mEndLessEntry>();
    #endregion

    /// <summary>
    /// Width or height of the child items for positioning purposes.
    /// </summary>

    public int itemSize = 100;

    protected virtual void Start()
    { 
        /////////////////////////////////////
        mTrans = transform;
        mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
        mScroll = mPanel.GetComponent<UIScrollView>();

        /////////////////////////////////////
        // Cache all children and place them in order
        mChildren.Clear();
        for (int i = 0; i < mTrans.childCount; ++i)
            mChildren.Add(mTrans.GetChild(i).GetComponent<mEndLessEntry>());

        mChildren.Sort(delegate(mEndLessEntry a, mEndLessEntry b) { return b.transform.localPosition.y.CompareTo(a.transform.localPosition.y); });

        /////////////////////////////////////
        mScroll.GetComponent<UIPanel>().onClipMove += OnMove;
        mScroll.restrictWithinPanel = false;
        if (mScroll.dragEffect == UIScrollView.DragEffect.MomentumAndSpring)
            mScroll.dragEffect = UIScrollView.DragEffect.Momentum;

        OnMove(mPanel);
    }



      #region View

    public void OnMove(UIPanel panel)
    {
        WrapContent();

        UpdateScrollView(panel);
    }

    public void WrapContent()
    {
        float extents = itemSize * mChildren.Count * 0.5f;
        Vector3[] corners = mPanel.worldCorners;

        for (int i = 0; i < 4; ++i)
        {
            Vector3 v = corners[i];
            v = mTrans.InverseTransformPoint(v);
            corners[i] = v;
        }
        Vector3 center = Vector3.Lerp(corners[0], corners[2], 0.5f);

        //Debug.Log(" center  " + center);

        float min = corners[0].y - itemSize;
        float max = corners[2].y + itemSize;

        foreach (var t in mChildren)
        {
            float distance = t.transform.localPosition.y - center.y;

            if (distance < -extents)
            {
                t.transform.localPosition += new Vector3(0f, extents * 2f, 0f);
                distance = t.transform.localPosition.y - center.y;
            }
            else if (distance > extents)
            {
                t.transform.localPosition -= new Vector3(0f, extents * 2f, 0f);
                distance = t.transform.localPosition.y - center.y;
            }
        }
    }

    public void UpdateScrollView(UIPanel panel)
    {
        float localY = panel.clipOffset.y;

        foreach (mEndLessEntry item in mChildren)
        {
            float offsetY = item.transform.localPosition.y;

            float factor = ((offsetY - localY) + 300) / 600;

            item.UpdateScrollView(factor);
        }
    }
    #endregion
}

mEndLessEntry.cs

public class mEndLessEntry : MonoBehaviour
{
    public UIPanel panel;

    public UIWidget widget;

    public Transform mTrans;

    public mAniamtionCurve mAnim;

    void OnEnable()
    {
        mTrans = transform;

        panel = NGUITools.FindInParents<UIPanel>(gameObject);

        widget = GetComponent<UIWidget>();

        mAnim = Resources.Load<mAniamtionCurve>("mAnim");
    }

    #region View
    public void UpdateScrollView(float factor)
    {
        if (factor < 0 || factor > 1) Debug.Log("this ?!" + factor);

        Vector3 nowLocalEuler = transform.eulerAngles;
        nowLocalEuler.x = mAnim.localEulerX.Evaluate(factor);
        transform.eulerAngles = nowLocalEuler;

        Vector3 nowLocalPos = transform.localPosition;
        nowLocalPos.z = mAnim.localPosZ.Evaluate(factor);
        transform.localPosition = nowLocalPos;

        widget.width = (int)mAnim.widthCurve.Evaluate(factor);
        widget.height = (int)mAnim.heightCurve.Evaluate(factor);

    }
    #endregion
}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注