Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Jukebox for Game Music

Discussion in 'Scripting' started by tsphillips, Feb 8, 2006.

  1. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
  2. dho

    dho

    Joined:
    Jul 25, 2005
    Posts:
    14
    I was just looking at your code on the wiki. Nice job. and thanks!
     
  3. grobm

    grobm

    Joined:
    Aug 15, 2005
    Posts:
    217
    I am playing around with your code, but my C# skills stink. How would I implement this in my game?

    Here is some quick code I put together... but it does not do what I want it to do. ... Play a song.

    +++++++++++++++++++++++++++
    using UnityEngine;
    using System.Collections;

    public class JukeBoxTest : MonoBehaviour {
    AudioClip myclip;

    // Use this for initialization
    void Start () {
    JukeboxController jukebox = new JukeboxController();
    jukebox.AddClip("mysong", myclip);
    jukebox.PlayClip("mysong");
    //jukebox.StopClip();
    }

    // Update is called once per frame
    void Update () {

    }
    }
    +++++++++++++++

    Does it require a string path instead of a game audio object?

    Could I call this code through Javascript? If I can what would be the change from C#?

    Thanks in advance.
     
  4. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    There are many ways to skin the cat, but the jukebox object is primarily meant to live in a game manager. See this script as an example of something that can be used as the basis for a game manager:
    http://www.unifycommunity.com/wiki/index.php?title=AManagerClass

    In your code snippet, you have the jukebox object lasting only until the Start() method concludes. Things like sound managers need to persist.

    It looks like an easy fix (here, at least) would be to move your declaration/definition up to be a class variable. For example,
    Code (csharp):
    1.  
    2. . . .
    3. JukeboxController jukebox = new JukeboxController();
    4. public class JukeBoxTest : MonoBehaviour {
    5. . . .
    6.  
    [Edit] TYPO ALERT

    The above should be:

    Code (csharp):
    1.  
    2. . . .
    3. public class JukeBoxTest : MonoBehaviour {
    4.     JukeboxController jukebox = new JukeboxController();
    5. . . .
    6.  
    Sorry about the error. :oops:
     
  5. grobm

    grobm

    Joined:
    Aug 15, 2005
    Posts:
    217
    Thanks tsphillips!

    I will give it a shoot.
     
  6. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    See the above edit. I made an error and transposed the lines. Sorry about that. :oops: