在游戏中我们经常看见不同游戏的关卡之间的切换.那是如何实现的呢?如何在不同的场景中进行相同的控制呢!看完本篇文章会有一定的了解.
Application类##
这个可以对场景一中的音量进行设置大小。
实现这个效果主要用到了Application.LoadLevel(加载场景对应的数字)这个方法被SceneManager.LoadScene(1)代替了。
Application的一些属性,方法总结。
float vl=8;
// Use this for initialization
void Start () {
print(Application.loadedLevelName);//打印当前Scene的名字
//print(SceneManager.LoadScene());
print(Application.levelCount);//打印场景的个数
Debug.Log(SceneManager.sceneCount);//打印场景的个数
// Application.LoadLevel(0);
print(Application.platform);//打印当前游戏平台
Debug.Log("路径" + Application.dataPath);//打印项目的路径
//Debug.Log(Application.LoadLevelAsync(1));//异步加载场景,游戏一开始就会制动帮你加载到下一个场景
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();//退出游戏。在我们发布后可以实施,只有在当前场景按下Escape才会退出
}
if (Input.GetKeyDown(KeyCode.Space))
{
Application.CaptureScreenshot("截图.PNG");//截图保存在生成的Data文件下
}
}
void OnGUI()
{
GUILayout.Label("场景一");
if (GUILayout.Button("点击切换场景二"))
{
// Application.LoadLevel(1);
SceneManager.LoadScene(1);//加载另一个场景的代码
}
if (GUILayout.Button("打开网址"))
{
Application.OpenURL("http://www.jianshu.com/users/86b3a0af4571/latest_articles");//打开对应的网页
}
if (GUILayout.Button("音量大小"))
{
vl=PlayerPrefs.GetFloat("Volume");
}
GUILayout.Label(vl.ToString());
}
PlayerPrefs类###
用来承储数据与获得数据。可以理解为一个仓库。我们通过这个类来承储我们想要的东西或结果。类似于键值对。
代码演示
场景一的代码:
public AudioSource source;
float vl=8;
// Use this for initialization
void Start () {
print(Application.loadedLevelName);//打印当前Scene的名字
//print(SceneManager.LoadScene());
print(Application.levelCount);//打印场景的个数
Debug.Log(SceneManager.sceneCount);//打印场景的个数
// Application.LoadLevel(0);
print(Application.platform);//打印当前游戏平台
Debug.Log("路径" + Application.dataPath);//打印项目的路径
//Debug.Log(Application.LoadLevelAsync(1));//异步加载场景,游戏一开始就会制动帮你加载到下一个场景
source = transform.GetComponent<AudioSource>();
source.volume = PlayerPrefs.GetFloat("Volume");
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();//退出游戏。在我们发布后可以实施,只有在当前场景按下Escape才会退出
}
if (Input.GetKeyDown(KeyCode.Space))
{
Application.CaptureScreenshot("截图.PNG");//截图保存在生成的Data文件下
}
}
void OnGUI()
{
GUILayout.Label("场景一");
if (GUILayout.Button("点击切换场景二"))
{
// Application.LoadLevel(1);
SceneManager.LoadScene(1);//加载另一个场景的代码
}
if (GUILayout.Button("打开网址"))
{
Application.OpenURL("http://www.jianshu.com/users/86b3a0af4571/latest_articles");//打开对应的网页
}
//if (GUILayout.Button("音量大小"))
//{
// vl=PlayerPrefs.GetFloat("Volume");
//}
if (GUILayout.Button("音量设置"))
{
SceneManager.LoadScene(1);
}
// GUILayout.Label(vl.ToString());
}
场景二的代码:
AudioSource source;
// Use this for initialization
float v;
void Start () {
source = GetComponent<AudioSource>();
v = PlayerPrefs.GetFloat("Volume");
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUILayout.Label("场景二");
if (GUILayout.Button("点击切换场景一"))
{
// Application.LoadLevel(1);
SceneManager.LoadScene(0);
}
//if (GUILayout.Button("设置音量大小"))
//{
// PlayerPrefs.SetFloat("Volume", 1);
//}
v=GUI.HorizontalSlider(new Rect(100,200,100,50),v,0f,1f);
source.volume = v;
PlayerPrefs.SetFloat("Volume",v);
}