简易协程工具【Wait! - Easy Coroutines】

Wait! - Easy Coroutines | Utilities Tools | Unity Asset StoreUse the Wait! - Easy Coroutines from Iterant Games on your next project. Find this utility tool & more on the Unity Asset Store.https://prf.hn/l/b3AQw5a

1、概述

Wait - Easy Coroutines!是一个Unity协程助手,它允许您使用声明性语法编写协程,而不需要IEnumerator或yield语法来完成简单的定时任务。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait.Seconds(10,() => {Debug.Log("Waited 10 seconds");}).Start();
    }
}

2、特性

  • 等待Frames、Seconds、SecondsByFrame,或者直到满足指定的条件。
  • 重复你的等待多次或无限期。
  • 容易锁在一起等待。(如。等待一个条件完成后5秒)
  • 组等待一起停止、暂停或恢复它们。
  • 指定在启动等待时运行的操作。
  • 在任何脚本中运行协程,而不仅仅是MonoBehaviors

3、安装

它是免费的!所以只要从Unity Asset Store中抓取它并添加“using SL.Wait;”到任何脚本。

4、依赖

 插件依赖:More Effective Coroutines [FREE]

5、介绍

wait.Start():这个方法需要被调用来开始等待。如果不调用它,那么什么也不会发生。它通常用作Wait设置结束时的链式函数调用。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait.Seconds(10,() => {Debug.Log("Waited 10 seconds");}).Start();
    }
}

wait.Start() - delayed:您可以保存Wait对象并在以后启动它,而不需要立即启动它。这对于链式或分组等待特别方便。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing = Wait.Seconds(10, () => { Debug.Log("Waited 10 seconds"); });
        timing.Start();
    }
}

wait.Stop():您可以通过在实例上调用stop来提前停止等待。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing = Wait.Seconds(10, () => { Debug.Log("Waited 10 seconds"); });
        timing.Start();
        timing.Stop();
    }
}

wait.Pause():您可以通过在实例上调用pause来暂停运行中的等待。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing = Wait.Seconds(10, () => { Debug.Log("Waited 10 seconds"); });
        timing.Start();
        timing.Pause();
    }
}

wait.Resume():您可以通过在实例上调用resume来恢复检查等待。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing = Wait.Seconds(10, () => { Debug.Log("Waited 10 seconds"); });
        timing.Start();
        timing.Pause();
        timing.Resume();
    }
}

静态构造函数【Static Constructors】:有四个静态构造函数可以帮助清理语法。它们的功能与实例构造函数相同,只是看起来更好。下面是等效的实例语法。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing01 = Wait.Frames(10, () => { Debug.Log("timing01"); });
        //使用Unity Time,当游戏暂停时暂停执行。
        Wait timing02 = Wait.Seconds(10, () => { Debug.Log("timing02"); });
        //Unity Time暂停也运行。
        Wait timing03 = Wait.SecondsWhilePaused(10, () => { Debug.Log("timing03"); });
        Wait timing04 = Wait.For(() => { return true; }, () => { Debug.Log("timing04"); });

        timing01.Start();
        timing02.Start();
        timing03.Start();
        timing04.Start();
    }
}

实例【Instances】:

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing01 = new Wait().ForFrames(10).Then(() => { Debug.Log("timing01"); });
        Wait timing02 = new Wait().ForSeconds(10).Then(() => { Debug.Log("timing02"); });
        Wait timing03 = new Wait().ForSecondsByFrame(10).Then(() => { Debug.Log("timing03"); });
        Wait timing04 = new Wait().Until(() => { return true; }).Then(() => { Debug.Log("timing04"); });

        timing01.Start();
        timing02.Start();
        timing03.Start();
        timing04.Start();
    }
}

链式等待【Chained Waits】,但是不建议这么干:该语法允许您链接多个等待类型,但它将只使用指定的最后一个。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        // ForSeconds()将覆盖等待类型,而不是使用Frames。
        // Then()将覆盖构造函数。
        Wait timing = Wait.Frames(300, () => 
        {
            Debug.Log("这不会运行,它已被下一个Then()调用覆盖。");
        }).ForSeconds(10).Then(() => 
        {
            Debug.Log("等待10秒,而不是300帧。");
        });
        timing.Start();
    }
}

Wait.Frames():在执行函数之前等待指定的帧数。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait.Frames(300, () =>
        {
            Debug.Log("Waited 300 frames");
        }).Start();
        Wait.Frames(300).Then(() =>
         {
            Debug.Log("Waited 300 frames");
         }).Start();
    }
}

Wait.Seconds():在执行函数之前等待指定的秒数。使用内部计时方法,当游戏暂停时停止。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait.Seconds(10, () =>
        {
            Debug.Log("Waited 10 Seconds");
        }).Start();
        Wait.Seconds(10).Then(() =>
         {
            Debug.Log("Waited 10 Seconds");
         }).Start();
    }
}

Wait.SecondsWhilePaused():在执行函数之前等待指定的秒数。这将检查每一帧是否经过时间,所以即使游戏暂停,它也会运行。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait.SecondsWhilePaused(10,
     () => {
         Debug.Log("Waited 10 seconds");
     }).Start();
        Wait.SecondsWhilePaused(10).Then(
    () => {
        Debug.Log("Waited 10 seconds");
    }).Start();
    }
}

Wait.For():在运行给定函数之前,等待给定条件得到满足。这将检查条件每帧,这取决于你保持检查的合理性和性能。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait.For(() => { return true; }, () => { Debug.Log("等待直到条件返回true"); }).Start();
        Wait.For(() => { return true; }).Then(() => { Debug.Log("等待直到条件返回true"); }).Start();
    }
}

wait.Until():向任何等待添加条件的实例版本。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing = new Wait().Until(() =>
        {
            return true;
        }).Then(() =>
        {
            Debug.Log("等待直到条件返回true");
        }).Start();
    }
}

wait.Repeat():重复等待指定次数。如果输入0,它将无限重复。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait.Seconds(2,
     () =>
     {
         // 这将输出5次,每2秒。
         Debug.Log("Waited 2 seconds");
     }
    ).Repeat(5).Start();
    }
}

Repeat Indefinitely:重复下去,将0传递给Repeat()将无限重复。您可以在某个时候调用timing.Stop(),无论是在等待函数中还是在其他任何地方。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing = Wait.Seconds(2);
        timing.Then(() =>
        {
            //if (true)
            //{
            //    timing.Stop();
            //}
            // This will output every 2 seconds until .Stop() is called.
            Debug.Log("Waited 2 seconds");
        });
        // Repeat indefinitely
        timing.Repeat(0);
        timing.Start();
    }
}

wait.Chain():链式,在当前对象完成后运行第二个Wait对象。与其他方法不同,您可以在同一个Wait对象上任意多次调用。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait next_wait = Wait.Frames(600, () => { Debug.Log("等待3秒后等待600帧"); });
        Wait chain_wait = Wait.Seconds(3, () => { Debug.Log("等待3秒,链接到next_wait"); }).Chain(next_wait).Start();
    }
}

Chain Sequence:链序列,按顺序运行多个Wait对象。与其他方法不同,您可以在同一个Wait对象上任意多次调用. chain(),而无需重写。链式等待将按照您使用. chain()添加它们的顺序依次运行。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing_1 = Wait.Frames(600, () => 
        { Debug.Log("等待3秒,然后600帧-链接到next_wait"); });
        Wait timing_2 = Wait.Seconds(5, () => 
        { Debug.Log("等了3秒,然后是600帧,然后是5秒."); });
        Wait chain_wait = Wait.Seconds(3, () => 
        { Debug.Log("等待了3秒-链接到next_wait"); }).Chain(timing_1).Chain(timing_2).Start();
    }
}

Chain Function:链式函数,这将在. then()函数完成之后,与. then()函数在同一帧中运行链接函数。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait chain_wait = Wait.Seconds(3,
            () => { Debug.Log("Waited 3 seconds, chaining to next_wait"); }).Chain(
            () => { Debug.Log("Runs after the main Wait function."); }).Start();
    }
}

wait.Tag():默认情况下,Wait对象使用随机标识。您可以更改标记,以便您可以通过WaitManager引用它,而不需要保存实例。只有公开的等待才能在WaitManager中访问。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    string tagName = "myTiming";
    private void Start()
    {
        Wait.For(() => { return true; },
            () => { Debug.Log("***"); }).Tag(tagName).Start();
        // 在不传递实例的情况下从任何地方停止。
        WaitManager.StopTag(tagName);
    }
}

wait.Group():默认情况下,Wait对象不在任何组中。您可以通过传入任何字符串将其添加到组中。然后,您可以通过WaitManager同时停止、暂停和恢复所有分组对象。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        string groupName = "characterGroup";
        Wait.For(
         () => { return true; },
        () => { Debug.Log("***"); }
        ).Group(groupName).Start();
        Wait.Seconds(10,
         () => { Debug.Log("***"); }
        ).Group(groupName).Start();
        // 在不传递实例的情况下从任何地方停止。
        WaitManager.StopGroup(groupName);
    }
}

Wait Manager:等待管理器是一个静态类,它允许您更容易地访问当前等待的wait对象。您可以通过标记,分组或抓取每一个来停止,暂停或恢复它们来访问它们。

WaitManager.Stop():Stop all active Wait coroutine.

WaitManager.Pause() :Pause all active Wait coroutines.

WaitManager.Resume() :Resume all active Wait coroutines.

WaitManager.StopTag(): Stop the Wait coroutine with the specified tag.

WaitManager.PauseTag() :Pause the Wait coroutine with the specified tag.

WaitManager.ResumeTag():Resume the Wait coroutine with the specified tag.

WaitManager.StopGroup():Stop all active Wait coroutines in the given group.

WaitManager.PauseGroup(): Pause all active Wait coroutines in the given group.

WaitManager.ResumeGroup(): Resume all active Wait coroutines in the given group.

wait.OnStart():每当. start()被调用时运行指定的函数。这对于在一个地方定义并从其他地方开始的Wait对象非常有用。

using UnityEngine;
using SL.Wait;

public class Test : MonoBehaviour
{
    private void Start()
    {
        Wait timing = Wait.Seconds(2, () =>
        {
            // 这将输出5次,每2秒。
            Debug.Log("Waited 2 seconds");
        }
        ).OnStart(() =>
        {
            // 这只会在调用Start()时输出一次。
            Debug.Log("Coroutine Started!");
        }).Repeat(5);
        // 别的地方
        timing.Start();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值