FailAudio在游戏结束时一直循环播放,以下是GameManager的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public CanvasGroup winBackground;
public CanvasGroup failBackground;
public float duration = 1f;
public AudioSource winAudio;
public AudioSource failAudio;
public AudioSource backgroundAudio;
public Button restartButton;
public Button quitButton;
private bool isWin = false;
private bool isFail = false;
private float passTime = 0;
private void Start()
{
restartButton.gameObject.SetActive(false);
quitButton.gameObject.SetActive(false);
}
public void Win()
{
isWin = true;
}
public void Fail()
{
isFail = true;
}
public void Update()
{
if(isWin)
{
showBackgroud(winBackground,winAudio);
}
else if(isFail)
{
showBackgroud(failBackground,failAudio);
}
}
private void showBackgroud (CanvasGroup background, AudioSource audio)
{
passTime = passTime + Time.deltaTime;
background.alpha = passTime / duration;
if(!audio.isPlaying)
{
audio.Play();
}
restartButton.gameObject.SetActive(true);
quitButton.gameObject.SetActive(true);
}
}