本文主要是介绍Unity 制作跑马灯抽奖,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、效果展示
跑马灯抽奖
二、解决方案
- 动画使用DoTween 插件实现
- DOPath 按路径移动
- SetEase 可以设置先快后慢等效果
三、目录结构
四、代码实现
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;public class GoLuckyDraw : MonoBehaviour
{ private GameObject transparentBox;public List<Transform> boxList = new List<Transform>();public List<Transform> chooseBoxList = new List<Transform>();// 点了抽奖按钮正在抽奖 private bool isOnClickPlaying;// 获奖idprivate int rewardIndex;// 记录上一次的获奖的idprivate int lastIndex = 0;void Start(){for (int i = 1; i <= 10; i++){var name = "Image_" + i;var sprite = transform.Find(name);boxList.Add(sprite);}transparentBox = transform.Find("TransparentBox").gameObject;transparentBox.SetActive(false);isOnClickPlaying = false;}IEnumerator Move(){List<Vector3> waypoints = new List<Vector3>(); //先转几圈 for (int i = lastIndex; i < boxList.Count; i++){waypoints.Add(boxList[i].position);}for (int i = 0; i < boxList.Count; i++){waypoints.Add(boxList[i].position);}for (int i = 0; i < boxList.Count; i++){waypoints.Add(boxList[i].position);}for (int i = 0; i < boxList.Count; i++){waypoints.Add(boxList[i].position);}// 最后一圈for (int i = 0; i < chooseBoxList.Count; i++){waypoints.Add(chooseBoxList[i].position);}Tween _tweener = transparentBox.transform.DOPath(waypoints.ToArray(),5,PathType.Linear).SetEase(Ease.InOutSine);yield return _tweener.WaitForCompletion();transparentBox.gameObject.SetActive(false);isOnClickPlaying = false;lastIndex = rewardIndex;}private void chooseIndex( ){chooseBoxList.Clear();rewardIndex = Random.Range(0, boxList.Count); for (int i = 0; i < rewardIndex; i++){chooseBoxList.Add(boxList[i]);}}public void StartLuckyDraw(){if (isOnClickPlaying)return;chooseIndex();transparentBox.gameObject.SetActive(true);StartCoroutine(Move());isOnClickPlaying = true;}}
这篇关于Unity 制作跑马灯抽奖的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!