UGUI ScrollRect 带按钮翻页支持拖拽

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 23:21   765   0

这里写图片描述

using System.Collections;  
using UnityEngine.UI;  
using UnityEngine.EventSystems;  
using System.Collections.Generic;  
using System;  
/// <summary>  
/// 略知CSharp http://blog.csdn.net/subsystemp  
/// </summary>  
public class ScrollRectHelper : MonoBehaviour, IBeginDragHandler, IEndDragHandler  
{  
    
    public float smooting = 5;                          //滑动速度  
    public List<GameObject> listItem;                   //scrollview item   
    public int pageCount = 1;                           //每页显示的项目  
  
    ScrollRect srect;  
    float pageIndex;                                    //总页数  
    bool isDrag = false;                                //是否拖拽结束  
    List<float> listPageValue = new List<float> { 0 };  //总页数索引比列 0-1  
    float targetPos = 0;                                //滑动的目标位置  
    float nowindex = 0;                                 //当前位置索引  
  
    void Awake()  
    {  
        srect = GetComponent<ScrollRect>();  
        ListPageValueInit();  
    }  
  
    //每页比例  
    void ListPageValueInit()  
    {  
        pageIndex = (listItem.Count / pageCount)-1;  
        if (listItem != null && listItem.Count != 0)  
        {  
            for (float i = 1; i <= pageIndex; i++)  
            {  
                listPageValue.Add((i / pageIndex));  
            }  
        }  
    }  
  
    void Update()  
    {  
        if (!isDrag)  
            srect.horizontalNormalizedPosition = Mathf.Lerp(srect.horizontalNormalizedPosition, targetPos, Time.deltaTime * smooting);  
    }  
    /// <summary>  
    /// 拖动开始  
    /// </summary>  
    /// <param name="eventData"></param>  
    public void OnBeginDrag(PointerEventData eventData)  
    {  
        isDrag = true;  
    }  
    /// <summary>  
    /// 拖拽结束  
    /// </summary>  
    /// <param name="eventData"></param>  
    public void OnEndDrag(PointerEventData eventData)  
    {  
        isDrag = false;  
        var tempPos = srect.horizontalNormalizedPosition; //获取拖动的值  
        var index = 0;  
        float offset = Mathf.Abs(listPageValue[index] - tempPos);    //拖动的绝对值  
        for (int i = 1; i < listPageValue.Count; i++)  
        {  
            float temp = Mathf.Abs(tempPos - listPageValue[i]);  
            if (temp < offset)  
            {  
                index = i;  
                offset = temp;  
            }  
        }  
        targetPos = listPageValue[index];  
        nowindex = index;  
    }  
  
    public void BtnLeftGo()  
    {  
        nowindex = Mathf.Clamp(nowindex - 1, 0, pageIndex);  
        targetPos = listPageValue[Convert.ToInt32(nowindex)];  
    }  
  
    public void BtnRightGo()  
    {  
        nowindex = Mathf.Clamp(nowindex + 1, 0, pageIndex);  
        targetPos = listPageValue[Convert.ToInt32(nowindex)];  
  
    }  
}

DEMO 下载地址

原文:http://blog.csdn.net/subsystemp/article/details/50608420

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP