- Home /
Sound to play when clicking buttons on GUI
On the main menu of my scene I have created; attempting to link sound to gui buttons to play when clicked on...
Any idea how I would go about this?
This is the following code I have for my main menu scene:
var beep : AudioClip; var menuSkin : GUISkin; var areaWidth : float; var areaHeight : float;
function OnGUI (){ GUI.skin = menuSkin; var ScreenX = ((Screen.width 0.5) - (areaWidth 0.5)); var ScreenY = ((Screen.height 0.5) - (areaHeight 0.5)); GUILayout.BeginArea ( Rect (ScreenX, ScreenY, areaWidth, areaHeight));
if(GUILayout.Button ("Play")) { OpenLevel ("AGP"); } if(GUILayout.Button ("Instructions")) { OpenLevel ("Instructions_page"); }
if(GUILayout.Button ("Credits")) { OpenLevel ("Credits"); }
if(GUILayout.Button ("Quit")) { Application.Quit(); }
GUILayout.EndArea(); }
function OpenLevel (level : String) {
yield new WaitForSeconds (0.35);
Application.LoadLevel (level);
}
Much kudos.
Answer by ziC · May 17, 2011 at 10:30 PM
Attach an AudioSource component to your script. Then write this code:
public AudioClip playClip; public AudioClip instructionsClip;
if(GUILayout.Button ("Play")) { audio.clip = playClip; audio.Play(); OpenLevel ("AGP"); } if(GUILayout.Button ("Instructions")) { audio.clip = instructionsClip; audio.Play(); OpenLevel ("Instructions_page"); }
Then you attach the sound file to your public playClip an instructionsClip.
Just add the rest in the same manner.
Sweet, i noticed if you want to play more then 1 sound at the same time with the same AudioSource. Use this ins$$anonymous$$d: audio.PlayOneShot(TheClip);
Your answer
Follow this Question
Related Questions
How do I use my .png file as a button? 1 Answer
Play Sound when GUI button is pressed. 7 Answers
My GUI buttons are triggering without user input. 1 Answer
Animate GUI Elements 1 Answer
Cube click pop-up menu 0 Answers