[Unity基础]对Time.timeScale的一些理解

总结如下:

1.timeScale不影响UpdateLateUpdate,会影响FixedUpdate

2.timeScale不影响Time.realtimeSinceStartup,会影响Time.timeSinceLevelLoadTime.time

3.timeScale不影响Time.fixedDeltaTimeTime.unscaleDeltaTime,会影响Time.deltaTime

 

测试代码如下:

using UnityEngine;
using System.Collections;

public class TestTimeScale : MonoBehaviour {

	// Update is called once per frame
	void Update () 
    {
        if (Input.GetKeyDown(KeyCode.Q)) Time.timeScale = 0;
        if (Time.timeScale == 0)
        {
            print("Update");

            print("Time.time:" + Time.time);
            print("Time.timeSinceLevelLoad:" + Time.timeSinceLevelLoad);
            print("Time.realtimeSinceStartup:" + Time.realtimeSinceStartup);

            print("Time.deltaTime:" + Time.deltaTime);
            print("Time.fixedDeltaTime:" + Time.fixedDeltaTime);
            print("Time.unscaledDeltaTime:" + Time.unscaledDeltaTime);
        }
        //print("Time.timeSinceLevelLoad:" + Time.timeSinceLevelLoad);
	}

    void LateUpdate()
    {
        if (Time.timeScale == 0) print("LateUpdate");
    }

    void FixedUpdate()
    {
        if (Time.timeScale == 0) print("FixedUpdate");
    }
}


分别对部分代码进行注释,测试结果如下:

timeScale等于0时:

[Unity基础]对Time.timeScale的一些理解_第1张图片

1.UpdateLateUpdate可以执行,FixedUpdate不可以执行

[Unity基础]对Time.timeScale的一些理解_第2张图片

2.Time.realtimeSinceStartup依然在增加,Time.timeSinceLevelLoadTime.time均不变


3.Time.fixedDeltaTime不变,Time.deltaTime变为0Time.unscaleDeltaTime就像游戏正常速度运行下的Time.deltaTime


再次总结:

        当想代码受timeScale控制时(如暂停、加速),可以把代码放在FixedUpdate中,又或者跟Time.timeTime.deltaTime扯上关系;否则,可以用Time.realtimeSinceStartup(类似正常状态下的Time.time)Time.unscaleDeltaTime(类似正常状态下的Time.deltaTime)


你可能感兴趣的:(TimeScale)