unity3d 切换场景

本文介绍如何在Unity中创建并切换场景。首先创建两个场景并加入到构建设置中,然后通过给登录场景中的按钮添加点击事件实现场景跳转。文中提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,我已经创建好两个场景,放到Scene目录下


2,然后分别运行其场景,在运行的过程中,点击“File”->“Build Setting”;

如图所示,点击“Add Current”,场景就会自动加进去了


3,给LoginSence中的button加一个点击事件,代码如下

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;


public class LoginMgr : MonoBehaviour {


// Use this for initialization
void Start () {
        Debug.Log("game is start");


        var LoginBtn = gameObject.GetComponent<Button>();


        if (LoginBtn.name == "Btn_Login")
        {
            LoginBtn.ButtonClickEvent = LoginMethod;
        }
}


    public void LoginMethod(BaseEventData eventData)
    {
        Debug.Log("开始跳转场景");
        Application.LoadLevelAsync("SelectLevelSence");


        Debug.LogWarning("我成功啦");


    }




    void Awake() {
        Debug.Log(" this is LoginMgr's Awake()");
    }

// Update is called once per frame
void Update () {
        Debug.Log(" this is LoginMgr's Update()");
}




 
}

Application.LoadLevelAsync("SelectLevelSence");和Application.LoadLevelAsync(1);这两种方法都可以.。一个是加载名“SelectLevelSence”的关卡,一个是加载索引为1的关卡


4,运行游戏,点击按钮,就会进入下一个场景了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值