关于ScrollView的代码01

本文介绍了一个使用Unity实现的滑动翻页效果案例,通过监听用户的触摸操作并结合页面定位来实现流畅的手势翻页体验。文章详细展示了如何设置页面按钮、处理触摸事件以及自动播放等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
public class ScrollPage1 : MonoBehaviour, IBeginDragHandler,IEndDragHandler
{
   private ScrollRect rect;
    //存取得到的页面
   private List<GameObject> pages = new List<GameObject> ();
   private Transform mainGrid;
   private bool doNot = false;
   private bool startPosFlag = true;
   private Vector3 startFingerPos;
   private Vector3 nowFingerPos;
   private float xMoveDistance;
   //private float yMoveDistance;
    //存放页面显示对应的按钮
   public GameObject[] point;
    //存放页面没有显示对应的按钮
   public GameObject[] lostPoint;
   private float time;
   //记录OnBeginDrag时经过的时间
   private float saveTime;
   private bool isDrag=false;
   private float lastHosi=-1;
   private float smoothing =4;
   float targethorizontal = 0;
   private bool freePlay=false;
   //页面所对应的 rect.horizontalNormalizedPosition值
   private List<float> pagesHosi = new List<float>();
 // Use this for initialization
 void Start () {
        rect = this.transform.GetComponent<ScrollRect>();
        rect.horizontalNormalizedPosition = 0;
        mainGrid = rect.transform.GetChild(0);
        #region
        //point[0].SetActive(true);
        //point[1].SetActive(false);
        //point[2].SetActive(false);
        //point[3].SetActive(false);
        //for (int i = 0; i < lostPoint.Length; i++)
        //{
        //    lostPoint[i].GetComponent<Button>().onClick.AddListener(delegate()
        //    {
        //        Onclick(lostPoint[i].name);
        //    });
        //}
        #endregion
        ThisClick();
        Point(0);
        PageHosi();
     
 }
 
 // Update is called once per frame
 void Update () {
      
        if (!doNot)
        {
            UpdatePages();
      
        }
        //是否自动播放
        if (freePlay == false && pages.Count > 0)
       {
           time += Time.deltaTime;
           AutoPlay();
       }
        //判断是否加载完了,是否在自动播放,切换界面
        if (!isDrag && pages.Count > 0)
        {
            rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, Time.deltaTime * smoothing);
            if (rect.horizontalNormalizedPosition > 1)
            {
                rect.horizontalNormalizedPosition = 1;
            }
            else if (rect.horizontalNormalizedPosition < 0)
            {

           

                rect.horizontalNormalizedPosition = 0;
            }
        }
        #region
        //else
        //{
        //    judueFinger();     
        //}
        //rect.horizontalNormalizedPosition = 2 / ((float)(3));
        //if (pages.Count == 4&&s==false)
        //{
        //    //if (Input.GetMouseButtonDown(0))
        //    //{
        //    //    Debug.Log(pages[0].transform.position);
        //    //    Debug.Log(pages[1].transform.position);
        //    //    Debug.Log(pages[2].transform.position);
        //    //    Debug.Log(pages[3].transform.position);
        //    //}
        //    rect.horizontalNormalizedPosition = 2/ ((float)(3));
        //    s = true;

        //}
        #endregion
    }
    private void ThisClick()
    {
        for (int i = 0; i < lostPoint.Length; i++)
        {
            BtnOnClick(i, this.Onclick);

        }
    }
    private void PageHosi()
    {
        pagesHosi.Add(0);
        pagesHosi.Add(0.3333333f);
        pagesHosi.Add(0.666667f);
        pagesHosi.Add(1);
   
    }
    //得到所有的页面
    void UpdatePages()
    {
        #region
        // 获取子对象的数量
        ////int count = this.rect.content.childCount;
        ////int temp = 0;
        ////for (int i = 0; i < count; i++)
        ////{
        ////    if (this.rect.content.GetChild(i).gameObject.activeSelf)
        ////    {
        ////        temp++;
        ////    }
        ////}
        ////count = temp;

        ////if (pages.Count != count)
        ////{
        ////    if (count != 0)
        ////    {
        ////        pages.Clear();
        ////        for (int i = 0; i < count; i++)
        ////        {
        ////            float page = 0;
        ////            if (count != 1)
        ////                page = i / ((float)(count - 1));
        ////            pages.Add(page);
        ////            //     Debug.Log(i.ToString() + " page:" + page.ToString());
        ////        }
        ////    }
        ////}
        #endregion
        if (mainGrid.childCount==4)
        {
            Button[] btn = mainGrid.GetComponentsInChildren<Button>();
            foreach (Button item in btn)
            {
                pages.Add(item.gameObject);
            }
            doNot = true; 
        }    
      
}
    public void OnBeginDrag(PointerEventData eventData)
    {
        saveTime = time;
        isDrag = true;
        freePlay = true;
        lastHosi = rect.horizontalNormalizedPosition;
        startFingerPos = Input.mousePosition;
      
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
        nowFingerPos = Input.mousePosition;
        Debug.Log(saveTime);
        judueFinger();
      
       
     
    }
    public void Point(int index)
    {
        for (int i = 0; i < point.Length; i++)
        {
            if (i!=index)
            {
                point[i].SetActive(false);
            }
            else
            {
                point[i].SetActive(true);    
            }
        }
    }
   //点击按钮切换页面
    public void Onclick(int  go)
    {
        if (go==0)
        {
              targethorizontal=pagesHosi[0];
              time = 0;
              Point(0);
        }
        else if (go==1)
        {
            targethorizontal=pagesHosi[1];
            if (saveTime > 15.2)
            {
                time = 25.3f;
            }
            else
            {
                time = 5.3f;
            }
            Point(1);
        }
        else if (go==2)
        {
            targethorizontal=pagesHosi[2];
            if (saveTime > 15.2f)
            {
                time = 20.3f;
            }
            else
            {
                time = 10.3f;
            }
            Point(2);
        }
        else if (go==3)
        {
            targethorizontal=pagesHosi[3];
            time = 15.3f;
            Point(3);
        }

   
    }
    public delegate void BtnHandler(int  go);
    public int  BtnOnClick(int i, BtnHandler Onclickl)
    {
        //GameObject btnObj = GameObject.Find(btnName);
        //GameObject btnObj = GameUtility.FindDeepChild(this.gameObject, btnName).gameObject;
        //UnityEngine.UI.Button btn = btnObj.GetComponent<UnityEngine.UI.Button>();
        //btn.onClick.AddListener(delegate()
        //{
        //    onClick(btnObj);
        //});
        lostPoint[i].GetComponent<Button>().onClick.AddListener(delegate()
        {
            Onclickl(i);
        });;
        return i;
    }
    public void AutoPlay()
    {
        #region
        //if (time>=5)
        //{
        //    for (int i = 0; i < pagesHosi.Count; i++)
        //    {

        //        if (pagesHosi[i] - rect.horizontalNormalizedPosition < 0.01f)
        //        {
        //            if (i==3)
        //            {
        //                targethorizontal=pagesHosi[0];
        //                Point(0);
        //            }
        //            else
        //            {
        //                targethorizontal = pagesHosi[i + 1];
        //                Point(i+1);
        //            }

        //        }
        //    }
        //    //Debug.Log(rect.horizontalNormalizedPosition);
        //    time = 0;
        //}
        #endregion
        //0到1
        if (time >= 5 && time < 5.2f)
        {
            targethorizontal = pagesHosi[1];
            Point(1);
        }
        //1到2
        if (time >= 10 && time < 10.2f)
        {
            targethorizontal = pagesHosi[2];
            Point(2);
        }
        //2到3
        if (time >= 15 && time < 15.2f)
        {
            targethorizontal = pagesHosi[3];
            Point(3);
        }
        //3到2
        if (time >= 20 && time < 20.2f)
        {
            targethorizontal = pagesHosi[2];
            Point(2);

        }
        //2到1
        if (time >= 25 && time < 25.2f)
        {
            targethorizontal = pagesHosi[1];
            Point(1);

        }
        //1到0
        if (time >= 30)
        {
            targethorizontal = pagesHosi[0];
            Point(0);
            time = 0;

        }

    }
    //判断滑动的方向,切换页面
  public void judueFinger()
    {
      
        xMoveDistance = nowFingerPos.x - startFingerPos.x;
        Debug.Log(xMoveDistance);
        Debug.Log(lastHosi);
      //右滑
        if (xMoveDistance>0)
        {
            if (rect.horizontalNormalizedPosition!=0)
            {
                if (Mathf.Abs(lastHosi-pagesHosi[1])<0.1f)
                {
                    targethorizontal = pagesHosi[0];
                    time = 0;
                    Point(0);

                }
                else if (Mathf.Abs(lastHosi - pagesHosi[2]) < 0.1f)
                {
                    targethorizontal = pagesHosi[1];
                    if (saveTime>15.2)
                    {
                        time = 25.3f;
                    }
                    else
                    {
                        time = 5.3f;     
                    }
                   
                    Point(1);
                }
                else if (Mathf.Abs(lastHosi - pagesHosi[3]) < 0.1f)
                {
                   targethorizontal = pagesHosi[2];
                   if (saveTime>15.2f)
                   {
                       time = 20.3f;
                   }
                   else
                   {
                       time = 10.3f;  
                   }
                  
                   Point(2);
                }
               

                //targethorizontal = 0.333333f;
                //Debug.Log(targethorizontal);
            }
         
        }
         //左滑
        else if (xMoveDistance<0)
        {
            if (rect.horizontalNormalizedPosition!=1)
            {
                if (Mathf.Abs(lastHosi - pagesHosi[2]) < 0.1f)
                {
                    targethorizontal=pagesHosi[3];
                    time = 15.3f;
                    Point(3);
                }
                else if (Mathf.Abs(lastHosi - pagesHosi[1]) < 0.1f)
                {
                    targethorizontal=pagesHosi[2];
                    time = 10.3f;
                    if (saveTime > 15.2)
                    {
                       time = 20.3f;
                   }
                   else
                   {
                       time = 10.3f;  
                   }
                    Point(2);
                }
                else if (Mathf.Abs(lastHosi - pagesHosi[0]) < 0.1f)
                {
                    targethorizontal = pagesHosi[1];
                    if (saveTime > 15.2)
                    {
                        time = 25.3f;
                    }
                    else
                    {
                        time = 5.3f;
                    }
                    Point(1);
                }
               
            }
           
        }

        freePlay = false;

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值