我有正在使用的JavaScript代码,它在我的Unity游戏中制作了一个暂停菜单。这是一个简单的暂停菜单,显示">主菜单"one_answers">Quit Game(退出游戏("按钮。
如何将按钮绑定到Unity中的场景?因此,如果我点击">主菜单",我希望它能将我带到scene1
,并用音乐按钮替换">Quit Game(退出游戏("按钮,以打开/关闭我的音乐。
暂停菜单脚本:
#pragma strict
var musicBtnText : String = "Music: On";
var musicOn : boolean = true;
var isPaused : boolean = false;
function Update()
{
if(musicOn)
{
musicBtnText = "Music: On";
}
else
{
musicBtnText = "Music: Off";
}
if(Input.GetKeyDown(KeyCode.P))
{
isPaused = !isPaused;
if(isPaused)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
}
function OnGUI()
{
if(isPaused)
{
if (GUI.Button(Rect(Screen.width/2-50, 240, 100, 40), "Main Menu"))
{
Application.LoadLevel("scene1");
}
if(GUI.Button(Rect(Screen.width/2-50, 300, 100, 40), "" + musicBtnText))
{
musicOn = !musicOn;
GameObject.Find("cameraName").GetComponent(AudioListener).enabled = !GameObject.Find("cameraName").GetComponent(AudioListener).enabled;
}
}
}
但是,如果您想在自己的代码中实现这一点,需要注意的一点是,在统一中制作按钮时,您可以将它们包含在if语句中。这样,只要单击该按钮,就会调用if语句中的任何代码。至于打开和关闭音乐,您需要启用和禁用主音频侦听器,默认情况下,该侦听器连接到主摄像头。所以在我的代码中,当它说GameObject.Find("cameraName")
时,将相机名称替换为主相机的名称。最后,通过调用Application.LoadLevel("levelName")
来实现统一的加载级别。祝你好运!如果这给你带来任何错误,请发表评论,我很乐意为你提供帮助
在C#中:
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour {
public string musicBtnText = "Music: On";
public bool musicOn = true;
public bool isPaused = false;
void Update()
{
if(musicOn)
{
musicBtnText = "Music: On";
}
else
{
musicBtnText = "Music: Off";
}
if(Input.GetKeyDown(KeyCode.P))
{
isPaused = !isPaused;
if(isPaused)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
}
void OnGUI()
{
if(isPaused)
{
if (GUI.Button(new Rect(Screen.width/2-50, 240, 100, 40), "Main Menu"))
{
Application.LoadLevel("scene1");
}
if(GUI.Button(new Rect(Screen.width/2-50, 300, 100, 40), "" + musicBtnText))
{
musicOn = !musicOn;
GameObject.Find("cameraName").GetComponent<AudioListener>().enabled = !GameObject.Find("cameraName").GetComponent<AudioListener>().enabled;
}
}
}
}