Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按下鼠标1时播放声音,未按下时停止声音_C#_Unity3d - Fatal编程技术网

C# 按下鼠标1时播放声音,未按下时停止声音

C# 按下鼠标1时播放声音,未按下时停止声音,c#,unity3d,C#,Unity3d,我在统一中编码,基本上我有一个带喷雾罐的角色。当我按住鼠标1键时,我想让我的“喷雾罐”发出声音 我在一个空白更新中尝试了它,然后我得到了多个相同的声音叠加在一起 void Update() { if (Input.GetButton("Fire1")) { FindObjectOfType<AudioManager>().Play("Spraycan"); } else if (Input.GetBu

我在统一中编码,基本上我有一个带喷雾罐的角色。当我按住鼠标1键时,我想让我的“喷雾罐”发出声音

我在一个空白更新中尝试了它,然后我得到了多个相同的声音叠加在一起

void Update()
   {
       if (Input.GetButton("Fire1"))
       {
           FindObjectOfType<AudioManager>().Play("Spraycan");
       }
       else if (Input.GetButtonUp("Fire1"))
       {
           //STOP SOUND HERE
       }
   }

void Update()
{
if(Input.GetButton(“Fire1”))
{
FindObjectOfType().Play(“Spraycan”);
}
else if(Input.GetButtonUp(“Fire1”))
{
//在这里停止声音
}
}
编辑:我的AudioManager完全脱离了Brackeys教程,如下所示:

using UnityEngine.Audio;
using System;
using UnityEngine;

public class AudioManager : MonoBehaviour
{
    public Sound[] sounds;

    public static AudioManager instance;


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

        DontDestroyOnLoad(gameObject);

        foreach (Sound s in sounds)
        {
            s.source = gameObject.AddComponent<AudioSource>();
            s.source.clip = s.clip;


            s.source.volume = s.volume;
            s.source.pitch = s.pitch;
            s.source.loop = s.loop;
        }
    }
    void Start()
    {
        Play("Theme");
    }


    public void Play (string name)
    {
        Sound s = Array.Find(sounds, sound => sound.name == name);
        if (s == null)
        {
            Debug.LogWarning("Sound: " + name + "not found!");
            return;
        }

        s.source.Play();
    }
}
if (Input.GetButtonDown("fire1")
    AudioManager.Play

if (Input.GetButtonUp("fire1")
    AudioManager.Stop
使用UnityEngine.Audio;
使用制度;
使用UnityEngine;
公共类音频管理器:MonoBehavior
{
公共声音;
公共静态AudioManager实例;
无效唤醒()
{
if(实例==null)
实例=此;
其他的
{
摧毁(游戏对象);
返回;
}
DontDestroyOnLoad(游戏对象);
foreach(声音中的声音s)
{
s、 source=gameObject.AddComponent();
s、 source.clip=s.clip;
s、 source.volume=s.volume;
s、 source.pitch=s.pitch;
s、 source.loop=s.loop;
}
}
void Start()
{
戏剧(“主题”);
}
公共无效播放(字符串名称)
{
Sound s=Array.Find(sounds,Sound=>Sound.name==name);
如果(s==null)
{
LogWarning(“声音:+name+“找不到!”);
返回;
}
s、 source.Play();
}
}

更新功能几乎会连续启动,就像每一帧或其他东西一样(我不记得它的间隔) 按下按钮时,必须使用特定于该按钮的事件。 然后使用标志指示当前正在播放声音,以避免再次播放。 播放结束后,重新开始播放。 在用户释放按钮之前,您将标志发送到false并停止声音。

类似于以下内容:

using UnityEngine.Audio;
using System;
using UnityEngine;

public class AudioManager : MonoBehaviour
{
    public Sound[] sounds;

    public static AudioManager instance;


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

        DontDestroyOnLoad(gameObject);

        foreach (Sound s in sounds)
        {
            s.source = gameObject.AddComponent<AudioSource>();
            s.source.clip = s.clip;


            s.source.volume = s.volume;
            s.source.pitch = s.pitch;
            s.source.loop = s.loop;
        }
    }
    void Start()
    {
        Play("Theme");
    }


    public void Play (string name)
    {
        Sound s = Array.Find(sounds, sound => sound.name == name);
        if (s == null)
        {
            Debug.LogWarning("Sound: " + name + "not found!");
            return;
        }

        s.source.Play();
    }
}
if (Input.GetButtonDown("fire1")
    AudioManager.Play

if (Input.GetButtonUp("fire1")
    AudioManager.Stop
该AudioManager必须具有Stop()方法。调用AudioSource的.Stop()方法。如果没有,就创建它。

Sam

编程的一半关键是学习如何找到东西

如果你看一下unity关于audiosource的手册

您将看到brackeys“play”的来源。。看看还有什么。。你看到了什么,听起来很像它阻止了源代码的播放


看看你能不能自己做一个停止功能

首先,您应该在
AudioManager
类中定义一个stop方法。类似于此(未经测试):

然后按如下方式实施:

void Update()
{
    //Get Button will fire this continuously as you keep it pressed. 
    //Consider using GetButtonDown, or implement delayed repetition.
    if (Input.GetButton("Fire1"))
    {
        FindObjectOfType<AudioManager>().Play("Spraycan");
    }
    else if (Input.GetButtonUp("Fire1"))
    {
        FindObjectOfType<AudioManager>().Stop("Spraycan");
    }
}
void Update()
{
//“获取”按钮将在您按住它的同时持续触发此按钮。
//考虑使用GetButtonDown,或者实现延迟重复。
if(Input.GetButton(“Fire1”))
{
FindObjectOfType().Play(“Spraycan”);
}
else if(Input.GetButtonUp(“Fire1”))
{
FindObjectOfType().Stop(“Spraycan”);
}
}
旁注:代码中有一些东西不利于效率:

  • 如果您使用
    字典
    而不是
    数组
    来查找声音,您的查找速度会快得多

  • FindObjectOfType()
    分配给
    Start()
    处的变量,并避免在用户按下鼠标按钮时不断查找该变量

我不想谈太多细节,因为这与问题无关


旁注2:我完全同意BugFinder的答案。要成为一名更好的程序员,您应该慢慢开始学习如何浏览API文档并找到您想要的内容。

您尝试了什么?除了发出if语句外,你还尝试过什么来阻止它呢?我对C完全是新手,所以我尝试过的唯一代替“//stop SOUND HERE”的方法是“return;”。这显然不起作用。没有返回只是退出该函数。请查看我的编辑!我确信我需要更改屏幕上的空白,但我真的不知道如何。。C#Unity对menope来说是全新的,您的更新很好。。您只需根据上面的提示为音频管理器添加一点内容,谢谢您的支持。我实现了所有功能,没有出现任何错误,但声音不会消失。为了澄清,我也100%同意在发布之前找到解决方案。我刚刚开始做这件事,我已经在我的项目上取得了长足的进步,即使之前没有任何编码经验。这让我被困了一整天,如果没有这个问题,我就不能继续下去了。它能工作吗!我犯了一个小错误。非常感谢你!