Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Last active December 17, 2015 22:29
Show Gist options
  • Save jakevsrobots/5682325 to your computer and use it in GitHub Desktop.
Save jakevsrobots/5682325 to your computer and use it in GitHub Desktop.
/*
Sounds can be played with AudioManager.PlayClip() or with an AudioSource in the scene.
If they're played with an AudioSource, add a LocalSound component to the object so the
sounds can be controlled with volume settings in the pause menu. LocalSound also lets
you assign a tag to the sound to fade it from code (see below).
*/
using UnityEngine;
using System.Collections;
public class AudioSample : MonoBehaviour
{
public AudioClip theSoundClip;
private PlayingSound thePlayer;
void Start()
{
// Params are: AudioClip clip, bool loop, float baseVolume
thePlayer = AudioManager.PlayClip(theSoundClip, true, 0.5f);
// Change the pitch:
thePlayer.Pitch = 0.5f;
// Fade a single sound:
// from baseVolume to 0 in 5 seconds
StartCoroutine(thePlayer.FadeOut(5));
// from 0 to baseVolume in 5 seconds
StartCoroutine(thePlayer.FadeIn(5));
// from current volume to 0.3 in 5 seconds
StartCoroutine(thePlayer.FadeTo(5, 0.3f));
// Fade a group of sounds (these static functions wrap the inner coroutines, so you just call them as normal functions)
// NOTE: don't actually fade tags within Start(), as the tags may not be registered with the AudioManager until the first frame.
// if you want to call a fade at scene start, wrap it in TimeUtils.DelayOneFrame
thePlayer.tags.Add("weird");
AudioManager.FadeInTag("weird", 5);
AudioManager.FadeOutTag("weird", 5);
AudioManager.FadeTagToTarget("weird", 5, 0.3f); // 0.3f in 5 seconds
AudioManager.SetTagVolume("weird", 0.3f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment