如何在游戏对象设置为活动状态时播放音频文件?



我有一个问答游戏,每个类别有 5 个问题。我想在用户按正确答案或错误答案时添加音频。我怎样才能使当我按下错误或正确的答案时,音频文件会播放?

这是我在问题场景中运行的 levelcontrolscript2 的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelControlScript2 : MonoBehaviour 
{
// Get references to game objects that should be disabled and enabled
// at the start
GameObject[] toEnable, toDisable;
// References to game objects that should be enabled
// when correct or incorrect answer is given
private int triesLeft = 2;    // Set this to 1 (leave after second) or 
whatever when a level starts 
public GameObject correctSign, incorrectSign, incorrectSign2;
// Variable to contain current scene build index
int currentSceneIndex; 
// Use this for initialization
void Start () {
// Getting current scene build index
currentSceneIndex = SceneManager.GetActiveScene ().buildIndex;
// Finding game objects with tags "ToEnable" and "ToDisable"
toEnable = GameObject.FindGameObjectsWithTag ("ToEnable");
toDisable = GameObject.FindGameObjectsWithTag ("ToDisable");
// Disabling game objects with tag "ToEnable"
foreach (GameObject element in toEnable)
{
element.gameObject.SetActive (false);
}
}
// Method is invoked when correct answer is given
public void RightAnswer()
{
// Disabling game objects that are no longer needed
foreach (GameObject element in toDisable)
{
element.gameObject.SetActive (false);
}
// Turn on "correct" sign
correctSign.gameObject.SetActive (true);
// Invoke GotoMainMenu method in 1 second
Invoke ("LoadNextLevel", 1f);
}

// Method is invoked if incorrect answer is given
public void WrongAnswer()
{
// Disabling game objects that are no longer needed
foreach (GameObject element in toDisable)
{
element.gameObject.SetActive (false);
}
// Turn on "incorrect" sign
incorrectSign.SetActive (true);
// Disabling game objects that are no longer needed
foreach (GameObject element in toDisable)
{
element.gameObject.SetActive (true);
}
triesLeft--;
if(triesLeft <= 0)
{
// Disabling game objects that are no longer needed
foreach (GameObject element in toDisable)
{
element.gameObject.SetActive (false);
}
// Turn on "incorrect" sign
incorrectSign2.SetActive (true);
// Invoke GotoMainMenu method in 1 second
Invoke ("GotoCategories", 1f);
}
}

// Method loads next level depending on current scenes build index
void LoadNextLevel()
{
SceneManager.LoadScene (currentSceneIndex + 1);
}
// Method loads Category scene
void GotoCategories()
{
SceneManager.LoadScene ("Easy");
}
}   

为此,您需要使用AudioSource&AudioClips。只需将这些变量放在脚本中并在运行时分配值即可。

public class LevelControlScript2 : MonoBehaviour
{
// Get references to game objects that should be disabled and enabled
// at the start
GameObject[] toEnable, toDisable;
// References to game objects that should be enabled
// when correct or incorrect answer is given
private int triesLeft = 2;    // Set this to 1 (leave after second) or whatever when a level starts
public GameObject correctSign, incorrectSign, incorrectSign2;
public AudioSource _audioSource;
public AudioClip _incorrect;
public AudioClip _correct;
// Variable to contain current scene build index
int currentSceneIndex;
// Use this for initialization
void Start()
{
// Getting current scene build index
currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
// Finding game objects with tags "ToEnable" and "ToDisable"
toEnable = GameObject.FindGameObjectsWithTag("ToEnable");
toDisable = GameObject.FindGameObjectsWithTag("ToDisable");
// Disabling game objects with tag "ToEnable"
foreach (GameObject element in toEnable)
{
element.gameObject.SetActive(false);
}
}
// Method is invoked when correct answer is given
public void RightAnswer()
{
// Disabling game objects that are no longer needed
foreach (GameObject element in toDisable)
{
element.gameObject.SetActive(false);
}
// Turn on "correct" sign
correctSign.gameObject.SetActive(true);
_audioSource.clip = _correct;
_audioSource.Play();

// Invoke GotoMainMenu method in 1 second
Invoke("LoadNextLevel", 1f);
}

// Method is invoked if incorrect answer is given
public void WrongAnswer()
{
// Disabling game objects that are no longer needed
foreach (GameObject element in toDisable)
{
element.gameObject.SetActive(false);
}
// Turn on "incorrect" sign
incorrectSign.SetActive(true);
// Disabling game objects that are no longer needed
foreach (GameObject element in toDisable)
{
element.gameObject.SetActive(true);
}
triesLeft--;
if (triesLeft <= 0)
{
// Disabling game objects that are no longer needed
foreach (GameObject element in toDisable)
{
element.gameObject.SetActive(false);
}
// Turn on "incorrect" sign
incorrectSign2.SetActive(true);
_audioSource.clip = _incorrect;
_audioSource.Play();
// Invoke GotoMainMenu method in 1 second
Invoke("GotoCategories", 1f);
}
}

// Method loads next level depending on current scenes build index
void LoadNextLevel()
{
SceneManager.LoadScene(currentSceneIndex + 1);
}
// Method loads Category scene
void GotoCategories()
{
SceneManager.LoadScene("Easy");
}
}

因此,基本上将正确和不正确的音频剪辑分配给_incorrect_correct这些变量。然后,您需要将音频源附加到场景中的游戏对象,并将其分配给脚本。然后就说_audioSource.Play().这应该允许您播放所需的剪辑。更多详细信息位于 Unity 手册中有关AudioSourceAudioClip设置的内容。上述答案在您只是播放音频剪辑的情况下起作用。

我在您的问题标题中注意到的另一件事,但不是在正文中,是播放音频文件,然后将游戏对象设置为活动状态?如果您想在播放音频后激活某些游戏对象,那么这里也是解决方案:

source.Play(clip);
StartCoroutine(MyCoroutine(clipLength));

下面是协程片段:

private IEnumerator MyCoroutine(float clipLength)
{
yield return new WaitForSeconds(clipLength);
//activate your gameobject here
}

音频剪辑的回调已完成使用此引用

最新更新