Easy Fade In

From Unify Community Wiki
Jump to: navigation, search

Description

Attach this script to a GameObject with an AudioSource component and adjust fade time to your desired length. It will then smoothly increase the audio source volume over this period of time until it reaches maximum volume. Then it will destroy itself to prevent wasting update cycles.

EasyFadeIn.cs

// Original script by Desi Quintans (CowfaceGames.com)
 
using UnityEngine;
 
[RequireComponent(typeof(AudioSource))]
public class EasyFadeIn : MonoBehaviour
{
    [SerializeField]
    private int m_FadeInTime = 10;
    private AudioSource m_AudioSource;
 
 
    private void Awake()
    {
        m_AudioSource = GetComponent<AudioSource>();
    }
 
 
    private void Update()
    {
        if (m_AudioSource.volume < 1)
        {
            m_AudioSource.volume = m_AudioSource.volume + (Time.deltaTime / (m_FadeInTime + 1));
        }
        else
        {
            Destroy(this);
        }
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox