C# 每次加载场景时,背景音乐都会变得更响亮

C# 每次加载场景时,背景音乐都会变得更响亮,c#,unity3d,C#,Unity3d,我在level01上有一个游戏对象,有音频源和下面的脚本。 当游戏开始时,脚本运行,音乐开始播放 我的问题是,每次我加载一个新的级别,声音都会变得更大。我不明白为什么会这样。有人能解释一下原因并给出解决方案,或者给我指出正确的方向吗 using UnityEngine; using System.Collections; public class MusicManagerScript : MonoBehaviour { public AudioClip[] songs; int

我在level01上有一个
游戏对象
,有
音频源
和下面的
脚本
。 当游戏开始时,脚本运行,音乐开始播放

我的问题是,每次我加载一个新的级别,声音都会变得更大。我不明白为什么会这样。有人能解释一下原因并给出解决方案,或者给我指出正确的方向吗

using UnityEngine;
using System.Collections;

public class MusicManagerScript : MonoBehaviour
{
    public AudioClip[] songs;
    int currentSong = 0;

    // Use this for initialization
    void Start() {
        DontDestroyOnLoad(gameObject);
    }

    // Update is called once per frame
    void Update() {
        if (audio.isPlaying == false) {
            currentSong = currentSong % songs.Length;
            audio.clip = songs[currentSong];
            audio.Play();
            currentSong++;
        }
    }
}

编辑:我看到你的答案是,你的相机只是更接近3D音频源,但我把我的答案放在这里,因为这是一个常见问题的常见解决方案

每次进入包含音乐管理器的场景时,都会实例化音乐管理器,但决不会破坏音乐管理器,因为它会复制声音。您需要的是一个单实例—一种告诉您的代码永远不允许多个实例的方式。试试这个:

public class MusicManagerScript : MonoBehaviour
{
    private static MusicManagerScript instance = null;

    public AudioClip[] songs;
    int currentSong = 0;

    void Awake()
    {
        if (instance != null)
        {
            Destroy(this);
            return;
        }
        instance = this;
    }

    // Use this for initialization
    void Start()
    {
        DontDestroyOnLoad(gameObject);
    }

    // Update is called once per frame
    void Update()
    {
        if (audio.isPlaying == false)
        {
            currentSong = currentSong % songs.Length;
            audio.clip = songs[currentSong];
            audio.Play();
            currentSong++;
        }
    }

    void OnDestroy()
    {
        //If you destroy the singleton elsewhere, reset the instance to null, 
        //but don't reset it every time you destroy any instance of MusicManagerScript 
        //because then the singleton pattern won't work (because the Singleton code in 
        //Awake destroys it too)
        if (instance == this)
        {
            instance = null;
        }
    }
}

因为实例是静态的,所以每个music manager脚本都可以访问它。如果已经设置了,它们在创建时会自我毁灭。

编辑:我看到你的答案是,你的相机只是更接近3D音频源,但我还是把我的答案放在这里,因为这是一个常见问题的常见解决方案

每次进入包含音乐管理器的场景时,都会实例化音乐管理器,但决不会破坏音乐管理器,因为它会复制声音。您需要的是一个单实例—一种告诉您的代码永远不允许多个实例的方式。试试这个:

public class MusicManagerScript : MonoBehaviour
{
    private static MusicManagerScript instance = null;

    public AudioClip[] songs;
    int currentSong = 0;

    void Awake()
    {
        if (instance != null)
        {
            Destroy(this);
            return;
        }
        instance = this;
    }

    // Use this for initialization
    void Start()
    {
        DontDestroyOnLoad(gameObject);
    }

    // Update is called once per frame
    void Update()
    {
        if (audio.isPlaying == false)
        {
            currentSong = currentSong % songs.Length;
            audio.clip = songs[currentSong];
            audio.Play();
            currentSong++;
        }
    }

    void OnDestroy()
    {
        //If you destroy the singleton elsewhere, reset the instance to null, 
        //but don't reset it every time you destroy any instance of MusicManagerScript 
        //because then the singleton pattern won't work (because the Singleton code in 
        //Awake destroys it too)
        if (instance == this)
        {
            instance = null;
        }
    }
}

因为实例是静态的,所以每个music manager脚本都可以访问它。如果已经设置了,它们在创建时会自我销毁。

加载场景时,是否实例化另一个附加了此脚本的对象?因为你没有破坏它,你可能会有多个音频源播放同一个剪辑。如果我允许它被破坏,音乐会在另一个级别加载时停止。我刚刚尝试过,声音对我来说不会变大。另一个猜测:试着把你的脚本(和音频源)放在主摄像机上(或者任何有AudioListener组件的东西)啊!我忘了关闭音频剪辑上的3D声音,游戏中的摄像头离声音管理器更近,所以声音更大。谢谢。加载场景时,您是否实例化另一个附加了此脚本的对象?因为你没有破坏它,你可能会有多个音频源播放同一个剪辑。如果我允许它被破坏,音乐会在另一个级别加载时停止。我刚刚尝试过,声音对我来说不会变大。另一个猜测:试着把你的脚本(和音频源)放在主摄像机上(或者任何有AudioListener组件的东西)啊!我忘了关闭音频剪辑上的3D声音,游戏中的摄像头离声音管理器更近,所以声音更大。谢谢