Unity3D:按钮实现场景的跳转

这一示例使用一个按钮以及脚本演示如何跳转场景(scene)

软件版本:5.3.2.f1

1.将场景加入Buildsetting

打开File----Build Setting,打开场景,点击Add Open Scenes将当前几个场景加入进去

Unity3D:按钮实现场景的跳转


2.在开始界面场景添加一系列Button

GameObject ---- UI-----Button

Unity3D:按钮实现场景的跳转


3.编写按钮的脚本






ButtonNewgame.cs:




using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ButtonNewgame : MonoBehaviour {

// Use this for initialization
void Start () {
}
    void Awake()
    {
        Button button = gameObject.GetComponent() as Button;//获取Button
        button.onClick.AddListener(btClick);//添加监听器用于监听按键事件,并回调函数
    }
    // Update is called once per frame
    void Update () {
}
    void btClick()
    {
        print("Button Click");
        SceneManager.LoadScene(1);//跳转到指定的Level,也就是第一步中的右侧标号
    }
}



4.将按钮脚本拖入指定按钮




你可能感兴趣的:(Unity3D)