111 lines
4.0 KiB
C#
111 lines
4.0 KiB
C#
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using DG.Tweening;
|
||
|
||
public class MoneyAnimation : MonoBehaviour
|
||
{
|
||
public Transform Parent; // 设置父节点,调高层级,避免被其他UI遮挡
|
||
public GameObject coinPrefab; // 金币的预制体
|
||
public Transform startPoint; // 金币生成的起点(例如箱子的位置)
|
||
public Transform[] targetPoints; // 目标点数组(例如 5 个目标点)
|
||
public int coinCount; // 生成的金币数量
|
||
public float spreadRadius; // 金币生成时的随机扩散半径
|
||
public float moveDuration; // 金币移动到目标点的时间
|
||
public float delayBetweenCoins; // 每个金币延迟启动的时间间隔
|
||
public float randomTargetRadius; // 每个金币到达目标点时,周围随机偏移的半径
|
||
|
||
private List<GameObject> coins = new List<GameObject>();
|
||
|
||
private void Start()
|
||
{
|
||
// 初始化工作(如果有需要)
|
||
}
|
||
|
||
public void PlayCoinAnimation()
|
||
{
|
||
coins.Clear();
|
||
int completedCount = 0; // 记录已经完成动画的金币数量
|
||
|
||
// 确保目标点数组有至少 1 个元素
|
||
if (targetPoints.Length == 0)
|
||
{
|
||
Debug.LogError("没有设置目标点!");
|
||
return;
|
||
}
|
||
|
||
// 生成所有金币
|
||
for (int i = 0; i < coinCount; i++)
|
||
{
|
||
// 在起点附近生成金币
|
||
Vector3 randomOffset = new Vector3(
|
||
Random.Range(-spreadRadius, spreadRadius),
|
||
Random.Range(-spreadRadius, spreadRadius),
|
||
0);
|
||
GameObject coin = Instantiate(coinPrefab, startPoint.position + randomOffset, Quaternion.identity, Parent);
|
||
coins.Add(coin);
|
||
}
|
||
|
||
// 将金币分配到目标点,每个目标点分配相同数量的金币
|
||
int coinsPerTarget = coinCount / targetPoints.Length;
|
||
List<List<GameObject>> groups = new List<List<GameObject>>();
|
||
|
||
// 将金币分配到每个目标点
|
||
int coinIndex = 0;
|
||
for (int i = 0; i < targetPoints.Length; i++)
|
||
{
|
||
List<GameObject> group = new List<GameObject>();
|
||
for (int j = 0; j < coinsPerTarget; j++)
|
||
{
|
||
if (coinIndex < coinCount)
|
||
{
|
||
group.Add(coins[coinIndex]);
|
||
coinIndex++;
|
||
}
|
||
}
|
||
groups.Add(group);
|
||
}
|
||
|
||
// 对于每个目标点,逐个动画地将每组金币移动到目标点
|
||
for (int i = 0; i < targetPoints.Length; i++)
|
||
{
|
||
Transform targetPoint = targetPoints[i];
|
||
List<GameObject> group = groups[i];
|
||
|
||
// 为每组金币启动逐个动画
|
||
for (int j = 0; j < group.Count; j++)
|
||
{
|
||
GameObject coin = group[j];
|
||
|
||
// 计算延迟时间,稍微增加一点间隔
|
||
float delay = j * delayBetweenCoins; // 每个金币的延迟
|
||
|
||
// 在目标点附近生成一个随机位置
|
||
Vector3 randomTargetPosition = targetPoint.position + new Vector3(
|
||
Random.Range(-randomTargetRadius, randomTargetRadius),
|
||
Random.Range(-randomTargetRadius, randomTargetRadius),
|
||
0);
|
||
|
||
if (coin != null)
|
||
{
|
||
// 使用 DOTween 移动金币到随机的目标点附近
|
||
coin.transform.DOMove(randomTargetPosition, moveDuration)
|
||
.SetDelay(delay) // 只有延迟,但不需要等前一个金币完成
|
||
.SetEase(Ease.InOutQuad) // 设置缓动类型
|
||
.OnComplete(() =>
|
||
{
|
||
completedCount++;
|
||
Destroy(coin); // 动画完成后销毁金币
|
||
|
||
// 当所有金币都完成动画时,执行其他操作
|
||
if (completedCount == coinCount)
|
||
{
|
||
// 在所有金币完成后执行你需要的操作,比如:
|
||
// BettingBtn.instance.GiveMoney();
|
||
}
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|