gpt4 book ai didi

c# - Unity C#如何使倒计时滴答声响起?

转载 作者:行者123 更新时间:2023-12-03 01:09:21 25 4
gpt4 key购买 nike

我正在制作塔防游戏。我想发出滴答滴答的倒计时声音,但是当我在代码中调用“播放声音”功能时,它只会无限循环。我也用了协程,但没有用。请帮我。
=================================================== ================
波浪 Spanner

public float timeBetweenWaves = 34f;
private float countdown = 34f;

void Update()
{
if (waveNumber <= 40)
{
SpawnCount();
}
}
void SpawnCount()
{
GameObject[] enemyFind = GameObject.FindGameObjectsWithTag("Enemy");
int enemyCount = enemyFind.Length;
if (enemyCount == 0) // if enemy is 0, countdown goes 34 to 0
{
if (countdown <= 0f) // if countdown becomes 0, wave starts and countdown stops at 34
{
StartCoroutine(SpawnWave()); // wave starts here
countdown = timeBetweenWaves; // countdown initializes
}

countdown -= Time.deltaTime; // countdown goes 34 to 0
waveCountDownText.text = Mathf.Round(countdown).ToString();
// I want to play countdown tick-tock sound here until countdown becomes 0
//FindObjectOfType<SoundManager>().Play("Timer"); // this didn't work
}
}
声音管理员
public Sound[] sounds;
public static SoundManager instance;
void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
void Start()
{
Play("Theme");
}
public void Play (string name) // i used this function and sound name is "Timer"
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name + "Not Found!");
return;
}
s.source.Play();
}

最佳答案

尝试使用AudioSource

public AudioSource[] audioSource;
public AudioClip[] clips;

audioSource [source].clip = clips [0];
audioSource[source].Play ();
请也阅读此内容,这可能对 Unity: Difference between Audio Source, Audio Listener and Audio Clip有帮助

关于c# - Unity C#如何使倒计时滴答声响起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64060424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com