Unity3D UGUI:Scroll View滑动居中

今天是上班第一天,其他就也就不多说了,直接进入正题吧。今天要写的是Unity3D的UGUI的Scroll View的滑动居中,这个功能在NGUI里有现成的代码,这里的代码和NGUI的类似,思路都是一样的,据说做UGUI的就是那批做NGUI的人做的,闲话不多说了下面直接上代码。

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System;
/// <summary>
/// UGUI ScrollRect 滑动元素居中
/// </summary>
public class UICenterOnChild : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
    public float scrollSpeed = 8f;
    public Transform UIGrid;
    private ScrollRect scrollRect;
    private float[] pageArray;
    private float targetPagePosition = 0f;
    private bool isDrag = false;
    private int pageCount;
    private int currentPage = 0;
    private List<Transform> items = new List<Transform>();
    // Use this for initialization
    void Awake()
    {
        scrollRect = GetComponent<ScrollRect>();
    }
    void Start()
    {
        InitPageArray();
    }
    /// <summary>
    /// 初始化获取元素总个数
    /// </summary>
    void InitPageArray()
    {
        foreach (Transform item in UIGrid)
        {
            if (item.gameObject.activeSelf && !items.Contains(item))
            {
                items.Add(item);
            }
        }
        pageCount = items.Count;
        pageArray = new float[pageCount];
        for (int i = 0; i < pageCount; i++)
        {
            pageArray[i] = (1f / (pageCount - 1)) * i;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (!isDrag)
        {
            if (scrollRect.horizontal)
            {
                scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, targetPagePosition, scrollSpeed * Time.deltaTime);
            }
            else if (scrollRect.vertical)
            {
                scrollRect.verticalNormalizedPosition = Mathf.Lerp(scrollRect.verticalNormalizedPosition, targetPagePosition, scrollSpeed * Time.deltaTime);
            }
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        isDrag = true;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
        float pos = scrollRect.horizontal ? scrollRect.horizontalNormalizedPosition : scrollRect.verticalNormalizedPosition;
        int index = 0;
        float offset = Math.Abs(pageArray[index] - pos);
        for (int i = 1; i < pageArray.Length; i++)
        {
            float _offset = Math.Abs(pageArray[i] - pos);
            if (_offset < offset)
            {
                index = i;
                offset = _offset;
            }
        }
        targetPagePosition = pageArray[index];
        currentPage = index;
    }
    /// <summary>
    /// 向左移动一个元素
    /// </summary>
    public void ToLeft()
    {
        if (currentPage > 0)
        {
            currentPage = currentPage - 1;
            targetPagePosition = pageArray[currentPage];
        }
    }
    /// <summary>
    /// 向右移动一个元素
    /// </summary>
    public void ToRight()
    {
        if (currentPage < pageCount - 1)
        {
            currentPage = currentPage + 1;
            targetPagePosition = pageArray[currentPage];
        }
    }
    /// <summary>
    /// 获取当前页码
    /// </summary>
    /// <returns></returns>
    public int GetCurrentPageIndex()
    {
        return currentPage;
    }
    /// <summary>
    /// 设置当前页码
    /// </summary>
    /// <param name="index"></param>
    public void SetCurrentPageIndex(int index)
    {
        currentPage = index;
        targetPagePosition = pageArray[currentPage];
    }
    /// <summary>
    /// 获取总页数
    /// </summary>
    /// <returns></returns>
    public int GetTotalPages()
    {
        return pageCount;
    }
}

把上面的脚本放到Scroll View上面,把Content拖到脚本上的 UIGrid上面,如下图:

自己也可以拓展里面的代码,最后的几个方法就是我自己写的,为了方便外面获取信息用的,大家有更好的方法也可以留言,不懂的地方,看到回复我一定会解答的。

You May Also Like

About the Author: 大腿Plus

1 Comment

发表评论