[Unity教程]如何暂停
通过学习 Unity官方的 教程素材,来学习如何暂停。
Interactive Tutorial 3:Tweaking Components
一个素材里面有 几个关卡。
运行后,鼠标移动到 其他的视图,就会 出现 暂停 UI,并暂停
PauseCanvas如下图所示,其子类PausePanel,如下图所示
GameController 的GameController(Script)有PauseIndicator ,如下图所示
打开GameController.cs,代码比较简单,逻辑关系用 不同的颜色区别出来了。
这个RestartLevel函数,是否是决定 暂停的函数。下面进行测试。把RestartLevel函数直接放入 Update函数中。
发现的确是 暂停了,但是 Hierarchy 有2个 相同的场景。
MonoBehaviour.OnApplicationFocus(bool)
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationFocus.html
如果 物体 被选中,就 函数就返回 真,否则 返回假。
SceneManager.LoadScene
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.htmlDescription
Loads the Scene by its name or index in Build Settings.
通过场景的名字或者索引数字 载入在 BuildSettings种存在的 场景
Note: In most cases, to avoid pauses or performance
hiccups while loading, you should use the asynchronous version of this command which is:LoadSceneAsync.
多数情况下,为了避免在暂停 或者是
性能打嗝时加载
你应该使用 异步版本的命令:LoadSceneAsync
When using this
SceneManager.LoadScene
,
the loading does not happen immediately, it completes in the next frame. This semi-asynchronous behavior can cause frame stuttering and can be confusing because load does not complete immediately.当使用 SceneManager.LoadScene,载入的场景 并不是 马上 生效,在下一帧才生效。这种 半异步 行为 才会造成 帧数卡顿 并且会 莫名其妙 因为 载入并没有马上生效。
The given
sceneName
can
either be the Scene name only, without the .unity
extension,
or the path as shown in the BuildSettings window still without the .unity
extension.
If only the Scene name is given this will load the first Scene in the list that matches. If you have multiple Scenes with same name but different paths, you should use the full path.仅仅使用 场景的名字 不用 加上 Unity的版本号,或者是 在 BuildSettings视图 的 路径 并没有 unity版本号。如果只有 场景的名字 这种 在 场景表中 找到第一次 先 找到的名字相同的场景 。如果你有多个 相同名字 不同路径的 场景,你应该使用 完整 的路径。
Note that
sceneName
is
case insensitive, except when you load the scene from an AssetBundle.
由于 这个脚本 Debug.Log 和 print 无效。(可能是因为 异步?)
就直接在下面进行注释,再运行,暂停 就失效了。
当没有使用程序的时候(切换程序,最小化程序)的时候,会调用OnApplicationFocus函数
设置 Time.timeScale = 0f; AudioListener.pause = true;
就处于暂停状态。