Easy Fade In
From Unify Community Wiki
(Difference between revisions)
(→EasyFadeIn.cs) |
Isaiah Kelly (Talk | contribs) (improved code and formatting) |
||
Line 1: | Line 1: | ||
− | == | + | == 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>(); | ||
+ | } | ||
+ | |||
− | + | private void Update() | |
− | + | { | |
− | + | if (m_AudioSource.volume < 1) | |
− | + | { | |
− | + | m_AudioSource.volume = m_AudioSource.volume + (Time.deltaTime / (m_FadeInTime + 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); } } }