C# 创建一个类/方法一段时间(开始、重置、停止、get-istant、get-timerun)

C# 创建一个类/方法一段时间(开始、重置、停止、get-istant、get-timerun),c#,class,unity3d,methods,time,C#,Class,Unity3d,Methods,Time,我正在制作一个赛车游戏,我正在研究比赛时间 我尝试构建一个系统来启动具有各种选项的计时器实例 我小小的经验让我陷入了危机。。。会有好灵魂来帮我吗 这就是我的想法: public class Timer { public float counter; public bool reset; public string runtime = "--:--:--"; public string istant = "not istant"; public vo

我正在制作一个赛车游戏,我正在研究比赛时间

我尝试构建一个系统来启动具有各种选项的计时器实例

我小小的经验让我陷入了危机。。。会有好灵魂来帮我吗

这就是我的想法:

public class Timer {


    public float counter;
    public bool reset; 
    public string runtime = "--:--:--";
    public string istant = "not istant";

    public void startTimer()
    {

        /* inupdatealternative: counter += Time.deltaTime; */

        if(reset == true)
        {
            counter = 0;
        }
        else
        {
            counter = Time.time;
        }

        var minutes = counter/60;               // divide guitime by sixty (minutes)
        var seconds = counter%60;               // euclidean division (seconds)
        var fraction = (counter * 100) % 100;   // get fraction of seconds
        runtime = string.Format ( "{0:00}:{1:00}:{2:000}", minutes, seconds, fraction);

        Debug.Log("in Start: "+runtime);

    }

    public void resetTimer()
    {
        reset = true;
    }

    public string getTimerRuntime()
    {
        return runtime;
    }

    public string getTimerIstant()
    {
        istant = runtime;
        return istant;
    }

}
在更新中,例如:

var lapTimer = new Timer(); // create a new timer
if(Lap < Pilot.pilotlap )
{
    lapTimer.startTimer();
    Lap++
}
else if(Lap==Pilot.pilotlap)
{
    timerLabel.text = lapTimer.getTimerIstant();
    lapTimer.resetTimer();
    lapTimer.startTimer();
}
var lapTimer=new Timer();//创建一个新计时器
如果(圈数

在我的脑海里,我相信已经有人处理过了。。。肯定会有一些东西管理时间并返回值​​以各种方式:它存在吗?或者说有没有办法制造或制造这样的东西?

有,它被称为
秒表
,它是C#中使用的使用精确计时器的类,它位于
System.Diagnostics
命名空间中

使用
Update()
示例,您可以这样使用它:

//创建一个新的秒表实例
//如果计时器重复使用,只需在开始时实例化一个计时器并重复使用,
//避免产生垃圾
秒表计时圈=新秒表();
如果(圈数
您可以在此处阅读有关该类(其方法、字段和属性)的信息:


您正在进行大量不必要的
bool
和本地字段复制和设置。我会简单地使用

public class Timer 
{
    private float _startTime;
    public bool IsRunning;

    // you don't need an extra reset method
    // simply pass it as a parameter
    public void Start(bool reset = false)
    {
        if(IsRunning && !reset)
        {
            Debug.LogWarning("Timer is already running! If you wanted to restart consider passing true as parameter.");
            return;
        }

        _startTime = Time.time;                                             

        Debug.Log("in Start: " + GetFormattedTime(_startTime));

        IsRunning = true;
    }

    // depending what stop should do
    // since this doesn't use any resources while running you could also simply
    // only stick to the Start method and pass in true .. does basically the same
    public void Stop()
    {
        IsRunning = false;
    }

    // I didn't see any difference between you two methods so I would simply use
    public string GetCurrentTime()
    {
        if(!IsRunning)
        {
            Debug.LogWarning("Trying to get a time from a Timer that isn't running!");
            return "--:--:---";
        }

        var timeDifference = Time.time - _startTime;

        return GetFormattedTime(timeDifference);
    }

    private static string GetFormattedTime(float time)
    {
                                                                  // e.g. time = 74.6753
        var minutes = Mathf.FloorToInt(time / 60f);               // e.g. 1 (rounded down)
        var seconds = Mathf.FloorToInt(time - 60f * minutes);      // e.g.  14 (rounded down)
        var fraction = Mathf.RoundToInt((time - seconds) * 1000f); // e.g. 676 (rounded down or up)

        // Use a string interpolation for better readability
        return $"{minutes:00}:{seconds:00}:{fraction:000}";
    }
}
然后在您的
更新中
您不想使用

var lapTimer = new Timer(); // create a new timer
一直以来,因为它会创建一个新的计时器,你不会得到任何跟踪时间。。。你宁愿只使用一次

private Timer timer;

// just in case you want to keep track of needed times per lap
public List<string> lapTimes = new List<string>();

private void Awake()
{
    timer = new Timer();
    lapTimes.Clear();
}

private void Update()
{
    ...

    if(Lap < Pilot.pilotlap)
    {
        timer.Start();
        Lap++
    }
    else if(Lap == Pilot.pilotlap)
    {
        var currentTime = timer.GetCurrentTime();
        timerLabel.text = currentTime;
        lapTimes.Add(currentTime);
        timer.Start(true)
    }

    ...
}
专用定时器;
//以防你想记录每圈所需的时间
公共列表圈数=新列表();
私人空间
{
定时器=新定时器();
圈数;
}
私有void更新()
{
...
if(圈数<引航员引航图)
{
timer.Start();
圈++
}
else if(Lap==Pilot.pilotlap)
{
var currentTime=timer.GetCurrentTime();
timerLabel.text=当前时间;
lapTimes.Add(当前时间);
计时器启动(真)
}
...
}

请注意,我不知道这是否是您在
Update
中的全部内容,也不知道您是如何使用它的,但您可能也不想(重新)启动计时器并计算每帧的
圈数
您的条件是
正确的
。。。应该进行更多检查,以确保每圈只能调用一次

您是否查看了
TimeSpan
?问题到底是什么?有什么事情没有按预期进行吗?我不知道时间跨度。。。现在我学习它。谢谢是的,当我调用操作时,变量不会改变。也许,我来自javascript,在c#的方法和类方面有困难。时间开始了。。。但是不重置也不返回值:还有时间部分但不返回你看过秒表类(System.Diagnostics)了吗?你刚才是不是“steel”Nils Lande的答案?;)在这种情况下,秒表工作得非常好,谢谢。这不是我一直在寻找的解决方案,但目前还不错。感谢大家,我稍后会回来创建一个特定的类。我不是“钢铁”,而是“增强”。:我只是想给出一个简单使用Stopwatch类的恰当例子,因为它觉得OP根本不知道它的存在@阿尔贝托,那你到底想完成什么?当你得到一个特定的时间消耗量时,你可以根据自己的需要来处理它,如果你进一步详细说明你想要什么,我们可以进一步讨论它。@Galandil我肯定会深化这个问题,因为我不想依赖带计时器的库,而是想了解如何做和改进。刚好有足够的时间来解决这个新发现的问题,然后回到这里。(为了解释:我正在与Ui、光线投射和点击以及阻止XD一团糟的lerp旋转作斗争)我解决并返回:D