问题描述
所以我决定将我的游戏分成不同的场景(主菜单、游戏结束等)并且我试图在我的代码中使用 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.
所以非弃用的等价物:
if (Application.loadedLevelName == "gameover") {
// ...
}
应该是:
if (SceneManager.GetActiveScene().name == "gameover") {
// ...
}
这篇关于使用 SceneManager 检查当前场景名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!