为了账号安全,请及时绑定邮箱和手机立即绑定

带有脚本的 Unity 音频错误

带有脚本的 Unity 音频错误

C#
四季花海 2021-06-02 18:15:25
我正在 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");    }
查看完整描述

2 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

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


查看完整回答
反对 回复 2021-06-05
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

您需要指定DontDestroyOnLoad将影响gameObject本身,而不仅仅是MonoBehaviour.


void Awake()

{

    if (instance != null)

    {

        if (instance != this)

        {


            Destroy(this.gameObject);

        }

    }

    else

    {

        instance = this;

        DontDestroyOnLoad(this.gameObject); // <-- Here

    }

}


查看完整回答
反对 回复 2021-06-05
  • 2 回答
  • 0 关注
  • 176 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信