有什么方法可以控制Unity中的时间延迟? [英] What are ways to control the time delay in Unity?

查看:85
本文介绍了有什么方法可以控制Unity中的时间延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,显然我可以在这些库/解决方案之间进行选择:

I've looked around and apparently I've got the choice between these libraries/ solutions:

一个:

public void Awake() {
    Invoke("InvokeAndPrint", 2);
}

void InvokeAndPrint() {
    print("InvokeAndPrint 2");
}

两个:

void Start() {
    StartCoroutine(WaitAndPrint(2.0F));
}

IEnumerator WaitAndPrint(float waitTime) {
    yield return new WaitForSeconds(waitTime);
    print("WaitAndPrint " + Time.time);
}

我想知道是否还有其他更好的方法?

I want to know if there is any other better way?

推荐答案


这是我在上面的评论的总结


This is a summary of my comments above

我唯一想到的另一种方法是过时的学习,要注意开始的时间;然后在您的 Update()方法中检查经过的时间.您基本上是自己做所有事情.尽管比上面的示例更冗长,但它是类型安全的,不需要任何额外的线程或线程作业对象.

The only other method I can think of is old-school take note of the time at start; then in your Update() method check for elapsed time. You essentially do everything yourself. Though way more verbose than the above examples, it's type-safe and does not require any extra threads or thread job objects.

首先,我们需要定义一些字段:

First we need some fields defined:

private DateTime _start;
private bool _done;

开始时请记下时间:

void Start()
{
    _start = DateTime.Now;
}

...然后在更新中检查已花费了多少时间.如果它大于您说的2秒超时,请启动您想执行的任何操作-在这种情况下,请 print():

...then in your update check to see how much time has elapsed. If its greater than your timeout of say 2 seconds, fire off what-ever you wish to do - in this case print():

void Update()
{
    if (! _done && (DateTime.Now - _start).TotalSeconds >= 2)
    {
        print("hello world");
        _done = true;
    }
}

就是这样.

您可能会发现,在很多地方都需要这样做,因此,如果有一种减少重复代码的方法,那不是很麻烦.也许是一个包装它的班级?

You'll probably find that there are many places where there is a need for this so wouldn't it be groovy if there was a way to cut down on repeated code. Perhaps a class to wrap it up in?

class DelayedJob
{
    private readonly TimeSpan _delay;
    private readonly Action _action;
    private readonly DateTime _start;

    public DelayedJob(TimeSpan delay, Action action)
    {
        if (action == null)
        {
            throw new ArgumentNullException("action");
        }
        _delay = delay;
        _action = action;
        _start = DateTime.Now;
    }

    /// <summary>
    /// Updates this instance.
    /// </summary>
    /// <returns>true if there is more work to do, false otherwise</returns>
    public bool Update()
    {
        if (DateTime.Now - _start >= _delay)
        {
            _action();
            return false;
        }

        return true;
    }
}

然后您可以执行以下操作:

Then you could do something like this:

void Start()
{
    _job = new DelayedJob(TimeSpan.FromSeconds(2), ()=> print("hello"));
}

...相应地更新了 Update()之后:

...after updating your Update() accordingly:

void Update()
{
    if (_job != null && !_job.Update())
    {
        _job = null;
    }
}

多个职位

只需将它们放入集合中并在运行时进行处理即可.

Multiple Jobs

It's just a matter of placing them in a collection and processing it at runtime.

private List<DelayedJob> _jobs; 

void Start()
{
    _jobs = new List<DelayedJob>
            {
                new DelayedJob(TimeSpan.FromSeconds(2), () => print("star wars")),
                new DelayedJob(TimeSpan.FromSeconds(3f), () => print("is coming!"))
            };
}

...对 Update()的一些更改:

...a few alterations to Update():

void Update()
{    
    bool again;

    do
    {
        again = false;
        // you probably want to optimise this so that we don't check the same items
        // at the start again after removing an item
        foreach (var delayedJob in _jobs)
        {
            if (!delayedJob.Update())
            {
                _jobs.Remove(delayedJob);
                again = true; // start over
                break;
            }
        }
    }
    while (again);    
}

这篇关于有什么方法可以控制Unity中的时间延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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