【问题标题】:Unity audio bug with scripts带有脚本的 Unity 音频错误
【发布时间】:2018-10-21 00:58:03
【问题描述】:

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

我的audiomanager代码在这里

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(this);在菜单场景中为您的经理。
  • 对不起,没有描述清楚。但我刚刚修复了这个错误,谢谢大家

标签: c# unity3d audio scripting


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    void Awake()
    {
        if (instance != null)
        {
            if (instance != this)
            {
    
                Destroy(this.gameObject);
            }
        }
        else
        {
            instance = this;
            DontDestroyOnLoad(this.gameObject); // <-- Here
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多