- Home /
Scale gui button to resolution and playing audio
Hi I have a pause button that when pressed sets time to 0.
When it is at 0 I draw 2 buttons onto the screen, a resume and menu button.
I have two problems; when I click resumethe sound plays fine, when I click menu the sound cuts out too early, how can I fix this ? a tutor mentioned something about invoke.
Also another problem I have is scaling these two gui buttons to the screen resolution, how can I go about adapting code so the buttons scale with resolution ? thank you in advance.
my code is
using UnityEngine;
using System.Collections;
public class pauseScript : MonoBehaviour
{
bool Pause = false;
public GUIStyle ResumeStyle;
public GUIStyle MenuStyle;
void OnMouseUp() {
{
audio.Play();
if (Time.timeScale == 1)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
}
void OnGUI ()
{
if (Time.timeScale == 0)
{
if (GUI.Button (new Rect (Screen.width/2-50, Screen.height/2-25, 100, 50),"",ResumeStyle))
{
audio.Play();
Time.timeScale = 1;
}
if (GUI.Button (new Rect (Screen.width/2-50, (Screen.height/2-25)+60, 100, 50),"",MenuStyle))
{
audio.Play();
Time.timeScale = 1;
Application.LoadLevel ("MAINMENU");
}
}
}
}
Answer by cdrandin · May 03, 2015 at 08:51 PM
Take a look at the new UI system.
Placing a canvas and a UI Button object in the scene will automatically scale the button to the resolution with some minor changes in the inspector. Then you can add in events for onclick. That way your onclick event will be to turn on the audio or play a specific AudioClip.