QF框架使用之案例解析(四) ------ NodeActionSystem
QFramework 是一套 渐进式 的 快速开发 框架。目标是作为无框架经验的公司、独立开发者、以及 Unity3D 初学者们的 第一套框架。框架内部积累了多个项目的在各个技术方向的解决方案。学习成本低,接入成本低,重构成本低,二次开发成本低,文档内容丰富(提供使用方式以及原理、开发文档)。github:https://github.com/liangxiegame/QFramework
今天分享的是QF的NodeActionSystem,原先没有运行示例直接看官方文章是有点懵的,运行了之后发现真实用
官方文章 : http://qframework.io/Assets/QFramework/Framework/1.ActionKit/Document.html
案例地址 : QFramework\Assets\QFramework\Example\ActionKitExample
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 驱动延时回调。
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 执行完成");
}
}
灰常好用啊有木有
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 执行完成");
}
}
3.容器节点
①.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;
}
4.Timer
运行案例:
瞅瞅代码:
private void OnGUI()
{
if (GUILayout.Button("开启定时"))
{
text3.text = "";
if (!Timer.Exist(timer))
{
timer = Timer.AddTimer(5, "12138").OnUpdated((v) =>
{
image.fillAmount = v;
text.text = (v * timer.Duration).ToString();
}).OnCompleted(
() =>
{
text3.text = "计时完成!";
}
);
text2.text = "当前设定时间为:" + timer.Duration;
}
}
if (GUILayout.Button("定时2秒"))
{
if (Timer.Exist("12138"))
{
Timer.GetTimer("12138").SetTime(2); //当定时器走了大于2秒时按下,会提示错误,并瞬间完成事件
text2.text = "当前设定时间为:" + timer.Duration; //当定时器走了小于2秒时按下,会加速完成
}
}
if (GUILayout.Button("定时-2秒")) //当定时器走了大于2秒时按下,会提示参数必须为正值,并瞬间完成事件
{ //当定时器走了小于2秒时按下,会提示参数必须为正值,会加速完成
if (Timer.Exist("12138"))
{
Timer.GetTimer("12138").SetTime(-2);
text2.text = "当前设定时间为:" + timer.Duration;
}
}
if (GUILayout.Button("定时10秒")) //当定时器走了小于2秒时按下,会提示参数必须为正值,会加速完成
{
if (null != timer)
{
timer.SetTime(10);
text2.text = "当前设定时间为:" + timer.Duration;
}
}
}
实用很简单~
好啦,分享结束