上效果
?
?实现起来也很简单,配合DoTween插件,一个数组,一个索引就可以实现。指定五个位置,通过索引拿到图片在五个位置上一直tween就可以了。
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MySelect : MonoBehaviour
{
public Image[] all;
public int StartIndex = 0;
public Transform pos_left_2;
public Transform pos_left;
public Transform pos_Mid;
public Transform pos_right;
public Transform pos_right_2;
int length = 0;
public float speed = 1f;
[HideInInspector]
public bool IsTweening = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
TurnLeft();
if (Input.GetKeyDown(KeyCode.RightArrow))
TurnRight();
}
private void Start()
{
length = all.Length;
Mid_Index = StartIndex;
init();
}
int Mid_Index = 0;
int left_Index;
int left_Index_2;
int right_Index;
int right_Index_2;
public void init()
{
for (int i = 0; i < length; i++)
{
all[i].transform.localScale = Vector3.zero;
all[i].gameObject.SetActive(false);
}
all[Mid_Index].transform.position = pos_Mid.position;
all[Mid_Index].transform.localScale = pos_Mid.localScale;
all[Mid_Index].gameObject.SetActive(true);
left_Index = Mid_Index - 1;
if (left_Index < 0) left_Index += length;
all[left_Index].transform.position = pos_left.position;
all[left_Index].transform.localScale = pos_left.localScale;
all[left_Index].gameObject.SetActive(true);
right_Index = Mid_Index + 1;
if (right_Index >= length) right_Index -= length;
all[right_Index].transform.position = pos_right.position;
all[right_Index].transform.localScale = pos_right.localScale;
all[right_Index].gameObject.SetActive(true);
all[right_Index].transform.SetAsLastSibling();
all[left_Index].transform.SetAsLastSibling();
all[Mid_Index].transform.SetAsLastSibling();
}
//向左 索引增加
public void TurnLeft()
{
if (!IsTweening)
StartCoroutine(ITurnLeft());
}
IEnumerator ITurnLeft()
{
IsTweening = true;
//右侧 生成一个
right_Index_2 = right_Index + 1;
if (right_Index_2 >= length) right_Index_2 -= length;
all[right_Index_2].transform.position = pos_right_2.position;
all[right_Index_2].transform.localScale = pos_right_2.localScale;
all[right_Index_2].gameObject.SetActive(true);
all[right_Index_2].transform.SetAsLastSibling();
all[right_Index].transform.SetAsLastSibling();
all[Mid_Index].transform.SetAsLastSibling();
//滑动
all[right_Index_2].transform.DOMove(pos_right.position, speed);
all[right_Index_2].transform.DOScale(pos_right.localScale, speed);
all[right_Index].transform.DOMove(pos_Mid.position, speed);
all[right_Index].transform.DOScale(pos_Mid.localScale, speed);
all[Mid_Index].transform.DOMove(pos_left.position, speed);
all[Mid_Index].transform.DOScale(pos_left.localScale, speed);
all[left_Index].transform.DOMove(pos_left_2.position, speed); ;
all[left_Index].transform.DOScale(pos_left_2.localScale, speed);
yield return new WaitForSeconds(speed);
all[left_Index].gameObject.SetActive(false);
Mid_Index++;
if (Mid_Index >= length) Mid_Index -= length;
left_Index = Mid_Index - 1;
if (left_Index < 0) left_Index += length;
right_Index = Mid_Index + 1;
if (right_Index >= length) right_Index -= length;
IsTweening = false;
}
//向右 索引减小
public void TurnRight()
{
Debug.LogError($"left_2={left_Index_2},left={left_Index},mid={Mid_Index},right={right_Index},right_2={right_Index_2}");
//左侧 生成一个
if (!IsTweening)
StartCoroutine(ITurnRight());
}
IEnumerator ITurnRight()
{
IsTweening = true;
left_Index_2 = left_Index - 1;
if (left_Index_2 < 0) left_Index_2 += length;
all[left_Index_2].transform.position = pos_left_2.position;
all[left_Index_2].transform.localScale = pos_left_2.localScale;
all[left_Index_2].gameObject.SetActive(true);
all[left_Index_2].transform.SetAsLastSibling();
all[left_Index].transform.SetAsLastSibling();
all[Mid_Index].transform.SetAsLastSibling();
//滑动
all[left_Index_2].transform.DOMove(pos_left.position, speed);
all[left_Index_2].transform.DOScale(pos_left.localScale, speed);
all[left_Index].transform.DOMove(pos_Mid.position, speed);
all[left_Index].transform.DOScale(pos_Mid.localScale, speed);
all[Mid_Index].transform.DOMove(pos_right.position, speed);
all[Mid_Index].transform.DOScale(pos_right.localScale, speed);
all[right_Index].transform.DOMove(pos_right_2.position, speed); ;
all[right_Index].transform.DOScale(pos_right_2.localScale, speed);
yield return new WaitForSeconds(speed);
all[right_Index].gameObject.SetActive(false);
Mid_Index--;
if (Mid_Index < 0) Mid_Index += length;
left_Index = Mid_Index - 1;
if (left_Index < 0) left_Index += length;
right_Index = Mid_Index + 1;
if (right_Index >= length) right_Index -= length;
IsTweening = false;
}
}
|