Unity SKFramework框架(十一)、ActionChain 事件链

目录

一、Action 事件

1.Simple 普通事件

2.Delay 延迟事件

3.Timer 定时事件

4.Until 条件事件

5.While 条件事件

6.Tween 动画事件

7.Animate 动画事件

8.Timeline 时间轴事件

二、Action Chain 事件链

1.Timeline 时间轴事件链

2.Sequence 序列事件链

3.Concurrent 并发事件链

4.事件链嵌套


一、Action 事件

        框架内置了八种类型的事件,分别是Simple普通事件Delay延迟事件Timer定时事件Until条件事件While条件事件Tween动画事件Animate动画事件Timeline时间轴事件。也可以通过继承AbstractAction抽象事件类,重写OnInvokeOnReset函数来自定义事件。下面是内置的八种事件的介绍:

1.Simple 普通事件

普通事件是最简单的,可以理解为一个简单的Action回调函数。

2.Delay 延迟事件

延迟事件需要指定一个时长,在经过该时长后执行指定的回调函数。

3.Timer 定时事件

定时事件可以理解为定时器,分为正计时和倒计时,通过参数isReverse指定,事件为Action<float>类型,通过已经计时的时长(正计时)或剩余的时长(倒计时)调用执行。

4.Until 条件事件

Until条件事件需要指定一个Func<bool>条件,表示直到该条件成立时,调用回调函数,事件结束。

5.While 条件事件

While条件事件同样需要指定一个Func<bool>条件,与Until条件事件不同的是,While条件事件中设置的回调函数在条件成立时一直被调用,当条件不再成立时,事件结束。

6.Tween 动画事件

框架中集成了DoTween插件,Tween事件表示的是播放一个DoTween动画,动画播放完后,事件结束。

7.Animate 动画事件

Animate动画事件指的是通过Animator播放动画,需要指定Animator组件和Animator Controller中动画状态State的名称,动画播放完后,事件结束。

8.Timeline 时间轴事件

时间轴事件需要指定事件开始的时间节点,和事件执行的时长,需要配合Timeline ActionChain时间轴事件链使用。

二、Action Chain 事件链

        事件链包含三种类型,分别是Timeline时间轴事件链Sequence序列事件链Concurrent并发事件链,均继承自IActionChain接口,包含Begin启动Pause暂停Resume恢复Stop终止函数

1.Timeline 时间轴事件链

        事件链的执行依赖于携程,通过this获取事件链表示以当前的MonoBehaviour开启携程,也可以通过ActionChain获取事件链,表示以ActionChain的管理类开启携程,如下所示:

//通过当前MonoBehaviour获取事件链
IActionChain chain1 = this.Timeline();
//通过ActionChain获取事件链
IActionChain chain2 = ActionChain.Timeline();

时间轴事件链中包含的核心变量如下:

(1).CurrentTime 当前执行的时间节点

(2).Speed 执行的速度

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    [SerializeField] private GameObject cube;
    [SerializeField] private GameObject sphere;

    private TimelineActionChain timeline;

    private void Start()
    {
        timeline = this.Timeline()
            //通过Append添加时间轴事件
            //第一个参数表示该事件开始的时间节点
            //第二个参数表示该事件的时长
            .Append(0f, 5f, s => cube.transform.position = Vector3.Lerp(Vector3.zero, new Vector3(0, 0, 5f), s))          
            .Append(2f, 4f, s => sphere.transform.position = Vector3.Lerp(Vector3.zero, Vector3.up * 2f, s))
            .Begin() as TimelineActionChain;

        //2倍速
        timeline.Speed = 2f;
    }

    private void OnGUI()
    {
        GUILayout.BeginHorizontal();
        GUILayout.Label("时间轴");
        //通过Slider调整CurrentTime 实现从指定的时间节点执行
        timeline.CurrentTime = GUILayout.HorizontalSlider(timeline.CurrentTime, 0f, 6f, GUILayout.Width(300f), GUILayout.Height(50f));
        GUILayout.EndHorizontal();
    }
}

2.Sequence 序列事件链

        序列事件链中的事件是依次执行的,即只有上一个事件执行结束后,才会开始执行下一个事件。如下所示,事件链中包含三个事件,Event指的是Simple普通事件,该事件链首先打印日志Begin,第一个事件结束,第二个事件开始执行,为延迟事件,在经历3秒的延迟后,第二个事件结束,第三个事件开始执行,打印日志"3f"。

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        this.Sequence()
            .Event(() => Debug.Log("Begin"))
            .Delay(3f)
            .Event(() => Debug.Log("3f"))
            .Begin();
    }
}

其他一些事件的使用示例如下:

using UnityEngine;
using DG.Tweening;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        this.Sequence()
            //普通事件
            .Event(() => Debug.Log("Begin"))
            //延迟2秒
            .Delay(2f)
            //普通事件
            .Event(() => Debug.Log("2f"))
            //直到按下键盘A键
            .Until(() => Input.GetKeyDown(KeyCode.A))
            //普通事件
            .Event(() => Debug.Log("A Pressed."))
            //DoTween动画事件
            .Tween(() => transform.DOMove(new Vector3(0f, 0f, 1f), 2f))
            //定时事件
            .Timer(3f, false, s => Debug.Log(s))
            .Begin()
            .OnStop(() => Debug.Log("Completed"));
    }
}

3.Concurrent 并发事件链

        并发事件链中的事件是并发执行的,在事件链启动时同时开启执行,在所有的事件都执行完成后,事件链终止。

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        this.Concurrent()
            .Event(() => Debug.Log("Begin"))
            .Delay(1f, () => Debug.Log("1f"))
            .Delay(2f, () => Debug.Log("2f"))
            .Delay(3f, () => Debug.Log("2f"))
            .Until(() => Input.GetKeyDown(KeyCode.A))
            .Begin()
            .OnStop(() => Debug.Log("Completed"));
    }
}

4.事件链嵌套

事件链之间支持互相嵌套,如下所示:

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        this.Sequence()
            .Event(() => Debug.Log("Begin"))
            //嵌套一个并发事件链
            .Append(new ConcurrentActionChain()
                .Delay(1f, () => Debug.Log("1f"))
                .Delay(2f, () => Debug.Log("2f"))
                .Delay(3f, () => Debug.Log("3f"))
                as IAction)
            //并发事件链执行完成后 继续执行序列事件链
            .Until(() => Input.GetKeyDown(KeyCode.A))
            .Event(() => Debug.Log("A Pressed."))
            .Timer(3f, false, s => Debug.Log(s))
            .Begin()
            .OnStop(() => Debug.Log("Completed."));
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42139931/article/details/124925366