Easy Fade In

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(EasyFadeIn.cs)
(improved code and formatting)
 
Line 1: Line 1:
==EasyFadeIn.cs==
+
== 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 ===
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 +
// Original script by Desi Quintans (CowfaceGames.com)
 +
 
using UnityEngine;
 
using UnityEngine;
  
 
[RequireComponent(typeof(AudioSource))]
 
[RequireComponent(typeof(AudioSource))]
 +
public class EasyFadeIn : MonoBehaviour
 +
{
 +
    [SerializeField]
 +
    private int m_FadeInTime = 10;
 +
    private AudioSource m_AudioSource;
 +
 +
 +
    private void Awake()
 +
    {
 +
        m_AudioSource = GetComponent<AudioSource>();
 +
    }
 +
  
public class EasyFadeIn : MonoBehaviour {
+
    private void Update()
+
    {
/*
+
        if (m_AudioSource.volume < 1)
About
+
        {
Easy Fade In
+
            m_AudioSource.volume = m_AudioSource.volume + (Time.deltaTime / (m_FadeInTime + 1));
By Desi Quintans (CowfaceGames.com), 18 August 2012.
+
        }
+
        else
License
+
        {
Free as in speech, and free as in beer.
+
            Destroy(this);
+
        }
Usage
+
    }
Attach this script to a GameObject with an AudioSource and enter a fade time. Easy Fade In will
+
smoothly increase the audiosource's volume over this period of time until it reaches maximum
+
volume, and then will destroy itself to prevent wasting a FixedUpdate() check.
+
*/
+
+
public int approxSecondsToFade = 10;
+
+
void FixedUpdate ()
+
{
+
if (audio.volume < 1)
+
{
+
audio.volume = audio.volume + (Time.deltaTime / (approxSecondsToFade + 1));
+
}
+
else
+
{
+
Destroy (this);
+
}
+
}
+
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 19:36, 14 November 2018

[edit] 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.

[edit] 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