UGUI ScrollRect 带按钮翻页支持拖拽



Demo:http://download.csdn.net/detail/subsystemp/9422439


[csharp]  view plain  copy
 print ?
  1. "font-family:Microsoft YaHei;font-size:14px;">using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEngine.UI;  
  4. using UnityEngine.EventSystems;  
  5. using System.Collections.Generic;  
  6. using System;  
  7. ///   
  8. /// 略知CSharp http://blog.csdn.net/subsystemp  
  9. ///   
  10. public class ScrollRectHelper : MonoBehaviour, IBeginDragHandler, IEndDragHandler  
  11. {  
  12.     
  13.     public float smooting = 5;                          //滑动速度  
  14.     public List listItem;                   //scrollview item   
  15.     public int pageCount = 3;                           //每页显示的项目  
  16.   
  17.     ScrollRect srect;  
  18.     float pageIndex;                                    //总页数  
  19.     bool isDrag = false;                                //是否拖拽结束  
  20.     List<float> listPageValue = new List<float> { 0 };  //总页数索引比列 0-1  
  21.     float targetPos = 0;                                //滑动的目标位置  
  22.     float nowindex = 0;                                 //当前位置索引  
  23.   
  24.     void Awake()  
  25.     {  
  26.         srect = GetComponent();  
  27.         ListPageValueInit();  
  28.     }  
  29.   
  30.     //每页比例  
  31.     void ListPageValueInit()  
  32.     {  
  33.         pageIndex = (listItem.Count / pageCount)-1;  
  34.         if (listItem != null && listItem.Count != 0)  
  35.         {  
  36.             for (float i = 1; i <= pageIndex; i++)  
  37.             {  
  38.                 listPageValue.Add((i / pageIndex));  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43.     void Update()  
  44.     {  
  45.         if (!isDrag)  
  46.             srect.horizontalNormalizedPosition = Mathf.Lerp(srect.horizontalNormalizedPosition, targetPos, Time.deltaTime * smooting);  
  47.     }  
  48.     ///   
  49.     /// 拖动开始  
  50.     ///   
  51.     ///   
  52.     public void OnBeginDrag(PointerEventData eventData)  
  53.     {  
  54.         isDrag = true;  
  55.     }  
  56.     ///   
  57.     /// 拖拽结束  
  58.     ///   
  59.     ///   
  60.     public void OnEndDrag(PointerEventData eventData)  
  61.     {  
  62.         isDrag = false;  
  63.         var tempPos = srect.horizontalNormalizedPosition; //获取拖动的值  
  64.         var index = 0;  
  65.         float offset = Mathf.Abs(listPageValue[index] - tempPos);    //拖动的绝对值  
  66.         for (int i = 1; i < listPageValue.Count; i++)  
  67.         {  
  68.             float temp = Mathf.Abs(tempPos - listPageValue[i]);  
  69.             if (temp < offset)  
  70.             {  
  71.                 index = i;  
  72.                 offset = temp;  
  73.             }  
  74.         }  
  75.         targetPos = listPageValue[index];  
  76.         nowindex = index;  
  77.     }  
  78.   
  79.     public void BtnLeftGo()  
  80.     {  
  81.         nowindex = Mathf.Clamp(nowindex - 1, 0, pageIndex);  
  82.         targetPos = listPageValue[Convert.ToInt32(nowindex)];  
  83.     }  
  84.   
  85.     public void BtnRightGo()  
  86.     {  
  87.         nowindex = Mathf.Clamp(nowindex + 1, 0, pageIndex);  
  88.         targetPos = listPageValue[Convert.ToInt32(nowindex)];  
  89.   
  90.     }  
  91. }"font-size:14px;">  
  92.  

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