Search Unity

Groove.JS (a music script for your Games) ;)

Discussion in 'Made With Unity' started by hai_ok, May 1, 2010.

  1. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    (from the script)
    Groove v1.1 © "hai_ok". Feel free to use for anything.
    If you use this, please let me know. Its good to know if I helped someone. :D

    INSTRUCTIONS:
    Use this script to dynamically change the music in a game, in the middle of any "song".
    It only works with loops that you might use to make music in music creation software like Garageband or FL Studio (Techno EJ...).
    This only assumes you want to use bass and drum tracks.
    I did this so that I could use more prominant lead loops to "celebrate" getting collectibles or acheiving goals.

    You can Add AS MANY LOOPS AS YOU NEED from within the Unity Inspector once you have added the script to the Raideo.
    Audition them to see where you would put them.
    Try to keep them in groups that maintain the same mood.
    Or switch to a funkier set of loops to "punctuate" a win.

    THE ORDER OF EVENTS IN THIS SCRIPT IS VITAL TO ITS FUNCTION = DO NOT CHANGE THIS WITHOUT KNOWING WHAT YOU'RE DOING:

    This was written to call from another script.
    Here is the Syntax:

    groove.tune("set","dance");}
    groove.tune("set","triphop");}
    groove.tune("set","off");}

    Code (csharp):
    1. // Make sure that "Play On Awake" is on, and "Loop" is off, for your Audio Source.
    2. private var triphopdelay : float = 4.8; // length of the triphop loops
    3. private var dancedelay : float = 3.692; // length of the dance loops
    4. var triphopDrumLo : AudioClip[];//Lo triphop Drum Loop Array
    5. var triphopBassLo : AudioClip[];//Lo triphop Bass Line Array
    6. var danceDrumLo : AudioClip[];//Hi triphop Drum Loop Array
    7. var danceBassLo : AudioClip[];//Hi triphop Bass Line Array
    8. /*create a one dimensional array where the name is an int from 0-infinity and I and inject my own values*/
    9. static var music = new Array();/*the array is calle music and is accesable as groove.music[x] where x = the int entry value*/
    10. static var station:String;/*the big ticket*/
    11. function Awake () {//prep stuff here so other stuff can use it
    12.     tune("set","off");//tune the raideo off
    13. }
    14. function Start(){/*00*/
    15.     while (true) {/*crack smoker*//*which is to say, "hard worker", or always on the job. ;) */
    16.         station=tune("get","tune");/*call the music array and get the station*/
    17.         switch (station){/*evaluate the station*/
    18.             case "triphop":/*Play a random pair of drum and bass loops from any selection in their entire arrays*/
    19.                 audio.PlayOneShot(triphopDrumLo[Random.Range(0,triphopDrumLo.Length)]);
    20.                 audio.PlayOneShot(triphopBassLo[Random.Range(0,triphopBassLo.Length)]);
    21.                 yield WaitForSeconds (triphopdelay);/*wait for the loops to play themselves thru*/
    22.             break;
    23.             case "dance":/*Play a random pair of drum and bass loops from any selection in their entire arrays*/
    24.                 audio.PlayOneShot(danceDrumLo[Random.Range(0,danceDrumLo.Length)]);
    25.                 audio.PlayOneShot(danceBassLo[Random.Range(0,danceBassLo.Length)]);
    26.                 yield WaitForSeconds (dancedelay);/*wait for the loops to play themselves thru*/
    27.             break;
    28.             case "off":
    29.                 yield WaitForSeconds (2);//wait to see if they turn me back on
    30.             break;
    31.         }
    32.     }
    33. }
    34. /*---TUNE THE RAIDEO-----------------------------------------------------------------*/
    35. static function tune (instruction:String,value:String){
    36.     if ("set" in instruction){/*if we are changing the station*/
    37.         music.Clear();/*empty the array*//*we're only storing whats on so we dont need anything else*/
    38.         music.Add(value);/*inject the value as the first entry (stored at the [0] index)*/
    39.         //Debug.Log (">>>GETTING TIRED OF "+station+".  TIME FOR SOME "+value+".<<<");/*tell teh player what we did*/
    40.     }else if ("get" in instruction){/*if we are retreiving the station*/
    41.         value=music[0];/*assign the value to the first array entry (stored at the [0] index)*/
    42.         return value;/*return the value*/
    43.     }
    44. }
    >>>THE ORDER OF EVENTS IN THIS SCRIPT IS VITAL TO ITS FUNCTION<<<

    Got the idea from this part of the Unity Documentation:
    // Launches a projectile in 2 seconds
    var projectile : Rigidbody;
    Invoke("LaunchProjectile", 2);
    function LaunchProjectile () {
    instance = Instantiate(prefab);
    instance.velocity = Random.insideUnitSphere * 5;
    }

    TODO:
    Consider weaving invoke into this instead of WaitForSeconds()

    Was going to post to the Wiki, still might.

    I hope this helps somebody as much as you guys have all helped me.
    -Hai
     

    Attached Files:

  2. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    looks good so far, will give it a try later on, thanks for the good job.