生成场景并转换!
Unity5.3
UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ApplicationnDemo : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log(SceneManager.GetActiveScene().buildIndex);
Debug.Log(SceneManager.sceneCount);
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
if (GUI.Button(new Rect(0,0,60,40),"截图"))
{
Application.CaptureScreenshot("./Picture/11");
Debug.Log("截图成功");
Application.CaptureScreenshot(Application.dataPath + "/Picture/123.png");//得到相对路径
}
Debug.Log(SceneManager.GetActiveScene().buildIndex +"当前关卡");
Debug.Log(SceneManager.sceneCountInBuildSettings +"游戏加载关卡总数");
//表示游戏关卡总数减一得到最大索引
if (SceneManager.GetActiveScene().buildIndex != SceneManager.sceneCountInBuildSettings -1)
{
if (GUI.Button(new Rect(0, 45, 60, 40), "下一关"))
{
GameObject gamemanger = GameObject.Find("GameObject1");
DontDestroyOnLoad(gamemanger);
//获取当前活跃的场景加1
int index = SceneManager.GetActiveScene().buildIndex + 1;
SceneManager.LoadScene(index);
}
}
if (SceneManager.GetActiveScene().buildIndex != 0)
{
if (GUI.Button(new Rect(0, 90, 60, 40), "上一关"))
{ SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
}
}
void OnApplicationQuit()
{
Application.Quit();
}
}
5.2
using UnityEngine;
using System.Collections;
public class ApplicationDemo : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log(Application.loadedLevel +" 场景测试");
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
if (GUI.Button(new Rect(0,0,60,40),"截图"))
{
Application.CaptureScreenshot(@"/Pictures/YY.png");
Debug.Log("截图成功"); Application.CaptureScreenshot(Application.dataPath + "/Pictures/YY.png");
}
//5.3中使用SceneManager.sceneCount 替代 Application.levelCount
if (Application.loadedLevel != Application.levelCount-1)
{
if (GUI.Button(new Rect(0, 45, 60, 40), "下一个关卡"))
{
//5.3 使用下面的方法 替代5.2中的
//SceneManager.LoadScene(0);Application.LoadLevel(0);
Application.LoadLevel(Application.loadedLevel + 1);
//gamemanager是脚本绑定到的空物体的名字
GameObject gamemanager = GameObject.Find("GameManager");
DontDestroyOnLoad(gamemanager);
}
}
if (Application.loadedLevel!=0)
{
if (GUI.Button(new Rect(0, 90, 60, 40), "上一个关卡"))
{
Application.LoadLevel(Application.loadedLevel - 1);
}
}
}
void OnApplicationQuit()
{
Application.Quit();
}
}