所以我有两个不同的场景。一种是 MainScene,带有 MainMenu Panel、LevelSelection Panel、Settings Panel 和 Shop Panel。另一个是Level1场景。因此,在 Level1 场景中,每个完成的关卡都有一个进度条和硬币奖励。在 MainScene 的 LevelSelection Panel 上,每个级别还有一个进度条和一个全局硬币计数器。如何从两个场景中连接进度条和硬币系统,以便当 Level1 完成时,在 LevelSelection 面板上添加 5 个硬币并且进度条也“进步”为 1?这是我的进度条脚本,我还没有制作硬币,但我对此也有一个想法:
进度条:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProgressBar : MonoBehaviour
{
[SerializeField]
private Image stars;
[SerializeField]
private Image stars_full;
private int level = 0;
private float currentAmount = 0;
private Coroutine routine;
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void OnEnable()
{
level = 0;
currentAmount = 0;
stars_full.fillAmount = currentAmount;
UpdateLevel(level);
}
public void UpdateProgress(float amount, float duration = 0.1f)
{
if (routine != null)
StopCoroutine(routine);
float target = currentAmount + amount;
routine = StartCoroutine(FillRoutine(target, duration));
}
public IEnumerator FillRoutine(float target, float duration)
{
float time = 0;
float tempAmount = currentAmount;
float diff = target - tempAmount;
currentAmount = target;
while (time < duration)
{
time += Time.deltaTime;
float percent = time / duration;
stars_full.fillAmount = tempAmount + diff * percent;
yield return null;
}
if (currentAmount >= 1)
LevelUp();
}
public void LevelUp()
{
UpdateLevel(level + 1);
UpdateProgress(-1f, 0.2f);
}
public void UpdateLevel(int level)
{
this.level = level;
}
}
在我的 Level1 Control 脚本中,我只调用 UpdateProgress() 方法并且它可以工作。当 Level2 加载 DontDestroyOnLoad() 时,我还能以某种方式停止进度条重置吗?