c# - 带有脚本的Unity音频错误

标签 c# unity3d audio scripting

我正在像5.3这样的真正旧版本上统一构建游戏。我有一个主场景和一个菜单场景,每个场景都有一个音频管理器脚本来控制音频。我的问题是这个函数称为DontDestroyOnLoad。更具体地说,问题是当我构建游戏时,游戏运行完美,菜单也正常运行,但菜单发出声音,而游戏没有声音。游戏中只有一些声音不在音频管理器中,因此我将它们手动放置。

我的音频管理器代码在这里

using UnityEngine;

[System.Serializable]
public class Sound
{

    public string name;
    public AudioClip clip;

    [Range(0f, 1f)]
    public float volume = 0.7f;
    [Range(0.5f, 1.5f)]
    public float pitch = 1f;

    [Range(0f, 0.5f)]
    public float randomVolume = 0.1f;
    [Range(0f, 0.5f)]
    public float randomPitch = 0.1f;

    public bool loop = false;

    private AudioSource source;

    public void SetSource(AudioSource _source)
    {
        source = _source;
        source.clip = clip;
        source.loop = loop;
    }

    public void Play()
    {
        source.volume = volume * (1 + Random.Range(-randomVolume / 2f, randomVolume / 2f));
        source.pitch = pitch * (1 + Random.Range(-randomPitch / 2f, randomPitch / 2f));
        source.Play();
    }

    public void Stop()
    {
        source.Stop();
    }

}

public class AudioManager : MonoBehaviour
{

    public static AudioManager instance;

    [SerializeField]
    Sound[] sounds;

    void Awake()
    {
        if (instance != null)
        {
            if (instance != this)
            {

                Destroy(this.gameObject);
            }
        }
        else
        {
            instance = this;
            DontDestroyOnLoad(this);
        }
    }

    void Start()
    {
        for (int i = 0; i < sounds.Length; i++)
        {
            GameObject _go = new GameObject("Sound_" + i + "_" + sounds[i].name);
            _go.transform.SetParent(this.transform);
            sounds[i].SetSource(_go.AddComponent<AudioSource>());
        }

        PlaySound("Music");

    }

    public void PlaySound(string _name)
    {
        for (int i = 0; i < sounds.Length; i++)
        {
            if (sounds[i].name == _name)
            {
                sounds[i].Play();
                return;
            }
        }

        // no sound with _name
        Debug.LogWarning("AudioManager: Sound not found in list, " + _name);
    }

    public void StopSound(string _name)
    {
        for (int i = 0; i < sounds.Length; i++)
        {
            if (sounds[i].name == _name)
            {
                sounds[i].Stop();
                return;
            }
        }

        // no sound with _name
        Debug.LogWarning("AudioManager: Sound not found in list, " + _name);
    }

}

最佳答案

也许您可能需要使用DontDestroyOnLoad(gameobject);
不用将此作为参数。

关于c# - 带有脚本的Unity音频错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50278250/

相关文章:

c# - 将文本框值发送到 SQL 数据库

unity3d - 以相反顺序显示行的文本组件

audio - 确定声音流的方向性声音

python - Python 中的 FFT 及其解释

c# - 为 SqlCipher 数据库存储加密 key 的正确方法

c# - 使用 Linq 查询和 SQLite 插入多个表(一对一关系)

c# 泛型变量声明,这可能吗?

python - python 3.5 的 Tensorflow 1.7.1 错误

c# - Unity - 通过抵消重力创建气垫车

audio - 音乐 discord.js 机器人 : The "url" argument must be of type string