Unity scene jump simple code and easy to operate

1. Before jumping the scene, you need to add the scenes that need to jump and use the jump in File —> Build Settings --> Add Open Scenes.
Insert picture description here
2. Jump code The
old version of the jump code is outdated. Use the new version of the code and show the old version of the code.
Old version jump code 1

    //跳转场景方法
    public void OnStartGame(string ScenceName) {
    
    
        //跳转场景  ScenceName场景的名字
        Application.LoadLevel(ScenceName);
    }

Old version jump code 2

//跳转场景方法2
    public void OnStartGame2(int ScenceIndex)
    {
    
    
        //跳转场景 ScenceIndex根据场景的下标 
        Application.LoadLevel(ScenceIndex);
    }

The new version of the jump code The
namespace needs to be quoted before the jump:
using UnityEngine.SceneManagement;
Jump code 3:

//使用场景名进行跳转
void OnClicke() {
    
    
		SceneManager.LoadScene(ScenceName);
	}

Jump code 4:

//使用第一步添加场景时的索引进行跳转
void OnClicke() {
    
    
		SceneManager.LoadScene(ScenceIndex);
	}

Finally, put the code on the button. That's all
there is to it.

Guess you like

Origin blog.csdn.net/m0_47605113/article/details/109448347