JukeboxController

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
(Description)
Line 6: Line 6:
 
==Description==
 
==Description==
  
''Description is missing''
+
This is a simple jukebox-style audio manager that lets you load multiple sound clips and have them played/mixed on the fly. The intent was to gradually grow this class into something that would easilly allow crossfades, ducking, etc.
  
 
== C# - JukeboxController.cs ==
 
== C# - JukeboxController.cs ==

Revision as of 17:42, 23 September 2006

Author: Unknown (69.174.143.210)

Description

This is a simple jukebox-style audio manager that lets you load multiple sound clips and have them played/mixed on the fly. The intent was to gradually grow this class into something that would easilly allow crossfades, ducking, etc.

C# - JukeboxController.cs

<csharp> using UnityEngine; using System.Collections;

/*

   Usage:
   JukeboxController jukebox = new JukeboxController();
   jukebox.AddClip("mysong", myclip);
   jukebox.PlayClip("mysong");
   jukebox.StopClip();
  • /

public class JukeboxController : MonoBehaviour {

   Hashtable jukebox;
   string current_clip;
   public void Awake()
   {
       jukebox = new Hashtable();
       current_clip = null;
   } // Awake()
   public void AddClip(string name, AudioClip clip)
   {
       if (jukebox == null)
       {
           jukebox = new Hashtable();
       } // if
       GameObject obj;
       obj = new GameObject();
       obj.AddComponent("AudioSource");
       obj.audio.clip = clip;
       obj.audio.ignoreListenerVolume = true;
       DontDestroyOnLoad(obj);
       jukebox.Add(name, obj);
   } // AddClip()
   /*
       Play a named audio clip.
       Does not restart the clip if it is played twice in a row.
       Will stop a previously playing clip to play this new clip.
   */
   public void PlayClip(string name)
   {
       if (name == current_clip)
       {
           return;
       } // if
       if (current_clip != null)
       {
           StopClip();
       } // if
       
       ((GameObject)jukebox[name]).audio.Play();
       current_clip = name;
   } // PlayClip()
   
   public void StopClip()
   {
       ((GameObject)jukebox[current_clip]).audio.Stop();
   } // StopClip()

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox