Giter Club home page Giter Club logo

fadeaudio's Introduction

FadeAudio

Description

This mechanic shows how to fade in and out background audio at the beginning or end of the audio.

Implementation

The code itself is straightforward:

using UnityEngine;

namespace Audio
{
    public class FadeAudio : MonoBehaviour
    {
        public bool fadeOnAudioEnd = true;
        public bool fadeOnAudioStart = true;
        public float fadeTime;

        public float minVolume = 0;
        public float maxVolume = 1;

        public AudioSource source;

        public void Play()
        {
            source.volume = minVolume;
            source.Play();
        }

        private void Update()
        {
            if (source == null || source.clip == null || source.clip.length == 0)
            {
                return;
            }

            // Fade at the beginning
            if (fadeOnAudioStart && source.time < fadeTime)
            {
                // Smoothly increase volume
                source.volume = Mathf.Lerp(minVolume, maxVolume, source.time / fadeTime);
            }
            // Fade at the end
            else if (fadeOnAudioEnd && source.clip.length - source.time < fadeTime)
            {
                // Smoothly decrease volume
                source.volume = Mathf.Lerp(minVolume, maxVolume, (source.clip.length - source.time) / fadeTime);
            }
            else
            {
                // If no fade is needed, keep volume at max
                source.volume = maxVolume;
            }
        }
    }
}

To set it up:

  • Ensure there is an AudioListener component on your main camera.
  • Add an AudioSource component and assign an audio clip.
  • Optionally, click the Loop option to on if you want the audio to loop.
  • Add a FadeAudio component and assign the source property to the audio source you just created.
  • Change the Fade Time property to however long you want the audio to fade in/out for.

Example Project

It can be viewed here

There are no enemies or controls. The text displays the amount into the audio clip the source currently is, and the volume of the source. The slider lets the user seek to any part of the audio, so that the fade in and out can more easily be heard.

Challenges

This wasn't particularly challenging, especially because I've implemented similar systems in other games. It took about an hour, although I spent another hour figuring out what to do for this assignment.

Credits

The audio was pulled from a previous project and written by David Zeng, although it is licensed under MIT, so this is technically unecessary to include.

fadeaudio's People

Contributors

loukylor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.