返回介绍

Time.realtimeSinceStartup 从开始真实时间

发布于 2019-12-18 15:38:40 字数 3642 浏览 1372 评论 0 收藏 0

JavaScript => public static var realtimeSinceStartup: float;
C# => public static float realtimeSinceStartup;

Description 描述

The real time in seconds since the game started (Read Only).

以秒计,自游戏开始的真实时间(只读)。

In almost all cases you can and should use Time.time instead.

在几乎所有情况下,你应该使用Time.time代替。

realtimeSinceStartup returns the time since startup, not affected by Time.timeScale. realtimeSinceStartup also keeps increasing while the player is paused (in the background). Using realtimeSinceStartup is useful when you want to pause the game by setting Time.timeScale to zero, but still want to be able to measure time somehow.

realtimeSinceStartup返回从开始时间,不受Time.timeScale影响。即使玩家暂停游戏,realtimeSinceStartup也将保持增加(在后台)。当你想通过设置Time.timeScale为0暂停游戏时,但仍希望能够以某种方式来计算时间,使用realtimeSinceStartup是很有用的。

Note that realtimeSinceStartup returns time as reported by system timer. Depending on the platform and the hardware, it may report the same time even in several consecutive frames. If you're dividing something by time difference, take this into account (time difference may become zero!).

请注意, realtimeSinceStartup返回时间来源于系统的计时器。根据不同的平台和硬件,甚至在一些连贯的帧上报告相同的时间,如果通过不同的时间来区分不同的事情,要考虑到这点(时间差可能变成零!)。

JavaScript:

// Prints the real time since start up.
print(Time.realtimeSinceStartup);

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Example() {
        print(Time.realtimeSinceStartup);
    }
}

另一个例子:

JavaScript:

// An FPS counter.
// It calculates frames/second over each updateInterval,
// so the display does not keep changing wildly.
 
var updateInterval = 0.5;
private var lastInterval : double; // Last interval end time
private var frames = 0; // Frames over current interval
private var fps : float; // Current FPS
 
function Start() {
	lastInterval = Time.realtimeSinceStartup;
	frames = 0;
}
 
function OnGUI () {
	// Display label with two fractional digits
	GUILayout.Label("" + fps.ToString("f2"));
} 
 
function Update() {
	++frames;
	var timeNow = Time.realtimeSinceStartup;
	if( timeNow > lastInterval + updateInterval )
	{
		fps = frames / (timeNow - lastInterval);
		frames = 0;
		lastInterval = timeNow;
	}
}

C#:

using UnityEngine;
using System.Collections;
 
// An FPS counter.
// It calculates frames/second over each updateInterval,
// so the display does not keep changing wildly.
public class ExampleClass : MonoBehaviour {
    public float updateInterval = 0.5F;
    private double lastInterval;
    private int frames = 0;
    private float fps;
    void Start() {
        lastInterval = Time.realtimeSinceStartup;
        frames = 0;
    }
    void OnGUI() {
        GUILayout.Label("" + fps.ToString("f2"));
    }
    void Update() {
        ++frames;
        float timeNow = Time.realtimeSinceStartup;
        if (timeNow > lastInterval + updateInterval) {
            fps = (float) (frames / (timeNow - lastInterval));
            frames = 0;
            lastInterval = timeNow;
        }
    }
}

Time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文