Fre*_*red 2 .net c# stopwatch unity-game-engine
我想测量并返回用户按下 Tab 和空格按钮之间经过的时间。不幸的是,我的代码只返回 00:00:00。到目前为止,这是我的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
using System;
using System.Diagnostics;
using Debug=UnityEngine.Debug;
public class timer : MonoBehaviour {
public Stopwatch zeit;
void Update () {
zeit = new Stopwatch();
if (Input.GetKeyDown ("tab")) {
zeit.Start();
}
if (Input.GetKeyDown ("space")){
TimeSpan ts = zeit.Elapsed;
zeit.Stop ();
print(ts);
zeit.Reset ();
}
}
}?
Run Code Online (Sandbox Code Playgroud)
调用更新时,您正在重新创建秒表。您可以在“构造函数”中正手初始化秒表。
public Stopwatch zeit = new StopWatch();
void Update () {
if (Input.GetKeyDown ("tab")) {
zeit.Start();
}
if (Input.GetKeyDown ("space")){
TimeSpan ts = zeit.Elapsed;
zeit.Stop ();
print(ts);
zeit.Reset ();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9285 次 |
| 最近记录: |