美文网首页
untiy UI左右滑动

untiy UI左右滑动

作者: 玄策丶 | 来源:发表于2023-07-24 16:40 被阅读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++;
        }
    }
}

相关文章

  • 【圣】你个死鬼别想跑

    左右滑动查看更多 ??? 左右滑动查看更多 ??? 左右滑动查看更多 ??? 左右滑动查看更多 ??? 左右滑动查看更多

  • OC总结篇 - UI视图

    UI视图 - 图像显现原理和滑动优化UI视图 - UI事件传递及视图响应链UI视图 - UITableView重用...

  • iOS: 如何简单舒服的实现新浪微博个人主页效果

    类似于新浪微博个人主页的UI效果在iOS中是很常见的, 实现的难度就在于既可以左右滑动, 也可以上下滑动. 通常的...

  • swipe左右滑动

    一、HTML 全球购 母婴 出行 服务 厨房 饮食 服用 其他 全球购 二、css .bra...

  • 列表左右滑动

    在列表UL上样式上加上 display: -webkit-box; overflow-x: auto; 就可以滑...

  • react 合成事件

    1. 场景 父组件是个左右可滑动的组件,子组件是可左右滑动的图片展示。功能是手指左右滑动时可页面切换,但是在滑动图...

  • 自定义ViewGroup(1)

    滑动左右两边的ListView独立滑动,滑动中间的ListView整体滑动 实现代码:

  • 虚拟制作有哪些?像素造影分享

    左右滑动查看

  • unity-UI-RawImage-SetNativeSize

    在untiy ui的图片格式选择问题上我们一般会选择sprite,但是在遇到大尺寸图片时sprite会占用非常大的...

  • vim 速查表

    基本操作 < 如显示不全,请左右滑动 > 方向键 < 如显示不全,请左右滑动 > 浏览文档 < 如显示不全,请左右...

网友评论

      本文标题:untiy UI左右滑动

      本文链接:https://www.haomeiwen.com/subject/oqohydtx.html