找回密码
 立即注册
查看: 317|回复: 0

[Unity教程]如何暂停

[复制链接]
发表于 2021-12-21 19:31 | 显示全部楼层 |阅读模式
通过学习 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.html
Description

[size=0.875em] Loads the Scene by its name or index in Build Settings.

[size=0.875em] 通过场景的名字或者索引数字 载入在 BuildSettings种存在的 场景


[size=0.875em]Note:[size=0.875em] In most cases, to avoid pauses or performance hiccups while loading, you should use the asynchronous version of this command which is:LoadSceneAsync[size=0.875em].
多数情况下,为了避免在暂停 或者是
性能打嗝时加载
你应该使用 异步版本的命令:LoadSceneAsync
[size=0.875em]When using this SceneManager.LoadScene[size=0.875em], 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,载入的场景 并不是 马上 生效,在下一帧才生效。这种 半异步 行为 才会造成 帧数卡顿 并且会 莫名其妙 因为 载入并没有马上生效。

[size=0.875em]The given sceneName[size=0.875em] can either be the Scene name only, without the .unity[size=0.875em] extension, or the path as shown in the BuildSettings window still without the .unity[size=0.875em] 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版本号。如果只有 场景的名字 这种 在 场景表中 找到第一次 先 找到的名字相同的场景 。如果你有多个 相同名字 不同路径的 场景,你应该使用 完整 的路径。

[size=0.875em]Note that sceneName[size=0.875em] is case insensitive, except when you load the scene from an AssetBundle[size=0.875em].
注意 场景的名字 不区分 大小写,除非 当你 用 AssetBundle 来载入场景。  
由于 这个脚本 Debug.Log 和  print 无效。(可能是因为 异步?)
就直接在下面进行注释,再运行,暂停 就失效了。



当没有使用程序的时候(切换程序,最小化程序)的时候,会调用OnApplicationFocus函数
设置 Time.timeScale = 0f; AudioListener.pause = true;
就处于暂停状态。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-6-15 10:26 , Processed in 0.099517 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表