【Unity】使用UniRx设置延时

Unity项目中添加UniRx插件

 插件下载:https://files.cnblogs.com/files/weigangblog/UniRx.zip

工具类代码:

using System;
using UniRx;
using UnityEngine;

public static class UniRxUtils
{
    public static void Delay(float second, Action action)
    {
        Observable.Timer(TimeSpan.FromSeconds(second)).Subscribe(_ => action());
    }

    public static void Interval(float second, Action action)
    {
        Observable.Interval(TimeSpan.FromSeconds(second)).Subscribe(_ => action());
    }

    public static void Delay(float second, Action action, GameObject obj)
    {
        Observable.Timer(TimeSpan.FromSeconds(second)).Subscribe(_ => action()).AddTo(obj);
    }

    public static void Interval(float second, Action action, GameObject obj)
    {
        Observable.Interval(TimeSpan.FromSeconds(second)).Subscribe(_ => action()).AddTo(obj);
    }
}

在需要延时处理或者延时调用的地方使用如:

    private void Awake()
    {
        UniRxUtils.Delay(5, () => { Debug.Log("Start"); });
    }
原文地址:https://www.cnblogs.com/weigangblog/p/14447667.html