【发布时间】:2019-05-13 13:00:51
【问题描述】:
基本上,我主要是从教程中复制代码,但是在播放另一个声音后我遇到了声音取消的问题。例如,当我射击并播放声音时,如果我击中敌人并且爆炸声播放时,射击声音会被静音。 我已经在空的游戏对象声音管理器上有这个脚本,并通过 soundmanager.instance.playSingle(sound) 访问它。 我不知道还要做什么才能使声音不会相互抵消! 任何帮助表示赞赏
void Awake()
{
//Check if there is already an instance of SoundManager
if (instance == null)
//if not, set it to this.
instance = this;
//If instance already exists:
else if (instance != this)
//Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
Destroy(gameObject);
//Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.
DontDestroyOnLoad(gameObject);
}
//Used to play single sound clips.
public void PlaySingle(AudioClip clip)
{
//Set the clip of our efxSource audio source to the clip passed in as a parameter.
efxSource.clip = clip;
//Play the clip.
efxSource.PlayOneShot(efxSource.clip);
}
public void PlayCrash(AudioClip clip)
{
//Set the clip of our efxSource audio source to the clip passed in as a parameter.
crashSource.clip = clip;
//Play the clip.
crashSource.PlayOneShot(crashSource.clip);
}
public void PlayShoot(AudioClip clip)
{
//Set the clip of our efxSource audio source to the clip passed in as a parameter.
ShootSource.clip = clip;
//Play the clip.
// ShootSource.PlayOneShot();
ShootSource.PlayOneShot(ShootSource.clip);
}
//RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch.
public void RandomizeSfx(params AudioClip[] clips)
{
//Generate a random number between 0 and the length of our array of clips passed in.
int randomIndex = Random.Range(0, clips.Length);
//Choose a random pitch to play back our clip at between our high and low pitch ranges.
float randomPitch = Random.Range(lowPitchRange, highPitchRange);
//Set the pitch of the audio source to the randomly chosen pitch.
efxSource.pitch = randomPitch;
//Set the clip to the clip at our randomly chosen index.
efxSource.clip = clips[randomIndex];
//Play the clip.
efxSource.Play();
}
}
【问题讨论】:
-
不想被拖得太远,为什么不为第二个声音添加另一个音频源。