如何让Unity在大量固定时间执行某项操作 [英] How to have Unity do something at a large number of fixed timings

查看:914
本文介绍了如何让Unity在大量固定时间执行某项操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时序列表(以秒为单位),需要在该列表上执行一个功能.我尝试了一种非常幼稚的方法,但是它似乎不起作用,很可能是因为Time.fixedTime并不总是准确的.关于如何在1000次确定的时间内执行某件事的更好的主意(但分布不均等)?

I have a List of Timings (in seconds), at which a function needs to be executed. I tried a very naive way but it doesn't seem to work, most likely because Time.fixedTime isn't always exact. Any better ideas on how to execute something on 1000 determined (but not equally spread out) times?

if (Timings.Contains(Time.fixedTime)){
    //do something
}

推荐答案

如果要在特定时间内连续执行某项操作,则必须在协程中使用Time.deltaTime.用Time.deltaTime0中的float值递增,直到达到您要执行此操作的时间为止.

If you want to continuously do something for a specific amount of time,you have to use Time.deltaTime in a coroutine. Increment a float value from 0 with Time.deltaTime until it reaches the time you want to do that thing.

IEnumerator executeInWithFixedTiming(float time)
{
    float counter = 0;

    while (counter <= time)
    {
        counter += Time.deltaTime;

        //DO YOUR STUFF HERE
        transform.Rotate(Vector3.right * Time.deltaTime);

        //Wait for a frame so that we don't freeze Unity
        yield return null;
    }
}

您可以像下面一样开始尽可能多的任务.该示例运行代码5秒:

You can start as many tasks as possible like below. The example run the code for 5 seconds:

StartCoroutine(executeInWithFixedTiming(5));


您还可以扩展此功能,并使其在该协程中的操作参数为Action.然后,您也可以传入代码以在该函数中运行.未经测试,但也可以工作.


You can also extend this function and make it take a parameter of what to do in that coroutine as Action. You can then pass in the code to run inside that function too. Not tested but should also work.

IEnumerator executeInWithFixedTiming(Action whatToDo, float time)
{
    float counter = 0;

    while (counter <= time)
    {
        counter += Time.deltaTime;
        whatToDo();
        //Wait for a frame so that we don't freeze Unity
        yield return null;
    }
}

然后像这样使用它:

StartCoroutine(executeInWithFixedTiming(
delegate
{
    //DO YOUR STUFF HERE
    transform.Rotate(Vector3.right * Time.deltaTime);
}, 5));



问题是,我不想连续X秒钟这样做,但是 在每个时间点只有一次

The thing is, I don't want to continuously do it for X seconds, but only once at each point of Timings

您提到计时器是经过排序的,因此for循环可以循环通过它,而while循环可以等待计时器完成,这应该做到这一点.

You mentioned that the timer is sorted so a for loop to loop through it and while loop to wait for the timer to finish should do it.

List<float> timer = new List<float>();

IEnumerator executeInWithFixedTiming()
{
    float counter = 0;

    //Loop through the timers
    for (int i = 0; i < timer.Count; i++)
    {
        //Wait until each timer passes
        while (counter <= timer[i])
        {
            counter += Time.deltaTime;
            //Wait for a frame so that we don't freeze Unity
            yield return null;
        }

        //TIMER has matched the current timer loop.
        //Do something below
        Debug.Log("TIMER REACHED! The current timer is " + timer[i] + " in index: " + i);
    }

    //You can now clear timer if you want
    timer.Clear();
}

只需在Start函数中启动协程一次,它就应该处理计时器. StartCoroutine(executeInWithFixedTiming());.您还可以使用此答案中的第二个示例对其进行修改,以使其采用每个代码的参数来执行.

Just start the coroutine once in the Start function and it should handle the timer. StartCoroutine(executeInWithFixedTiming());. You also also modify it with the second example in this answer to make it take a parameter of each code to execute.

注意:

在Unity中,最好使用Time.deltaTime计时.这是我所知道的最准确的计时方式.其他Unity变量会随着时间的流逝而失去准确性.

In Unity, it's better to time something with Time.deltaTime. It's the most accurate way of timing that I know about. Other Unity variables tend to loose their accuracy over time.

这篇关于如何让Unity在大量固定时间执行某项操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆