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


