实现 iPhone 游戏暂停功能的方法

来源:互联网 发布:系统优化的目的 编辑:程序博客网 时间:2024/05/02 02:51
一提到游戏暂停,很多人会想到 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 通过一个布尔值去控制暂停和恢复。
如果您设置 Time.timeScale 为 0,但你仍然需要做一些处理(也就是说动画暂停菜单的飞入),可以使用 Time.realtimeSinceStartup 不受 Time.timeScale 影响。


 Unity Answer 上提到的方法

要暂停游戏时,为所有对象调用 OnPauseGame 函数:
 Object[] objects = FindObjectsOfType (typeof(GameObject));
foreach (GameObject go in objects) {
        go.SendMessage ("OnPauseGame", SendMessageOptions.DontRequireReceiver);
}

从暂停状态恢复时,
A basic script with movement in the Update() could have something like this:
protected bool paused;

void OnPauseGame ()
{
        paused = true;
}

void OnResumeGame ()
{
        paused = false;
}

void Update ()
{
        if (!paused) {
                // do movement
        }
}



原创粉丝点击