栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

unity滑动层叠图片轮播展示代码

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity滑动层叠图片轮播展示代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System;
using System.Text.RegularExpressions;
public class ImgSliderMove : MonoBehaviour
{

    public Transform Imgs;
    public Button Left;
    public Button Right;
    public Sprite[] Sprite2D;
    float moveSpeed = 0.2f;
    Vector3 startPos;
    Vector3 stayPos;
    Vector3 endPos;
    Vector3 direction;
    public static bool IsCanClickBtn = true;
    public static int panoramaID = 0;
    public Text TextName;
    void Start()
    {
        Left.onClick.AddListener(LeftMoveToRight);
        Right.onClick.AddListener(RightMoveToLeft);
    }
    void Update()
    {
        //平板端未测试
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            switch (touch.phase)
            {
                case TouchPhase.Began:
                    startPos = touch.position;
                    break;
                case TouchPhase.Moved: {
                        stayPos = touch.position;
                        if ((startPos - stayPos).magnitude >= 100)
                            IsCanClickBtn = false;
                    }
                    break;
                case TouchPhase.Ended:{
                        endPos = touch.position;
                        direction = (startPos - endPos);
                        if (direction.magnitude >= 100)
                        {
                            IsCanClickBtn = false;
                            if (Math.Abs(direction.x) > Math.Abs(direction.y))
                            {
                                if (startPos.x > endPos.x)
                                    RightMoveToLeft();
                                else
                                    LeftMoveToRight();
                            }
                        }
                    }
                    break;
            }
        }
        //桌面端已测试
        if (Input.GetMouseButtonDown(0))
        {
            startPos = Input.mousePosition;
        }
        if (Input.GetMouseButton(0))
        {
            stayPos = Input.mousePosition;
            if ((startPos - stayPos).magnitude >= 100)
            {
                IsCanClickBtn = false;
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            endPos = Input.mousePosition;
            direction = (startPos - endPos);
            if (direction.magnitude >= 100)
            {
                IsCanClickBtn = false;
                if (Math.Abs(direction.x) > Math.Abs(direction.y))
                {
                    if (startPos.x > endPos.x)
                        RightMoveToLeft();
                    else
                        LeftMoveToRight();
                }
            }
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
    void LeftMoveToRight()
    {
        string str = Imgs.GetChild(0).GetChild(0).GetComponent().sprite.name;
        string result = Regex.Replace(str, @"[^0-9]+", "");
        int imgId = int.Parse(result);
        for (int i = 0; i < Imgs.childCount; i++)
        {    
            if (Imgs.GetChild(i).localPosition.x == 300)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(0));
                img.DOLocalMoveX(0, 0f);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear).OnComplete(() => {
                    IsCanClickBtn = true;
                });
                if (imgId == 0)
                {
                    imgId = Sprite2D.Length - 1;
                }
                else
                {
                    imgId -= 1;
                }
                img.GetComponent().sprite = Sprite2D[imgId];
                img.GetComponent().color = new Color(1, 1, 1, 130 / 255f);
                panoramaID--;
                if (panoramaID == -1)
                {
                    panoramaID = Sprite2D.Length-1;
                }
            }
            else if(Imgs.GetChild(i).localPosition.x==-300)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(2));
                img.DOLocalMoveX(0, moveSpeed).SetEase(Ease.Linear);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear);
                img.GetComponent().color = new Color(1, 1, 1, 190 / 255f);
            }
            else if (Imgs.GetChild(i).localPosition.x == -150)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(4));
                img.DOLocalMoveX(0, moveSpeed).SetEase(Ease.Linear);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear);
                img.GetComponent().color = new Color(1, 1, 1, 255 / 255f);
            }
            else if (Imgs.GetChild(i).localPosition.x == 0)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(3));
                img.DOLocalMoveX(0, moveSpeed).SetEase(Ease.Linear);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear);
                img.GetComponent().color = new Color(1, 1, 1, 190 / 255f);
            }
            else if (Imgs.GetChild(i).localPosition.x == 150)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(1));
                img.DOLocalMoveX(0, moveSpeed).SetEase(Ease.Linear);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear);
                img.GetComponent().color = new Color(1, 1, 1, 130 / 255f);
            }
        }
        string textA = Sprite2D[panoramaID].name;
        TextName.text = Regex.Replace(textA, @"d", "");
    }
    void RightMoveToLeft()
    {
        string str = Imgs.GetChild(1).GetChild(0).GetComponent().sprite.name;
        string result = Regex.Replace(str, @"[^0-9]+", "");
        int imgId = int.Parse(result);
        for (int i = 0; i < Imgs.childCount; i++)
        {
            if (Imgs.GetChild(i).localPosition.x == -300)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(1));
                img.DOLocalMoveX(0, 0f);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear).OnComplete(() => {
                    IsCanClickBtn = true;
                });
                img.GetComponent().color = new Color(1, 1, 1, 130 / 255f);
                if (imgId == Sprite2D.Length - 1)
                    imgId = 0;
                else
                    imgId += 1;
                img.GetComponent().sprite = Sprite2D[imgId];
                panoramaID++;
                if (panoramaID == Sprite2D.Length)
                    panoramaID = 0;
            }
            else if (Imgs.GetChild(i).localPosition.x == -150)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(0));
                img.DOLocalMoveX(0, moveSpeed).SetEase(Ease.Linear);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear);
                img.GetComponent().color = new Color(1, 1, 1, 130 / 255f);
            }
            else if (Imgs.GetChild(i).localPosition.x == 0)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(2));
                img.DOLocalMoveX(0, moveSpeed).SetEase(Ease.Linear);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear);
                img.GetComponent().color = new Color(1, 1, 1, 190 / 255f);
            }
            else if (Imgs.GetChild(i).localPosition.x == 150)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(4));
                img.DOLocalMoveX(0, moveSpeed).SetEase(Ease.Linear);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear);
                img.GetComponent().color = new Color(1, 1, 1, 255 / 255f);
            }
            else if (Imgs.GetChild(i).localPosition.x == 300)
            {
                Transform img = Imgs.GetChild(i).GetChild(0);
                img.SetParent(Imgs.GetChild(3));
                img.DOLocalMoveX(0, moveSpeed).SetEase(Ease.Linear);
                img.DOScale(1f, moveSpeed).SetEase(Ease.Linear);
                img.GetComponent().color = new Color(1, 1, 1, 190 / 255f);
            }
        }
        string textA = Sprite2D[panoramaID].name;
        TextName.text = Regex.Replace(textA, @"d", "");
    }
}

图片面板布局:

布局中的-300_0.7等组件属性为空,-300_0.7表示此组件x相对坐标为-300,组件scale属性为0.7。

Sprite2D的0号位图片在0_1子对象img(Imgs.GetChild(4)),
Sprite2D的1号位图片在150_0.85(Imgs.GetChild(3)),
Sprite2D的2号位图片在300_0.7(Imgs.GetChild(1)),
Sprite2D的倒数第一号位图片在-150_0.85(Imgs.GetChild(2)),
Sprite2D的倒数第二号位图片在-300_0.7(Imgs.GetChild(0))。

补充:精灵图片命名需要带唯一数字标识符获取当前精灵编号满足int imgId = int.Parse(result)代码,并严格排序。

实现效果:
原生JS层叠图片轮播展示代码

简化后的代码,不利于让人直观分析:

using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System.Text.RegularExpressions;

public class CardMove : MonoBehaviour
{
    public Sprite[] Sprite2D;
    Transform Imgs;
    int imgId = 0;

    void Start()
    {
        Imgs = transform.Find("Imgs");
        transform.Find("LeftBtn").GetComponent
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901195.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号