【问题标题】:Sound not playing in project build声音没有在项目构建中播放
【发布时间】:2018-11-15 18:36:03
【问题描述】:

我创建了一些链接到画布的代码,代码本身管理两个按钮,这些按钮在单击时相互切换,允许播放器打开和关闭音乐。当我进行构建时,声音总是关闭并且无法弄清楚原因(按钮功能正常)。还要声明我使用 Playerprefs 来保存设置,我的所有场景中都使用了相同的代码。我只想强制启动音频并将我的按钮设置为在第一个场景中处于活动状态。(开始场景)

  public AudioSource backgroundmusic;
public string sound;

public GameObject SoundOnButton, SoundOffButton;
public bool muted;



void Start ()
{

    backgroundmusic.Play();
    SoundCheck();

   if (sound == "enabled")
    {
        SoundOffButton.SetActive(false);
        SoundOnButton.SetActive(true);
    }

   if (sound == "muted")
    {

        SoundOffButton.SetActive(true);
        SoundOnButton.SetActive(false);
    }

}

private void Update()
{

}


public void SoundOFF()
{
    SoundOffButton.SetActive(false);
    SoundOnButton.SetActive(true);
    muted = false;

    PlayerPrefs.SetString("Sound", "enabled");

    SoundCheck();

}

public void SoundON()
{

    SoundOffButton.SetActive(true);
    SoundOnButton.SetActive(false);
    muted = true;

    PlayerPrefs.SetString("Button", "used");
    PlayerPrefs.SetString("Sound", "muted");

    SoundCheck();


}

public void SoundCheck()
{
    sound = PlayerPrefs.GetString("Sound");

    if (sound == "enabled")
    {

        muted = false;
        backgroundmusic.Stop();
    }
    if (sound == "muted")
    {

        muted = true;
        backgroundmusic.Play();

    }

}

【问题讨论】:

  • 乍一看,我在你的代码中看到了一个奇怪的东西: if (sound == "enabled"){ backgroundmusic.Stop(); } 对吗?
  • 我正在测试各种东西以使音频正常工作,所以现在我真的不确定

标签: c# unity3d audio


【解决方案1】:

我的第一个问题是:它适用于您的编辑器吗?我不清楚。

我想指出一些事情: 确保您已将 AudioClip 附加到您的音频源。确保音量是您可以实际听到的某个值。确保 GameObject 或包含 AudioSource 的 GameObject 的父级在任何时候都没有被禁用。确保您的项目实际上被允许发出声音(Windows 或 Mac 混音器)。

如果这些都不起作用,则不会。我会开始调试。

首先禁用您用来启动和暂停音乐的脚本,然后转到您的 AudioSource 并选择“Play on Awake”,这将使您的背景音乐随着游戏开始,如果不是,您现在就可以有不同的问题。

如果它确实与游戏一起玩,那么制作一个简单的脚本。

IEnumerator Start ()
{
    bool control;
    while (true)
    {
        control = !control;
        yield return new WaitForSeconds (1f);
        if (control)
            backgroundmusic.Play ();
        else
            backgroundmusic.Stop ();
    }
}

除了Play()和Stop(),你还可以使用:

GetComponent <AudioSource> ().volume = 0; and GetComponent <AudioSource> ().volume = 1;

尝试任何这些解决方案并告诉我。

【讨论】:

  • 我尝试了你提到的方法,但没有按照我需要的方式工作。
【解决方案2】:

我找到了我正在寻找的答案,我在这里和那里改变了一些东西,现在它可以工作了。这是代码。

   public AudioSource backgroundmusic;
public string sound;

public GameObject SoundOnButton, SoundOffButton, Canvas;
public bool muted, notloaded = false;



void Start()
{


    SoundCheck();

    if (notloaded == false)
    {
        if (Canvas == true)
        {
            SoundON();
            notloaded = true;
        }
    }

}



public void SoundOFF()
{

    muted = false;

    PlayerPrefs.SetString("Sound", "enabled");

    SoundCheck();

}

public void SoundON()
{


    muted = true;

    PlayerPrefs.SetString("Sound", "muted");

    SoundCheck();


}

public void SoundCheck()
{
    sound = PlayerPrefs.GetString("Sound");

    if (sound == "enabled")
    {

        muted = false;
        SoundOffButton.SetActive(false);
        SoundOnButton.SetActive(true);
        backgroundmusic.Pause();
        Debug.Log("Enabled");
    }
    if (sound == "muted")
    {

        muted = true;
        SoundOffButton.SetActive(true);
        SoundOnButton.SetActive(false);
        backgroundmusic.Play();
        Debug.Log("Muted");

    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多