您的位置:首页 > 移动开发 > Unity3D

在Unity3D中使用暂停的小技巧

2015-01-15 14:42 309 查看
很多人在游戏中写暂停脚本的时候,经常会想到 Time.timeScale = 0; 这种方法,但是 Time.timeScale 只是能暂停部分东西。如果在 update 函数中持续改变一个物体的位置,这种位置改变貌似是不会受到暂停影响的。比如

  transform.position = transform.position+transform.TransformDirection(Vector3(0,0,throwForce));

  Time.timeScale = 0 的时候这个东西仍然在动。

  把使用Time.timeScale = 0; 功能的函数写在 FixedUpdate() , 当使用 Time.timeScale = 0 时项目中所有 FixedUpdate() 将不被调用,以及所有与时间有关的函数。 在 update 通过一个布尔值去控制暂停和恢复。unity3D教程手册

  如果您设置 Time.timeScale 为 0,但你仍然需要做一些处理(也就是说动画暂停菜单的飞入),可以使用 Time.realtimeSinceStartup 不受 Time.timeScale 影响。

  另外您也可以参考一下 Unity Answer 上提到的方法 要暂停游戏时,为所有对象调用 OnPauseGame 函数:

01
1
02
03
04
Object[]
objects = FindObjectsOfType (
typeof
(GameObject));
05
06
07
foreach
(GameObject
go
in
objects)
{
08
09
10
go.SendMessage
(“OnPauseGame”, SendMessageOptions.DontRequireReceiver);
11
12
13
}
从暂停状态恢复时:

01
A
basic script with movement
in
the
Update() could have something like
this
:
protected
bool
paused;
02
03
04
void
OnPauseGame
()
05
06
07
{
08
09
10
paused
=
true
;
11
12
13
}
14
15
16
void
OnResumeGame
()
17
18
19
{
20
21
22
paused
=
false
;
23
24
25
}
26
27
28
void
Update
()
29
30
31
{
32
33
34
if
(!paused)
{
35
36
37
//
do movement
38
39
40
}
41
42
43
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: