Chi*_*001 3 c# asynchronous unity-game-engine
我正在预加载一个新场景SceneManager.LoadSceneAsync
来创建动画退出效果,但这给了我错误:
场景中有 2 个音频听众。请确保场景中始终只有一个音频侦听器。
如何确保场景中只有一个音频侦听器?
// Public function to change Scene
public void GoToScene(string goToScene)
{
// Starts exit animation and changes scene
CanvasAnimation.SetBool("hide", true);
StartCoroutine(ChangeScene(ExitTime, goToScene));
}
IEnumerator ChangeScene(float time, string goToScene)
{
//Set the current Scene to be able to unload it later
Scene currentScene = SceneManager.GetActiveScene();
// The Application loads the Scene in the background at the same time as the current Scene.
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(goToScene, LoadSceneMode.Additive);
asyncLoad.allowSceneActivation = false;
yield return new WaitForSeconds(time);
asyncLoad.allowSceneActivation = true;
//Wait until the last operation fully loads to return anything
while (!asyncLoad.isDone)
{
yield return null;
}
//Move the GameObject (you attach this in the Inspector) to the newly loaded Scene
SceneManager.MoveGameObjectToScene(ObjToSave, SceneManager.GetSceneByName(goToScene));
//Unload the previous Scene
SceneManager.UnloadSceneAsync(currentScene);
}
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助
AudioListener
您可以通过检查所有相机来确保您拥有一台。它们通常会自动连接到新创建的相机。检查每个摄像头并将其移除。您只需将一个AudioListener
连接到主相机即可。
您还可以通过代码执行此操作:
查找AudioListener
场景中的所有实例,FindObjectsOfType
然后将其删除(如果它们未连接到 MainCamera)。您可以AudioListener
通过检查其名称来确定是否已连接到主相机,tag
默认情况下应为“MainCamera”。
AudioListener[] aL = FindObjectsOfType<AudioListener>();
for (int i = 0; i < aL.Length; i++)
{
//Destroy if AudioListener is not on the MainCamera
if (!aL[i].CompareTag("MainCamera"))
{
DestroyImmediate(aL[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
有时,您可能有多个带有“MainCamera”标签的相机。如果是这种情况,请保留第一个AudioListener
返回的值FindObjectsOfType
,但将其销毁AudioListener
在数组中。您留下一个是因为需要在场景中播放声音。
AudioListener[] aL = FindObjectsOfType<AudioListener>();
for (int i = 0; i < aL.Length; i++)
{
//Ignore the first AudioListener in the array
if (i == 0)
continue;
//Destroy
DestroyImmediate(aL[i]);
}
Run Code Online (Sandbox Code Playgroud)
请注意,该Destroy
功能也应该没问题。我选择DestroyImmediate
立即删除它,而不是在另一个框架中删除它。
归档时间: |
|
查看次数: |
6173 次 |
最近记录: |