【问题标题】:How to fade out audio/music between scenes?如何淡出场景之间的音频/音乐?
【发布时间】:2015-06-26 09:35:44
【问题描述】:

我正在尝试让一些音乐在我开始我的安卓游戏时淡出。音乐在主菜单中播放,然后在播放器单击播放时淡出。我可以让音乐停止,但不会淡出。

我正在尝试使用这个来淡出:

using UnityEngine;
using System.Collections;

public class MusicScript : MonoBehaviour 
{
    static MusicScript instance;
    bool doneFading;

    void Start()
    {
        if(instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }

        else if (instance != this)
        {
            Destroy(gameObject);
        }
    }

    void Update()
    {
        if (Application.loadedLevelName == "Flappy Drone Gameplay Screen")
        {
            FadeMusic();
            if (doneFading == true)
            {
                Destroy(gameObject);
            }
        }
    }

    IEnumerator FadeMusic()
    {
        for (int i = 9; i > 0; i--)
        {
            Debug.Log("here");
            instance.audio.volume = i * 0.1f;
            yield return new WaitForSeconds(0.5f);
            Debug.Log("lowered volume");
        }
        doneFading = true;
    }

}

感谢任何帮助!

【问题讨论】:

    标签: android audio unity3d fade


    【解决方案1】:

    在玩了一些 Time.deltatime 之后,我找到了一种简单的淡入淡出方法:)

    适用于遇到相同问题的任何人的代码(此代码在特定场景中淡出音乐,但在所有其他场景中保持音乐播放):

    using UnityEngine;
    using System.Collections;
    
    public class MusicScript : MonoBehaviour 
    {
        static MusicScript instance;
        bool doneFading;
    
        float timer = 5f;
    
        void Start()
        {
            if(instance == null)
            {
                instance = this;
                DontDestroyOnLoad(gameObject);
            }
    
            else if (instance != this)
            {
                Destroy(gameObject);
            }
        }
    
        void Update()
        {
            if (Application.loadedLevelName == "Flappy Drone Gameplay Screen")
            {
                FadeMusic();
                if (doneFading == true)
                {
                    Destroy(gameObject);
                    Debug.Log ("Music destroyed.");
                }
            }
        }
    
        void FadeMusic()
        {
            if (timer > 0)
            {
                instance.audio.volume -= 0.015f;
                timer -= Time.deltaTime;
            }
            if (instance.audio.volume == 0)
            {
                doneFading = true;
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      相关资源
      最近更新 更多