gpt4 book ai didi

c# - 在Unity3D中播放另一个音频时停止音频

转载 作者:行者123 更新时间:2023-12-03 00:08:28 25 4
gpt4 key购买 nike

播放另一音频时如何停止音频?

public AudioSource audioSource;
public AudioClip audioClip;
private string[,] levelWords;
//public static string glupiaZmienna;
public Text txt;

public void OnPointerClick(PointerEventData eventData)
{
Debug.Log(gameObject.name);
WordsLoader w = new WordsLoader();
levelWords = w.wLevelWords;
SetCategoryButton scb = new SetCategoryButton();
int a = scb.Kategoria;
zupa();

AudioSource audio = gameObject.AddComponent<AudioSource>();
audio.Stop();
audio.clip = (AudioClip)Resources.Load(a+ "/" + WordsLoader.language + "/Sounds/" + gameObject.name);

audio.Play();


// txt = gameObject.GetComponent<Text>();
}

public void zupa()
{ WordsLoader w = new WordsLoader();
SetCategoryButton scb = new SetCategoryButton();
int a = scb.Kategoria;
print(w.wHowManyWords);

for (int i = 0; i < w.wHowManyWords; i++)
{
print("jestem tu");
if (levelWords[i, 0] == gameObject.name)
{
txt = GameObject.Find("Text").GetComponent<Text>(); ;
txt.text = levelWords[i, 2];
}

}
}

此代码位于脚本中,单击按钮后便开始播放声音。我想停止较早开始的音频。

最佳答案

编辑:答案必须更改,因为其下方留有注释。覆盖所有内容是有意义的。

您需要一个指向所有AudioSource的引用。将每个ClickAction实例存储在另一个脚本(List)中的PlayerManager中。在播放音频之前,请遍历该List并停止播放(如果正在播放)。之后,您就可以播放音频了。

另外,您当前每次想播放音频时都调用Resources.Load。就性能而言,这是不好的。您应该在Start()函数中执行一次,然后在需要时播放。另外,您的gameObject.AddComponent<AudioSource>()代码也应该只调用一次。

您的新ClickAction脚本:

public class ClickAction : MonoBehaviour
{
public AudioSource audioSource;
public AudioClip audioClip;
private string[,] levelWords;
//public static string glupiaZmienna;
public Text txt;

public AudioSource clickAudioSource;
PlayerManager playerManager = null;

void Start()
{
//Get PlayerManager from PlayerManager GameObject
playerManager = GameObject.Find("PlayerManager").GetComponent<PlayerManager>();
clickAudioSource = gameObject.AddComponent<AudioSource>();
clickAudioSource.clip = (AudioClip)Resources.Load(a + "/" + WordsLoader.language + "/Sounds/" + gameObject.name);

//Register this Script to the PlayerManager
playerManager.addClickAction(this);
}

public void OnPointerClick(PointerEventData eventData)
{
Debug.Log(gameObject.name);
WordsLoader w = new WordsLoader();
levelWords = w.wLevelWords;
SetCategoryButton scb = new SetCategoryButton();
int a = scb.Kategoria;
zupa();

//Play Audio
playAudio();

// txt = gameObject.GetComponent<Text>();
}

private void playAudio()
{
playerManager.playAudio(clickAudioSource);
}

public void stopAudio()
{
//If not null and playing then stop audio
if (clickAudioSource != null && clickAudioSource.isPlaying)
{
clickAudioSource.Stop();
}
}

public void zupa()
{
WordsLoader w = new WordsLoader();
SetCategoryButton scb = new SetCategoryButton();
int a = scb.Kategoria;
print(w.wHowManyWords);

for (int i = 0; i < w.wHowManyWords; i++)
{
print("jestem tu");
if (levelWords[i, 0] == gameObject.name)
{
txt = GameObject.Find("Text").GetComponent<Text>(); ;
txt.text = levelWords[i, 2];
}

}
}

void OnDisable()
{
//Un-Register this Script from the PlayerManager when it is destroyed
playerManager.removeClickAction(this);
}
}
PlayerManager脚本。创建一个名为 PlayerManager的GameObject,然后将以下脚本附加到它:
public class PlayerManager : MonoBehaviour
{
List<ClickAction> clickActions = new List<ClickAction>();

public void addClickAction(ClickAction clickActionToAdd)
{
//Adds clickActionToAdd if it does not exist in the List already
if (!clickActions.Contains(clickActionToAdd))
{
clickActions.Add(clickActionToAdd);
}
}

public void removeClickAction(ClickAction clickActionToAdd)
{
//Removes clickActionToAdd if it exist in the List
if (clickActions.Contains(clickActionToAdd))
{
clickActions.Add(clickActionToAdd);
}
}

public bool clickActionExist(ClickAction clickActionToAdd)
{
//Removes clickActionToAdd if it exist in the List
return clickActions.Contains(clickActionToAdd);
}

public void playAudio(AudioSource audSource)
{
//Stop Other Audio
stopAllClickActionAudio();

//Now, play the one that was passed in
audSource.Play();
}

void stopAllClickActionAudio()
{
//Stop Audio on every ClickAction script
for (int i = 0; i < clickActions.Count; i++)
{
clickActions[i].stopAudio();
}
}
}

调用 playAudio函数时,它将停止 AudioSource中存储的 List中的所有音频,然后它将播放传递到其中的当前 AudioSource

关于c# - 在Unity3D中播放另一个音频时停止音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40598305/

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