跳到主要内容

4. 创建自定义节点

在本篇中,我们将为 Dialogue 创建一个名为 Speak 的自定义节点。

创建 SpeakStep

我们回到 Assets/Scripts/Example.Runtime.DialogueManagement 目录下,新建一个名为 Steps 的文件夹来存放自定义的 Step。 进入 Steps 文件夹,创建一个名为 SpeakStep 的脚本并修改为以下内容。

Assets/Scripts/Example.Runtime.DialogueManagement/Steps/SpeakStep.cs
using System.Collections;
using winS.Sequencing.Process;
using winS.Unity.Process;

namespace Example.Runtime.DialogueManagement
{
public class SpeakStep : GeneralStepWithCoroutine
{
public string role;
public string content;

public override IEnumerator Execute(ProcessContext processContext)
{
yield break;
}
}
}

创建 SpeakStepNode

为了让 SpeakStep 可以被编辑器,我们需要创建对应的 Node 类型。

我们回到 Assets/EditorExtension/Example.Editor.DialogueEditor 目录下,新建一个名为 Nodes 的文件夹来存放自定义的 Node。 进入 Nodes 文件夹,创建一个名为 SpeakStepNode 的脚本并修改为以下内容。

Assets/EditorExtension/Example.Editor.DialogueEditor/Nodes/SpeakStepNode.cs
using Example.Runtime.DialogueManagement;
using winS.UnityEditor.ProcessEditor;

namespace Example.Editor.DialogueEditor
{
[StepNodeSearchTreeGroup("自定义节点/说话"), CustomProcess(typeof(Dialogue))]
public class SpeakStepNode : GeneralStepNode<SpeakStep>
{
public override string title => "说话";

protected override void OnCreated()
{
Add(guiFactory.CreateTextField("角色名称", step.role, newValue => step.role = newValue));
Add(guiFactory.CreateTextField("说话内容", step.content, newValue => step.content = newValue));
}
}
}