gpt4 book ai didi

c# - 统一: Survival Shooter Tutorial: How to switch between two background songs

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:32 24 4
gpt4 key购买 nike

我正在为一项任务搞乱 Unity,我创建了一个“伤害链”,如果我造成伤害的速度足够快,伤害链就会增加。

然后,当伤害链足够高时,我想从背景音乐切换到 Metallica 的“Enter Sandman”...

但是当我将“Enter Sandman”MP3 添加到层次结构中的背景音乐对象时,我必须添加第二个具有相同名称(即音频源)的“音频源”。

然后,当我尝试在代码中管理它时,我不能只运行 GetComponent<AudioClip>就像我在其他任何地方一样。


如何在脚本中的两首歌曲之间切换?这是我的对象设置的图片,然后是代码...

enter image description here

还有,我的代码(原样),SongManager:

using UnityEngine;
using System.Collections;

public class SongManager : MonoBehaviour {
AudioSource regular_music;
AudioSource high_dmg_music;
// Use this for initialization
void Start () {
regular_music = GetComponent<AudioSource> ();
high_dmg_music = GetComponent<AudioSource> ();

regular_music.loop = true;
regular_music.Play ();

high_dmg_music.loop = false;
high_dmg_music.Stop ();
}

// Update is called once per frame
void Update () {
if (DamageManager.dmg_chain > 1500) {
regular_music.loop = false;
high_dmg_music.loop = true;
high_dmg_music.Play ();
} else {
if (high_dmg_music.isPlaying) {
high_dmg_music.loop = false;
high_dmg_music.Stop ();
regular_music.loop = true;
regular_music.Play ();
}
}

}
}

很明显,代码不行,但我还是写完了。

我将如何实现此配置?

最佳答案

只创建一个AudioSource。创建许多 AudioClips,然后将要播放的每个 AudioClip 重新分配给 AudioSource。将声音拖到 AudioClip 插槽中。

public AudioClip background_music;
public AudioClip sandman_music;

private AudioSource audioSource;

Void Start()
{
audioSource = GetComponent<AudioSource>();

//To play backround sound
if (audioSource.isPlaying)
{
audioSource.Stop();
}
audioSource.clip = background_music;
audioSource.Play();

//To play sandman sound
if (audioSource.isPlaying)
{
audioSource.Stop();
}
audioSource.clip = sandman_music;
audioSource.Play();
}

关于c# - 统一: Survival Shooter Tutorial: How to switch between two background songs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37378553/

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