以点击按钮,实现场景的转换为例。
1,新建按钮
2,新建一空物体,新建一个新脚本,并将此脚本挂在该空物体上,例如,我新建一个名为GameManage的物体,并新建同名脚本挂在该物体上
3,在GameManage添加我们需要的方法,这里是场景的转换。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManage : MonoBehaviour {
public void OnStartGame(int sceneindex)
{
SceneManager.LoadScene(sceneindex);
}
}
4,回到按钮,在OnClick()下添加GameManage物体中的方法:OnStartGame(int sceneindex)
完成!
---------------------以下内容为转换场景--------------
转换场景的代码为SceneManager.LoadScene(sceneindex);首先要添加using UnityEngine.SceneManagement;
SceneManager.LoadScene(*)为重载函数,
参数可以是
Parameters
sceneName | Name or path of the Scene to load. |
sceneBuildIndex | Index of the Scene in the Build Settings to load. |
parameters | Various parameters used to load the Scene. |