untiy UI左右滑动

2023-07-24  本文已影响0人  玄策丶

1、触控屏上左右滑动翻页

/*****************************
 * Title:        
 * Date:         2023.01.01
 * Author:       玄策
 * UnityVersion: 2019.4.28
 * Func:              
 * 
 ****************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.Video;
public class Test : MonoBehaviour
{
    public Transform[] Trs;
    public int Index;

    bool isPress;
    private void Awake()
    {
        //锁定最大帧率为60帧
        //Application.targetFrameRate = 60;
    }
    void Start()
    {

        //将子物体加入集合
        Trs = new Transform[transform.childCount];
        for (int i = 0; i < transform.childCount; i++)
        {
            Trs[i] = transform.GetChild(i);
        }

        //初始
        Trs[0].GetComponent<VideoPlayer>().Play();
        for (int i = 1; i < Trs.Length; i++)
        {
            Trs[i].gameObject.SetActive(false);
        }
        
    }

    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A) && Index < Trs.Length -1 && !isPress)
        {
            isPress = true;
            Debug.Log("往左滑动");
            //中间视频往左
            Trs[Index].DOLocalMoveX(-1920, 1f).SetEase(Ease.OutBack).OnComplete(
                () =>
                {
                    Trs[Index - 1].gameObject.SetActive(false);
                    isPress = false;
                });
            Trs[Index].GetComponent<VideoPlayer>().Stop();

            //右边视频往左
            Trs[Index + 1].gameObject.SetActive(true);
            Trs[Index + 1].localPosition = new Vector3(1920, 0, 0);
            Trs[Index + 1].DOLocalMoveX(0, 1f).SetEase(Ease.OutBack);
            Trs[Index + 1].GetComponent<VideoPlayer>().Play();
            
            Index++;
        }

        if(Input.GetKeyDown(KeyCode.D) && Index > 0 && !isPress)
        {
            isPress = true;
            Debug.Log("往右滑动");
            //中间视频往右
            Trs[Index].DOLocalMoveX(1920, 1f).SetEase(Ease.OutBack).OnComplete(
                () =>
                {
                    Trs[Index + 1].gameObject.SetActive(false);
                    isPress = false;
                }); ;
            Trs[Index].GetComponent<VideoPlayer>().Stop();

            //左边视频往右
            Trs[Index - 1].gameObject.SetActive(true);
            Trs[Index - 1].localPosition = new Vector3(-1920, 0, 0);
            Trs[Index - 1].DOLocalMoveX(0, 1f).SetEase(Ease.OutBack);
            Trs[Index - 1].GetComponent<VideoPlayer>().Play();

            Index--;
        }
    }
}

2、左右按钮控制翻页

/*****************************
 * Title:        
 * Date:         2023.01.01
 * Author:       玄策
 * UnityVersion: 2022.3.0
 * Func:              
 * 
 ****************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PageChange : MonoBehaviour
{
    public List<Transform> _listTr;         //翻页面集合
    int Index;                              //翻页面Index
    public Button BtnUpPage, BtnNextPage;  //上下翻页按钮

    void Start()
    {
        BtnUpPage.onClick.AddListener(UpPage);
        BtnNextPage.onClick.AddListener(NextPage);
    }

    private void Update()
    {
        if (Index == 0)
        {
            BtnUpPage.interactable = false;
        }
        else if (Index == _listTr.Count - 1)
        {
            BtnNextPage.interactable = false;
        }
        else
        {
            BtnUpPage.interactable = true;
            BtnNextPage.interactable = true;
        }
    }

    void UpPage()
    {
        if (Index > 0)
        {
            _listTr[Index].gameObject.SetActive(false);
            _listTr[Index - 1].gameObject.SetActive(true);
            Index--;
        }
    }

    void NextPage()
    {
        if (Index < _listTr.Count - 1)
        {
            _listTr[Index].gameObject.SetActive(false);
            _listTr[Index + 1].gameObject.SetActive(true);
            Index++;
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读