使用SceneManager检查当前场景名称 [英] Checking current scene name using SceneManager

查看:215
本文介绍了使用SceneManager检查当前场景名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我决定将我的游戏分为单独的场景(主菜单,游戏结束等),并且我试图在代码中使用if语句来检查当前场景是否为游戏结束",因此我可以执行一些动作.我知道Unity现在使用SceneManager而不是Application,但是此功能等效于

So I've decided to split my game into separate scenes(main menu, gameover, etc.) And I'm trying to use an if statement in my code to check if the current scene is "gameover" so I can perform some action. I know Unity now uses SceneManager instead of Application, but what's the equivalent of this function

 if(Application.loadedLevelName == "gameover")

我尝试了SceneManager.GetActiveScene == "gameover" 但我只是收到像不能在此处使用==的错误. 我也导入了SceneManager

I've tried SceneManager.GetActiveScene == "gameover" But I just get errors like can't use == here. I have imported SceneManager aswell

推荐答案

您在这里遇到错误,因为 SceneManager.GetActiveScene() 返回类型为SceneManager.Scene的对象,而不是字符串.但是,根据文档,这使您可以访问公共Scene.name ,这是一个字符串.

You're getting an error here because SceneManager.GetActiveScene() returns an object of type SceneManager.Scene, not a string. However, according to the documentation, this gives you access to the public Scene.name, which is a string.

因此不建议使用的等效项:

So the non-deprecated equivalent of:

if (Application.loadedLevelName == "gameover") {
    // ...
}

将是:

if (SceneManager.GetActiveScene().name == "gameover") {
    // ...
}

这篇关于使用SceneManager检查当前场景名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆