unity2d实现图片滑动切换的具体操作和完整代码
时间: 2024-05-14 22:12:02 浏览: 127
实现图片滑动切换的具体操作和完整代码如下:
1. 创建一个空对象,命名为"ImageSlider",并添加UI/Image组件;
2. 在"ImageSlider"对象下创建子对象,命名为"Viewport",并添加UI/Image组件和Mask组件,将Mask的Mode设置为"Rect";
3. 在"Viewport"对象下创建子对象,命名为"Content",并添加UI/Image组件,设置好图片的大小和位置;
4. 在"Content"对象上添加一个脚本,命名为"ImageSliderController",并将以下代码添加到脚本中:
```csharp
using UnityEngine;
using UnityEngine.UI;
public class ImageSliderController : MonoBehaviour
{
public Sprite[] images; // 图片数组
public float transitionSpeed = 1f; // 切换速度
private int currentIndex = 0; // 当前图片索引
private Image contentImage; // Content对象上的Image组件
void Start()
{
contentImage = transform.GetChild(0).GetComponent<Image>();
contentImage.sprite = images[currentIndex];
}
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow)) // 按右箭头切换到下一张图片
{
currentIndex++;
if (currentIndex >= images.Length) currentIndex = 0;
StartCoroutine(TransitionToNextImage());
}
if (Input.GetKeyDown(KeyCode.LeftArrow)) // 按左箭头切换到上一张图片
{
currentIndex--;
if (currentIndex < 0) currentIndex = images.Length - 1;
StartCoroutine(TransitionToPreviousImage());
}
}
IEnumerator TransitionToNextImage()
{
float t = 0f;
Vector3 startPosition = contentImage.rectTransform.localPosition;
Vector3 endPosition = new Vector3(-contentImage.rectTransform.rect.width, 0f, 0f);
while (t < 1f)
{
t += Time.deltaTime * transitionSpeed;
contentImage.rectTransform.localPosition = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
contentImage.rectTransform.localPosition = endPosition;
contentImage.sprite = images[currentIndex];
contentImage.rectTransform.localPosition = new Vector3(contentImage.rectTransform.rect.width, 0f, 0f);
t = 0f;
startPosition = contentImage.rectTransform.localPosition;
endPosition = Vector3.zero;
while (t < 1f)
{
t += Time.deltaTime * transitionSpeed;
contentImage.rectTransform.localPosition = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
contentImage.rectTransform.localPosition = endPosition;
}
IEnumerator TransitionToPreviousImage()
{
float t = 0f;
Vector3 startPosition = contentImage.rectTransform.localPosition;
Vector3 endPosition = new Vector3(contentImage.rectTransform.rect.width, 0f, 0f);
while (t < 1f)
{
t += Time.deltaTime * transitionSpeed;
contentImage.rectTransform.localPosition = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
contentImage.rectTransform.localPosition = endPosition;
contentImage.sprite = images[currentIndex];
contentImage.rectTransform.localPosition = new Vector3(-contentImage.rectTransform.rect.width, 0f, 0f);
t = 0f;
startPosition = contentImage.rectTransform.localPosition;
endPosition = Vector3.zero;
while (t < 1f)
{
t += Time.deltaTime * transitionSpeed;
contentImage.rectTransform.localPosition = Vector3.Lerp(startPosition, endPosition, t);
yield return null;
}
contentImage.rectTransform.localPosition = endPosition;
}
}
```
5. 将需要切换的图片拖入"ImageSliderController"脚本的"images"数组中;
6. 运行游戏,按左右箭头即可切换图片。
完整代码戳这里:https://github.com/Unity3dAzure/Unity2D-ImageSlider/blob/master/Assets/Scripts/ImageSliderController.cs
阅读全文