unity | 动画模块之循环滚动选项框

一、作者的话

评论区有人问,有没有竖排循环轮播选项框,我就写了一个

二、效果动画

如果不是你们想要的,就省的你们继续往下看了

三、制作思路

把移动分成里面的方块,还有背景(父物体),方块自己移动,背景(父物体)控制方块在触碰到边界的时候移动位置,保证循环。

五、所有物体总览

脚本说明:

图片循环轮播物体上,挂有ControlMoveItem脚本

其他0-7物体上,均挂有MoveItem脚本

四、小方块(有数字的部分)制作思路

当点击到小方块时,所有小方块根据鼠标拖动的距离进行移动。

1.在小方块上加入EventSystems,用来识别小方块是否被按到。

在这里告诉父物体是否有人被按下,是为了方便其他小方块知道,是不是有物体被按下了

public class MoveItem : MonoBehaviour,IPointerDownHandler,IPointerUpHandler
{
    //声明父物体身上的脚本
    ControlMoveItem controlMoveItem;

    void Start()
    {
        //获取到父物体的身上的脚本
        controlMoveItem = transform.parent.GetComponent<ControlMoveItem>();
        
    }
    
    //当自己被按下时,告诉父物体,自己被按下了
    public void OnPointerDown(PointerEventData eventData)
    {
        controlMoveItem.isButtonDown = true;
    }
    
    //当鼠标抬起时,告诉父物体,没有被按了
    public void OnPointerUp(PointerEventData eventData)
    {
        controlMoveItem.isButtonDown = false;
    }
}


2.当物体被按下时,每个小方块,都挪动和鼠标相同的位置

 void Update()
    {
        bool isButtonDown = controlMoveItem.isButtonDown;
        if (isButtonDown == true)
        {
            if (mouthPosition == Vector3.zero)
            {
                mouthPosition = Input.mousePosition;
            }
            else
            {
                Vector3 del = Input.mousePosition - mouthPosition;
                transform.position += new Vector3(0, del.y, 0);
                mouthPosition = Input.mousePosition;
            }
        }
        else {
            mouthPosition = Vector3.zero;
        }
    }

3.总代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MoveItem : MonoBehaviour,IPointerDownHandler,IPointerUpHandler
{
    ControlMoveItem controlMoveItem;
    Vector3 mouthPosition = new Vector3();

    public void OnPointerDown(PointerEventData eventData)
    {
        controlMoveItem.isButtonDown = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        controlMoveItem.isButtonDown = false;
    }

    void Start()
    {
        controlMoveItem = transform.parent.GetComponent<ControlMoveItem>();
    }

    void Update()
    {
        bool isButtonDown = controlMoveItem.isButtonDown;
        if (isButtonDown == true)
        {
            if (mouthPosition == Vector3.zero)
            {
                mouthPosition = Input.mousePosition;
            }
            else
            {
                Vector3 del = Input.mousePosition - mouthPosition;
                if (controlMoveItem.dir == Dir.Vertical)
                {
                    transform.position += new Vector3(0, del.y, 0);
                }
                mouthPosition = Input.mousePosition;
            }
        }
        else {
            mouthPosition = Vector3.zero;
        }
    }
}
四、大方块(父物体)制作思路

1.读取显示的第一个物体和最后一个物体的位置

这样当超过这个位置的时候,我就知道,要移动别的方块了

    public bool isButtonDown = false;
    public int lastIndex = 5;

    Vector3 firstPosition;
    Vector3 lastPosition;

    void Start()
    {
        firstPosition = transform.GetChild(0).GetComponent<RectTransform>().anchoredPosition;
        lastPosition = transform.GetChild(lastIndex).GetComponent<RectTransform>().anchoredPosition;
    }

2.读取第一个小方块和第二个小方块之间的距离

这样当设置新移动过来的位置的时候,我知道把方块放到哪里

    public bool isButtonDown = false;
    public int lastIndex = 5;

    float d;
    Vector3 firstPosition;
    Vector3 lastPosition;

    void Start()
    {
            d = Mathf.Abs(transform.GetChild(0).localPosition.y - transform.GetChild(1).localPosition.y);

        firstPosition = transform.GetChild(0).GetComponent<RectTransform>().anchoredPosition;
        lastPosition = transform.GetChild(lastIndex).GetComponent<RectTransform>().anchoredPosition;
    }

3.如果向上滑动,第一个物体已经超过上方的线了,就要在队尾补物体了,

反之,如果向下滑动,最后一个物体如果已经超过最下方的线了,就要在上方补物体了

补的物体就是现在队头(或队尾)的现在的位置,加上(或减去)两个小方块的距离

    void Update()
    {
            if (transform.GetChild(0).GetComponent<RectTransform>().anchoredPosition.y < firstPosition.y)
            {
                Transform changeT = transform.GetChild(transform.childCount - 1);
                changeT.localPosition = transform.GetChild(0).localPosition + new Vector3(0, d, 0);
                changeT.SetSiblingIndex(0);
            }
            else if (transform.GetChild(transform.childCount - 1).GetComponent<RectTransform>().anchoredPosition.y > lastPosition.y)
            {
                Transform changeT = transform.GetChild(0);
                changeT.localPosition = transform.GetChild(transform.childCount - 1).localPosition - new Vector3(0, d, 0);
                changeT.SetSiblingIndex(transform.childCount - 1);
            }
    }

4.全部代码

using UnityEngine;

public class ControlMoveItem : MonoBehaviour
{
    public bool isButtonDown = false;
    public int lastIndex = 5;

    float d;
    Vector3 firstPosition;
    Vector3 lastPosition;

    void Start()
    {
            d = Mathf.Abs(transform.GetChild(0).localPosition.y - transform.GetChild(1).localPosition.y);

        firstPosition = transform.GetChild(0).GetComponent<RectTransform>().anchoredPosition;
        lastPosition = transform.GetChild(lastIndex).GetComponent<RectTransform>().anchoredPosition;
    }

    void Update()
    {
            if (transform.GetChild(0).GetComponent<RectTransform>().anchoredPosition.y < firstPosition.y)
            {
                Transform changeT = transform.GetChild(transform.childCount - 1);
                changeT.localPosition = transform.GetChild(0).localPosition + new Vector3(0, d, 0);
                changeT.SetSiblingIndex(0);
            }
            else if (transform.GetChild(transform.childCount - 1).GetComponent<RectTransform>().anchoredPosition.y > lastPosition.y)
            {
                Transform changeT = transform.GetChild(0);
                changeT.localPosition = transform.GetChild(transform.childCount - 1).localPosition - new Vector3(0, d, 0);
                changeT.SetSiblingIndex(transform.childCount - 1);
            }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/221288.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SI24R03 高度集成低功耗SOC 2.4G 收发一体芯片

今天给大家介绍一款Soc 2.4G 收发一体模块-SI24R03 Si24R03是一款高度集成的低功耗无线SOC芯片&#xff0c;芯片为QFN32 5x5mm封装&#xff0c;集成了资源丰富的MCU内核与2.4G收发器模块&#xff0c;最低功耗可达1.6uA&#xff0c;极少外围器件&#xff0c;大幅降低系统应用成本…

电子学会C/C++编程等级考试2023年03月(四级)真题解析

C/C++等级考试(1~8级)全部真题・点这里 第1题:最佳路径 如下所示的由正整数数字构成的三角形: 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 从三角形的顶部到底部有很多条不同的路径。对于每条路径,把路径上面的数加起来可以得到一个和,和最大的路径称为最佳路径。你的任务就是求出最…

spring 框架的 AOP

AOP依赖导入 <!-- AOP依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>

Git版本控制---入门学习

1.简介 是一个免费的开源分布式版本控制系统工具&#xff0c;旨在快速高效地处理从小型到大型的所有项目。 它是由 Linus Torvalds 在2005年创建的&#xff0c;用于开发 Linux 内核。 Git具有大多数团队和开发人员所需的功能、性能、安全性和灵活性。 它还用作重要的分布式版本…

【matlab程序】matlab画子图的多种样式

【matlab程序】matlab画子图的多种样式

openEuler操作系统安装

所需要的软件镜像 https://repo.openeuler.org/openEuler-20.03-LTS/ISO/x86_64/ 选择openEuler-20.03-LTS-everything-x86_64-dvd.iso 版本的最完整 如果硬盘空间小可选择openEuler-20.03-LTS-x86_64-dvd.iso 安装步骤 1 选择第一个 install openEuler 20.03-LTS 2 选择语…

ultralytics yolo图像分类训练案例;pytorch自有数据集图像分类案例

1、ultralytics yolo图像分类训练案例 优点:使用方便,训练过程评估指标可以方便查看 缺点:自带模型少,可选择自定义小 参考:https://docs.ultralytics.com/tasks/classify/#val https://blog.csdn.net/weixin_42357472/article/details/131412851 1)数据集格式 https://…

quickapp_快应用_快应用与h5交互

快应用与h5交互 h5跳转到快应用[1] 判断当前环境是否支持组件跳转快应用[2] h5跳转到快应用(1)deeplink方式进行跳转(推荐)(2)h5点击组件(接收参数存在问题)(3)url配置跳转(官方不推荐) 问题-浏览器问题 web组件h5页面嵌入快应用快应用发送消息到h5页面h5页面接收快应用发送的消…

NFC物联网解决方案应用实例:基于NFC的通用物流链防伪溯源

NFC物联网系统解决方案已在某局进行推广应用&#xff0c;给出了某省内出口蔬菜水果检验检疫监管的物联网解决方案。 依据相关法规&#xff0c;出口蔬菜必须在质检总局注册种植基地进行种植&#xff0c;出口前按批次向产地检验检疫部门进行申报&#xff0c;按时在集中监管区统一…

Windows XP安装SVN软件

SVN全称为SubVersion&#xff0c;是Apache开源软件协议下&#xff0c;一个用于代码分布式管理的工具&#xff0c;其孵化的软件产品是TortoiseSVN&#xff0c;该软件是带图形界面的代码管理工具&#xff0c;类似于Git&#xff0c;多了一个图形界面&#xff0c;方便鼠标操作。  …

Android : 篮球记分器app _简单应用

示例图&#xff1a; 1.导包 在build.gradle 中 加入 // 使用androidx版本库implementation androidx.lifecycle:lifecycle-extensions:2.1.0-alpha03 2. 开启dataBinding android{...// 步骤1.开启data bindingdataBinding {enabled true}...} 3.写个类继承 ViewModel pac…

设计一个简易版本的分布式任务调度系统

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱吃芝士的土豆倪&#xff0c;24届校招生Java选手&#xff0c;很高兴认识大家&#x1f4d5;系列专栏&#xff1a;Spring源码、JUC源码、Kafka原理、分布式技术原理&#x1f525;如果感觉博主的文章还不错的话&#xff…

96基于matlab的GMDH神经网络对YPML120 时间序列进行预测

基于matlab的GMDH神经网络对YPML120 时间序列进行预测&#xff0c;输出训练数据和测试数据的结果&#xff0c;及预测均方根误差结果和正态分布。数据可更换自己的&#xff0c;程序已调通&#xff0c;可直接运行。 96matlabGMDH神经网络 (xiaohongshu.com)

MQTT框架和使用

目录 MQTT框架 1. MQTT概述 1.1 形象地理解三个角色 1.2 消息的传递 2. 在Windows上体验MQTT 2.1 安装APP 2.2 启动服务器 2.3 使用MQTTX 2.3.1 建立连接 2.3.2 订阅主题 2.3.3 发布主题 2.4 使用mosquitto 2.4.1 发布消息 2.4.2 订阅消息 3. kawaii-mqtt源码分析…

音乐律动效果

先上图 代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>音乐律动效果</title><style>* {margin: 0;padding: 0;}li {list-style: none;}.container .img {width: 200px;height: 200…

k8s之镜像拉取时使用secret

k8s之secret使用 一、说明二、secret使用2.1 secret类型2.2 创建secret2.3 配置secret 一、说明 从公司搭建的网站镜像仓库&#xff0c;使用k8s部署服务时拉取镜像失败&#xff0c;显示未授权&#xff1a; 需要在拉取镜像时添加认证信息. 关于secret信息,参考: https://www.…

微信小程序之猜数字和猜拳小游戏

目录 效果图 app.json 一、首页&#xff08;index3&#xff09;的代码 wxml代码 wxss代码 二、猜数字页面&#xff08;index&#xff09;代码 wxml代码 wxss代码 js代码 三.游戏规则页面&#xff08;logs&#xff09;代码 wxml代码 wxss代码 四.猜拳页面&#xff…

pthread学习遇到的问题

1.pthread_t 是个类型&#xff0c;指的是线程ID。pthread_create&#xff08;&#xff09;的时候穿地址进去&#xff0c;线程创建好后就会成为线程ID&#xff08;即输出型参数&#xff09; 2.pthread_self() pthread_self()获得是调用这个函数的线程ID &#xff08;我以为是…

随着互联网的快速发展,日常网站监测工具显得越发重要

德迅云安全-领先云安全服务与解决方案提供商 如今&#xff0c;随着互联网的快速发展&#xff0c;网站安全问题日益凸显。恶意攻击、数据泄露、黑客入侵等威胁不断涌现&#xff0c;给企业和个人的信息安全带来了巨大风险。因此&#xff0c;选择一个强大的网站安全监测工具成为了…

Django 开发 web 后端,好用过 SpringBoot ?

基础语法 Django&#xff08;Python&#xff09;&#xff1a;以简洁和直观著称。它允许更快的开发速度&#xff0c;特别适合快速迭代的项目。例如&#xff0c;一个简单的视图函数&#xff1a; from django.http import HttpResponsedef hello_world(request):return HttpRespon…
最新文章