1 Star 1 Fork 0

凉鞋/ActionKit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ActionKit

由 QFramework 团队官方维护的独立工具包(不依赖 QFramework)。

环境要求

  • Unity 2018.4LTS

安装

快速开始

  • chainning style(Driven by MonoBehaviour or Update)
this.Sequence()
	.Delay(1.0f)
	.Event(()=>Log.I("Delayed 1 second"))
	.Until(()=>something is done)
	.Begin();
  • object oriented style
var sequenceNode = new SequenceNode();
sequenceNode.Append(DelayAction.Allocate(1.0f));
sequenceNode.Append(EventAction.Allocate(()=>Log.I("Delayed 1 second"));
sequenceNode.Append(UntilAction.Allocate(()=>something is true));

this.ExecuteNode(sequenceNode);

可执行节点系统:NodeActionSystem

NodeSystem 的设计初衷是为了解决异步逻辑的管理问题,异步逻辑在日常开发中往往比较难以管理,而且代码的风格差异很大。诸如 "播放一段音效并获取播放完成的事件","当 xxx 为 true 时触发",包括我们常用的 Tween 动画都是异步逻辑,以上的异步逻辑都可以用 ExecuteNode 来封装他们。由此设计出了 NodeSystem,灵感来自于 cocos2dCCAction

基础节点

1.延时节点: DelayNode

通过 this(MonoBehaviour) 触发延时回调。

快捷方式

this.Delay(1.0f, () =>
{
	Log.I("延时 1s");
});

面向对象

通过申请 DelayNode 对象,使用 this(MonoBehaviour) 触发延时回调。

var delay2s = DelayNode.Allocate(2.0f, () => { Log.I("延时 2s"); });
this.ExecuteNode(delay2s);

使用 Update 驱动延时回调。

Update 方式

private DelayNode mDelay3s = DelayNode.Allocate(3.0f, () => { Log.I("延时 3s"); });

private void Update()
{
	if (mDelay3s != null && !mDelay3s.Finished && mDelay3s.Execute(Time.deltaTime))
	{
		Log.I("Delay3s 执行完成");
	}
}

FeatureId:CEDN001

2.事件节点: EventNode

字如其意,**EventNode**,也就是分发事件。也许单独使用并不会发挥它的价值,但是在 **容器节点** 里他是不可或缺的。

通过申请 EventNode 对象,使用 this(MonoBehaviour) 触发事件执行。

var eventNode = EventNode.Allocate(() => { Log.I("event 1 called"); }, () => { Log.I("event 2 called"); });
this.ExecuteNode(eventNode);

使用 Update 驱动回调。

private EventNode mEventNode2 = EventNode.Allocate(() => { Log.I("event 3 called"); }, () => { Log.I("event 4 called"); });

private void Update()
{
	if (mEventNode2 != null && !mEventNode2.Finished && mEventNode2.Execute(Time.deltaTime))
	{
		Log.I("eventNode2 执行完成");
	}
}

FeatureId:CEEN001

容器节点

1.Sequence

SequenceNode 字如其意就是序列节点,是一种 容器节点 可以将孩子节点按顺序依次执行,每次执行完一个节点再进行下一个节点。

通过 this(MonoBehaviour) 触发延时回调。

this.Sequence()
	.Delay(1.0f)
	.Event(() => Log.I("Sequence1 延时了 1s"))
	.Begin()
	.DisposeWhenFinished() // Default is DisposeWhenGameObjDestroyed
	.OnDisposed(() => { Log.I("Sequence1 destroyed"); });

通过申请 SequenceNode 对象,使用 this(MonoBehaviour) 触发节点执行。

	var sequenceNode2 = SequenceNode.Allocate(DelayNode.Allocate(1.5f));
	sequenceNode2.Append(EventNode.Allocate(() => Log.I("Sequence2 延时 1.5s")));
	sequenceNode2.Append(DelayNode.Allocate(0.5f));
	sequenceNode2.Append(EventNode.Allocate(() => Log.I("Sequence2 延时 2.0s")));

	this.ExecuteNode(sequenceNode2);

	/* 这种方式需要自己手动进行销毁
	sequenceNode2.Dispose();
	sequenceNode2 = null;
	*/

	// 或者 OnDestroy 触发时进行销毁
	sequenceNode2.AddTo(this);

使用 Update 驱动执行。

private SequenceNode mSequenceNode3 = SequenceNode.Allocate(
			DelayNode.Allocate(3.0f),
			EventNode.Allocate(() => { Log.I("Sequence3 延时 3.0f"); }));

private void Update()
{
    if (mSequenceNode3 != null 
        && !mSequenceNode3.Finished 
        && mSequenceNode3.Execute(Time.deltaTime))
	{
		Log.I("SequenceNode3 执行完成");
	}
}

private void OnDestroy()
{
	mSequenceNode3.Dispose();
	mSequenceNode3 = null;
}

如何停止正在执行的 Action

using System.Collections;
using UnityEngine;

namespace QFramework.Example.ActionKit
{
    public class StopExample : MonoBehaviour
    {
        IEnumerator Start()
        {
            var delayAction = this.Delay(5, () =>
            {
                Debug.Log("Delay Action");
            });

            yield return new WaitForSeconds(2);

            if (!delayAction.Finished)
            {
                // 停止掉
                delayAction.Dispose();
                Debug.Log("暂停掉了:" + Time.time);
            }
        }
    }
}

更多

MIT License Copyright (c) 2021 liangxiegame Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
C#
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/liangxiegame/ActionKit.git
git@gitee.com:liangxiegame/ActionKit.git
liangxiegame
ActionKit
ActionKit
main

搜索帮助