gpt4 book ai didi

c# - 我在 Unity 中使用了 C# 秒表类,但它只返回 00 :00:00

转载 作者:太空宇宙 更新时间:2023-11-03 17:30:02 24 4
gpt4 key购买 nike

我想测量并返回用户按下 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 ();
}
}
}


最佳答案

您在调用更新时正在重新创建秒表。您可以在“构造函数”中初始化正手秒表。

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 ();
}
}

关于c# - 我在 Unity 中使用了 C# 秒表类,但它只返回 00 :00:00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45914775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com