Search Unity

Sound Effect for Selection Grid choices

Discussion in 'Immediate Mode GUI (IMGUI)' started by Jessy, Jan 25, 2008.

  1. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    One area in which the Unity GUI is currently a little weak, in my opinion, is audio. I find it pretty easy to work with text and images with it, but it's more work to bring audio into the picture. It's not a big deal to type "audio.PlayOneShot()" for Buttons, although it would be very nice to be able to define that clip in a Style or Skin. Selection Grids are a different story, however. Does anyone have a tip for getting a sound effect to play only when making a selection in the grid?
     
  2. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    use GUI.changed
    (http://unity3d.com/support/documentation/ScriptReference/GUI-changed.html)

    or something like

    Code (csharp):
    1.  
    2. function Update () {
    3.    if (!selectedgridbutton == selectedgridbutton1) {
    4.        //do something
    5.    }
    6.    selectedgridbutton1 = selectedgridbutton;
    7. }
    8. function OnGUI () {
    9.    selectedgridbutton = GUI.Grid...........
    10. }
    11.  
    Warning it's not real code just an example :wink:
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Thanks!

    I completely forgot I had this thread running. What I ended up doing was:

    Code (csharp):
    1. private var objects = new Array();  // a list of all currently available objects
    2. private var objectClicked : String;  // the object that has last been clicked, used for sfx upon selecting an object
    3. var clickSound : GameObject;  // a game object with the click audio clip attached
    4.  
    5. function Start() {
    6.     objects = ?;  // define what the objects array is here
    7.     object = objects.length + 1;  // make it so no object is selected to begin with
    8.     objectClicked = "";
    9. }
    10.  
    11. function OnGUI () {
    12.     var utObjects = objects.ToBuiltin(String);  // so you can modify the objects list and make the GUI grid work
    13.     object = GUI.SelectionGrid (Rect (?, ?, ?, ?), object, utObjects); 
    14.     if (object <= utObjects.Length) {
    15.         if (objectClicked != objects[object]) {
    16.             objectClicked = objects[object];
    17.             clickSound.audio.Play();
    18.         }
    19.     }
    20. }