x


Pause Menu Volume

hi guys. i have a pause menu script that i want to script to control all in game volume. i want it to be controlled through a horizontal GUI slider, but i have been having problems with that. here's the script:

using UnityEngine;
using System.Collections;

public class PauseMenu : MonoBehaviour 
{
    public GUISkin myskin;

    private Rect windowRect;
    private bool paused = false , waited = true;
    private bool options;
    private float hSliderValue = 0.5f;


    private void Start()
    {
       windowRect = new Rect(Screen.width / 2 - 100 , Screen.height / 2 - 100, 200, 200);
    }

    private void waiting()
    {
       waited = true;
    }

    private void Update ()
    {
       if (waited)
       if(Input.GetKey(KeyCode.P) || Input.GetKey(KeyCode.Escape))
       {
         if (paused)
          paused = false;
         else
          paused = true;

         waited = false;
         Invoke("waiting",0.3f);
       }

       if(paused)
         Time.timeScale = 0;
       else
         Time.timeScale = 1;

       if(paused)
         AudioListener.pause = true;
       else
         AudioListener.pause = false;

       if(options)
         AudioListener.volume = 0.10f;
       else
         AudioListener.volume = 0.2f;


    }

    private void OnGUI()
    {
       if (paused)
         windowRect = GUI.Window(0,windowRect, windowFunc, "Game Paused");


    }

    private void windowFunc(int id)
    {
       if(GUILayout.Button("Resume"))
       {
         paused = false;
       }
       if(GUILayout.Button("Volume"))
       {
         options = true;
       }
       if(GUILayout.Button("Return to Main Menu"))
       {
         Application.LoadLevel (0);
       }




}







}

do you have any suggestions in c#? thanks!

more ▼

asked Jun 22 '12 at 12:55 PM

BakuJake13 gravatar image

BakuJake13
30 12 19 23

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I modified your script a bit. If you press volume, the button will be replaced by a slider. If you press resume, the volume will be the value of the slider.

I don't know what the if(waited) does, so I removed it from the update. It just stood there and did nothing.

If you want your slider to be higher or wider, you can add some GUILayoutOptions. Check out the links. I hope this helps :)

http://unity3d.com/support/documentation/ScriptReference/GUILayoutOption.html

http://unity3d.com/support/documentation/ScriptReference/GUILayout.HorizontalSlider.html


public GUISkin myskin;

private Rect windowRect;
private bool paused = false, waited = false;
private bool options;
private float hSliderValue = 0.5f;


private void Start()
{
    windowRect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200);
}

private void waiting()
{
    waited = true;
}

private void Update()
{

    if (Input.GetKeyDown(KeyCode.P) || Input.GetKeyDown(KeyCode.Escape))
    {
        if (paused)
            paused = false;
        else
            paused = true;

        waited = false;
        Invoke("waiting", 0.3f);
    }

    if (paused)
    {
        Time.timeScale = 0;
        AudioListener.pause = true;
    }
    else
    {
        Time.timeScale = 1;
        AudioListener.pause = false;
    }
}

private void OnGUI()
{
    if (paused)
    {
        windowRect = GUI.Window(0, windowRect, windowFunc, "Game Paused");
    }
}

private void windowFunc(int id)
{
    if (GUILayout.Button("Resume"))
    {
        paused = false;
        options = false;

        AudioListener.volume = hSliderValue;
    }
    if (!options)
    {
        if (GUILayout.Button("Volume"))
        {
            options = true;
        }
    }
    else
    {
        hSliderValue = GUILayout.HorizontalSlider(hSliderValue, 0.0f, 1.0f);
    }

    if (GUILayout.Button("Return to Main Menu"))
    {
        Application.LoadLevel(0);
    }
}
more ▼

answered Jun 22 '12 at 01:40 PM

Deoxyz gravatar image

Deoxyz
94 2 3 7

works like a charm! thanks :D

Jun 22 '12 at 02:14 PM BakuJake13
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x402
x268
x78
x43
x19

asked: Jun 22 '12 at 12:55 PM

Seen: 1032 times

Last Updated: Jun 22 '12 at 02:14 PM