【问题标题】:Audio Fade In/Out with C# in Unity?在 Unity 中使用 C# 进行音频淡入/淡出?
【发布时间】:2021-07-14 18:45:53
【问题描述】:

我刚刚在 Unity 中为我的 android 平台游戏添加了一个音频管理器,该管理器基于此 video tutorial from Brackeys,它按预期工作。我想请人帮助我了解如何实现音频淡入/淡出代码(我认为这将是一个协程)所以当游戏开始时所有声音都会淡入以及当我在按钮上调用函数时(退出游戏 UI 按钮已创建),然后所有声音都会淡出。

我试图从我以前的 UI 管理器脚本中实现一个协程,但不幸的是,我在理解这个问题时遇到了问题。游戏的所有其他功能都按我的意愿工作。

音乐类.cs

using UnityEngine.Audio;
using UnityEngine;

[System.Serializable]
public class Classmusic
{
    public string name;

    public AudioClip clip;

    [Range(0f, 1f)]
    public float volume;
    [Range(0.1f, 3f)]
    public float pitch;

    public bool loop;

    [HideInInspector]
    public AudioSource source;
}

音乐播放器.cs

using System.Collections;
using UnityEngine;
using System;
using UnityEngine.Audio;


public class MPlayer : MonoBehaviour
{
    public Classmusic[] music;

    private void Awake()
    {
        foreach (Classmusic m in music)
        {
            m.source = gameObject.AddComponent<AudioSource>();
            m.source.clip = m.clip;
            m.source.volume = m.volume;
            m.source.pitch = m.pitch;
            m.source.loop = m.loop;
        }
    }

    private void Start()
    {
        Play("MainMusic");
    }

    public void Play (string name)
    {
        Classmusic m = Array.Find(music, sound => sound.name == name);
        if (m == null)
        {
            Debug.LogWarning("Music: " + name + " not found!");
            return;
        }
        m.source.Play();

    }
}

【问题讨论】:

    标签: c# unity3d audio


    【解决方案1】:

    在我的项目中,我使用了这个解决方案,它允许您为其选择衰落时间和插值函数(例如 Mathf.Lerp、Mathf.SmoothStep 等)。

    声音.cs

    using UnityEngine;
    
    [System.Serializable]
    public class Sound
    {
        public string name;
        public AudioClip clip;
    
        [Range(0f, 1f)]
        public float volume;
        [Range(.1f, 3f)]
        public float pitch;
    
        public bool loop;
    
        [HideInInspector]
        public AudioSource source;
    
    }
    

    AudioFade.cs

    using UnityEngine;
    using System.Collections;
    using System;
    
    public class AudioFade
    {
        public static IEnumerator FadeOut(Sound sound, float fadingTime, Func<float, float, float, float> Interpolate)
        {
            float startVolume = sound.source.volume;
            float frameCount = fadingTime / Time.deltaTime;
            float framesPassed = 0;
    
            while (framesPassed <= frameCount)
            {
                var t = framesPassed++ / frameCount;
                sound.source.volume = Interpolate(startVolume, 0, t);
                yield return null;
            }
    
            sound.source.volume = 0;
            sound.source.Pause();
        }
        public static IEnumerator FadeIn(Sound sound, float fadingTime, Func<float, float, float, float> Interpolate)
        {
            sound.source.Play();
            sound.source.volume = 0;
    
            float resultVolume = sound.volume;
            float frameCount = fadingTime / Time.deltaTime;
            float framesPassed = 0;
    
            while (framesPassed <= frameCount)
            {
                var t = framesPassed++ / frameCount;
                sound.source.volume = Interpolate(0, resultVolume, t);
                yield return null;
            }
    
            sound.source.volume = resultVolume;
        }
    }
    

    使用:

    StartCoroutine(AudioFade.FadeIn(mainThemeSound, 3f, Mathf.SmoothStep));
    

    【讨论】:

      【解决方案2】:

      这里的一个问题是您有两个卷,一个在 Classmusic 中,一个在 Classmusic 中包含的 AudioSource 源变量中。我猜播放功能集source.volume = 0;,但我不能肯定。你尝试过这样的事情吗?

      IEnumerator FadeIn(Classmusic sound) {
          sound.volume = 0;
          sound.source.volume = 0;
          float speed = 0.01f;   
      
          for (float i = 0; i < 1; i += speed)
          {
              sound.volume = i;
              sound.source.volume = i;
              yield return null;
          }
      }
      

      我知道同时设置这两个变量很烦人,但最好让它们保持同步。您还可以有一个 SetVolume() 函数来设置 AudioSource 音量和 Classmusic 类中的变量。

      【讨论】:

      • 感谢您的帮助。正如你之前提到的,我正在考虑一个 Couroutine。我正在努力让它工作,但我收到一条错误消息Failed to call function FadeIn of class MPlayer Calling function FadeIn with no parameters but the function requires 1.这是我没有得到的。我想在void Start() 中使用StartCoroutine("FadeIn"); 吗?
      • 你有两个启动协程的选项。您可以省略引号,直接写成这样:StartCoroutine(FadeIn(sound)); 或添加参数作为第二个参数:StartCoroutine("FadeIn", sound);。我相信第二个选项可能只允许 1 个参数,这在这种情况下很好。
      • 约翰,谢谢你的帮助,这正是我所需要的。另一方面,在我寻找音频管理器的过程中,我发现了这个很棒的 Git,并且淡入/淡出协程嵌入在一个带有其他好东西的函数中。一探究竟。这是链接。 Audio Manager Unity.
      【解决方案3】:

      这是我的 2d 平台游戏的解决方案。 当我的播放器靠近/远离其父对象时,我有一个声音淡入/淡出,我将其称为“敌人”。

      敌人的音源设置:

      Spatial Blend: 1
      Volume Rolloff: = Linear Rolloff
      Min distance = 1
      Max distance = 30
      

      将音频监听器放在播放器对象上,它应该可以工作。

      【讨论】:

        猜你喜欢
        • 2013-03-29
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多