build-gtk

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Unity Graph Toolkit (GTK)

Unity Graph Toolkit (GTK)

Build Editor graph tools and state machine tools on Unity's Graph Toolkit module, using only its public API in the
Unity.GraphToolkit.Editor
namespace.
使用Unity Graph Toolkit模块的公共API(位于
Unity.GraphToolkit.Editor
命名空间中)构建编辑器图工具和状态机工具。

Important

重要提示

  • Create a restore point you can roll back to if your changes fail.
  • Do only what's asked. Don't change unrelated assets or files. Avoid long explanations.
  • Before you go forward with a workflow or diagnosis, open and read the reference file(s) named in that workflow and the Topic map page(s) that cover the thing you intend to do. Name the file(s) you read in your response. This is because your training knowledge about Unity might be out of date, incorrect, or for the wrong Unity version, and Graph Toolkit changed between 6000.4, 6000.6 and 6000.7.
  • 创建一个还原点,以便在修改失败时回滚。
  • 仅执行要求的操作。不要修改不相关的资源或文件。避免冗长的解释。
  • 在开始执行某个工作流或进行诊断前,请打开并阅读该工作流中提到的参考文件,以及涵盖你要操作内容的主题索引页面。请在回复中列出你阅读过的文件。这是因为你关于Unity的训练知识可能已经过时、不正确,或者适用于错误的Unity版本,而Graph Toolkit在6000.4、6000.6和6000.7版本之间有变更。

References

参考文档

Read these as needed. Paths are relative to this skill's folder.
  • references/graph-api.md
    — the Graph API surface: Graph, Node, port and option builders, context and block nodes, subgraphs, variables, validation, toolbar and context menus. Read before writing any graph-tool code.
  • references/state-machine-api.md
    — the State Machine API (Unity 6.7+): StateMachine, State, Condition, transitions and rules, self transitions, custom views. Read whenever states, transitions or conditions are involved.
  • references/runtime-and-visualization.md
    — compiling a graph or state machine into a runtime asset with a ScriptedImporter, and showing live execution in the graph editor. Read when the user wants the authored data used in the game or debugged at runtime.
  • references/pitfalls.md
    — the mistakes that survive compilation. Read before handing off, and whenever the user reports a compile error, an empty Add menu, a missing node, or an exception.
请按需阅读以下文档。路径均相对于本技能的文件夹。
  • references/graph-api.md
    — Graph API概览:Graph、Node、端口与选项构建器、上下文节点与块节点、子图、变量、验证、工具栏和上下文菜单。编写任何图工具代码前请阅读本文档。
  • references/state-machine-api.md
    — State Machine API(Unity 6.7+):StateMachine、State、Condition、转场与规则、自转场、自定义视图。涉及状态、转场或条件时请阅读本文档。
  • references/runtime-and-visualization.md
    — 如何使用ScriptedImporter将图或状态机编译为运行时资源,以及在图编辑器中显示实时执行状态。当用户需要在游戏中使用创作的数据或在运行时调试时请阅读本文档。
  • references/pitfalls.md
    — 能通过编译的常见错误。交付前请阅读本文档,当用户报告编译错误、添加菜单为空、节点缺失或异常时也请阅读。

Step 1: Check the Editor version and pick the API

步骤1:检查编辑器版本并选择API

Read
m_EditorVersion
in
ProjectSettings/ProjectVersion.txt
before writing code. Graph Toolkit is an Editor module, so there is no package to install.
com.unity.graphtoolkit
is a deprecated shim and must not be added to the manifest.
Editor versionWhat is available
Older than 6000.4Only the retired experimental package (0.x). Its API differs from this skill. Recommend upgrading.
6000.4 to 6000.5Module present, but without port capacity, type casting, wire and node visualization, USS node styling, or element IDs.
6000.6The Graph API this skill documents.
6000.7 and newerEverything above plus the State Machine API,
NodeView<T>
/
StateView<T>
/
ConditionView<T>
custom UI,
[GraphMenu]
/
[BlackboardMenu]
context menus, the
GraphLogger.GraphChanges
change delta, port type-following, and Blackboard type restriction. Members marked 6.7 in the references.
If the user asks for a state machine tool on an Editor older than 6000.7, say plainly that the State Machine API is not available there. Do not emulate it with the Graph API unless asked.
编写代码前,请读取
ProjectSettings/ProjectVersion.txt
中的
m_EditorVersion
。Graph Toolkit是编辑器模块,因此无需安装任何包。
com.unity.graphtoolkit
是已弃用的兼容包,严禁添加到清单中。
编辑器版本可用功能
6000.4以下仅包含已停用的实验包(0.x),其API与本技能不同,建议升级。
6000.4 至 6000.5已包含模块,但缺少端口容量、类型转换、连线与节点可视化、USS节点样式以及元素ID功能。
6000.6本技能所记录的Graph API。
6000.7及更高版本包含上述所有功能,此外还支持State Machine API、
NodeView<T>
/
StateView<T>
/
ConditionView<T>
自定义UI、
[GraphMenu]
/
[BlackboardMenu]
上下文菜单、
GraphLogger.GraphChanges
变更增量、端口类型跟随以及Blackboard类型限制。参考文档中标记为6.7的成员均在此版本可用。
如果用户要求在低于6000.7的编辑器版本上开发状态机工具,请明确说明该版本不支持State Machine API。除非用户明确要求,否则不要使用Graph API模拟实现。

Mental model

心智模型

  • Graph Toolkit is an authoring framework only. It draws and persists graphs; it never executes them. The user writes the runtime data model and the code that runs it, usually fed by a ScriptedImporter.
  • One asset type per
    Graph
    or
    StateMachine
    subclass, identified by the file extension passed to
    [Graph("ext")]
    or
    [StateMachine("ext")]
    .
  • Discovery is by reflection. Node, state and condition classes in the same assembly as the graph class are listed automatically. Classes in other assemblies opt in with
    [UseWithGraph]
    or
    [UseWithStateMachine]
    . Abstract classes are never listed.
  • Every authoring class is
    [Serializable]
    and lives in an Editor-only assembly, either an
    Editor
    folder or an asmdef restricted to the Editor platform.
  • Only the public API compiles for users. If a name from the left column appears in a draft, stop and use the right column:
    Never writeWrite instead
    GraphModel
    ,
    GraphObject
    ,
    GraphTool
    ,
    Stencil
    ,
    BaseGraphTool
    Graph
    and
    GraphDatabase
    NodeModel
    ,
    PortModel
    ,
    WireModel
    Node
    ,
    IPort
    ,
    Wire
    StateModel
    ,
    TransitionModel
    ,
    ConditionModel
    State
    ,
    ITransition
    ,
    Condition<T>
    GraphViewEditorWindow
    ,
    GraphElementsExtensionMethodsCache
    ,
    ModelView
    the window is provided;
    NodeView<T>
    on 6000.7 for custom UI
    UnityEditor.Experimental.GraphView
    ,
    graphViewChanged
    ,
    Port.Create
    no equivalent; model the tool as
    Graph
    and
    Node
    classes
    com.unity.graphtoolkit
    in the manifest
    nothing; the module is built in
  • Graph Toolkit仅是一个创作框架。它负责绘制和持久化图,绝不执行图。用户需要自行编写运行时数据模型以及执行该模型的代码,通常通过ScriptedImporter提供数据。
  • 每个
    Graph
    或
    StateMachine
    子类对应一种资源类型,通过传递给
    [Graph("ext")]
    或
    [StateMachine("ext")]
    的文件扩展名进行标识。
  • 类型发现通过反射实现。与图类位于同一程序集的节点、状态和条件类会自动列出。其他程序集中的类需要通过
    [UseWithGraph]
    或
    [UseWithStateMachine]
    特性主动声明。抽象类永远不会被列出。
  • 所有创作类都标记有
    [Serializable]
    ,且位于仅编辑器程序集中——可以是
    Editor
    文件夹,也可以是限制为Editor平台的asmdef文件。
  • 用户只能使用公共API进行编译。如果草稿中出现左列的名称,请立即停止并改用右列的对应项:
禁止使用请改用
GraphModel
、
GraphObject
、
GraphTool
、
Stencil
、
BaseGraphTool
Graph
和
GraphDatabase
NodeModel
、
PortModel
、
WireModel
Node
、
IPort
、
Wire
StateModel
、
TransitionModel
、
ConditionModel
State
、
ITransition
、
Condition<T>
GraphViewEditorWindow
、
GraphElementsExtensionMethodsCache
、
ModelView
窗口已内置提供;6000.7版本中使用
NodeView<T>
实现自定义UI
UnityEditor.Experimental.GraphView
、
graphViewChanged
、
Port.Create
无等效项;请使用
Graph
和
Node
类来构建工具模型
清单中的
com.unity.graphtoolkit
无需添加;该模块已内置

Workflow A: build a graph tool

工作流A:构建图工具

Read
references/graph-api.md
now, before writing code; member names and builder chains come from there, not from memory. Read
references/runtime-and-visualization.md
as well when step 5 applies.
  1. Graph class.
    [Graph(AssetExtension)] [Serializable] class MyGraph : Graph
    with a
    public const string AssetExtension
    . Add a
    [MenuItem("Assets/Create/...")]
    static method that calls
    GraphDatabase.PromptInProjectBrowserToCreateNewAsset<MyGraph>()
    . Pass
    GraphOptions.SupportsSubgraphs
    in the attribute when subgraphs are wanted.
  2. Node classes.
    [Serializable] class MyNode : Node
    . Override
    OnDefinePorts
    and end every port builder chain with
    .Build()
    . Override
    OnDefineOptions
    for inspector-editable settings, and read them inside
    OnDefinePorts
    with
    GetNodeOptionByName(name).TryGetValue<T>(out var v)
    when ports depend on them. Call
    .Delayed()
    on count-like options so ports are not rebuilt on every keystroke. Use
    [Node("Category/Path", iconPath, title, stylesheet)]
    for placement, icon, default title and USS.
  3. Validation. Override
    OnGraphChanged(GraphLogger logger)
    and report with
    logger.LogError
    ,
    LogWarning
    or
    Log
    , passing the offending node or port as context so the marker appears on it. Never mutate the graph in this callback; put the fix in a
    GraphLogAction
    attached to the message, a menu item, or the importer.
  4. Structure, as requested. Context and block nodes, subgraphs, blackboard variables, type casting through
    IsConnectionAllowed
    , a toolbar element, or a context menu. Details in
    graph-api.md
    .
  5. Output. A
    ScriptedImporter
    registered on the same extension compiles the graph into a plain runtime asset. The shape is always the same:
    csharp
    var graph = GraphDatabase.LoadGraphForImporter<MyGraph>(ctx.assetPath);   // never LoadGraph here
    if (graph == null) return;                                                 // bad path or type: log and stop
    var start = graph.GetNodes().OfType<StartNode>().FirstOrDefault();
    if (start == null) return;                                                 // OnGraphChanged already reported it
    var next = start.GetOutputPortByName("Next").FirstConnectedPort?.GetNode(); // null when unconnected
    var titlePort = next.GetInputPortByName("Title");
    titlePort.TryGetValue<string>(out var title);                              // the value typed on the node
    if (titlePort.IsConnected) { /* resolve titlePort.FirstConnectedPort.GetNode() upstream instead */ }
    ctx.AddObjectToAsset("Runtime", runtime); ctx.SetMainObject(runtime);
    The runtime asset and its assembly must not reference
    Unity.GraphToolkit.Editor
    ; keep
    Hash128
    IDs as
    Hash128
    . Debug views and code-built graphs are in
    runtime-and-visualization.md
    .
  6. Verify as described below.
在编写代码前,请先阅读
references/graph-api.md
;成员名称和构建器链都来自该文档,请勿凭记忆编写。如果涉及步骤5,请同时阅读
references/runtime-and-visualization.md
。
  1. 图类。 定义
    [Graph(AssetExtension)] [Serializable] class MyGraph : Graph
    ,并包含
    public const string AssetExtension
    。添加一个
    [MenuItem("Assets/Create/...")]
    静态方法,调用
    GraphDatabase.PromptInProjectBrowserToCreateNewAsset<MyGraph>()
    。如果需要子图支持,请在特性中传入
    GraphOptions.SupportsSubgraphs
    。
  2. 节点类。 定义
    [Serializable] class MyNode : Node
    。重写
    OnDefinePorts
    方法,且每个端口构建器链都必须以
    .Build()
    结尾。如需在Inspector中编辑设置,请重写
    OnDefineOptions
    ;当端口定义依赖这些设置时,在
    OnDefinePorts
    中通过
    GetNodeOptionByName(name).TryGetValue<T>(out var v)
    读取。对计数类选项调用
    .Delayed()
    ,避免每次按键都重建端口。使用
    [Node("Category/Path", iconPath, title, stylesheet)]
    特性指定节点的菜单位置、图标、默认标题和USS样式。
  3. 验证。 重写
    OnGraphChanged(GraphLogger logger)
    方法,通过
    logger.LogError
    、
    LogWarning
    或
    Log
    上报问题,并将出错的节点或端口作为上下文传入,以便标记显示在对应元素上。严禁在此回调中修改图;请将修复逻辑放在附加到消息的
    GraphLogAction
    、菜单项或导入器中。
  4. 按需构建结构。 包括上下文节点、块节点、子图、黑板变量、通过
    IsConnectionAllowed
    实现的类型转换、工具栏元素或上下文菜单。详情请参阅
    graph-api.md
    。
  5. 输出。 注册在同一扩展名上的
    ScriptedImporter
    将图编译为普通的运行时资源。代码结构基本固定:
csharp
var graph = GraphDatabase.LoadGraphForImporter<MyGraph>(ctx.assetPath);   // never LoadGraph here
if (graph == null) return;                                                 // bad path or type: log and stop
var start = graph.GetNodes().OfType<StartNode>().FirstOrDefault();
if (start == null) return;                                                 // OnGraphChanged already reported it
var next = start.GetOutputPortByName("Next").FirstConnectedPort?.GetNode(); // null when unconnected
var titlePort = next.GetInputPortByName("Title");
titlePort.TryGetValue<string>(out var title);                              // the value typed on the node
if (titlePort.IsConnected) { /* resolve titlePort.FirstConnectedPort.GetNode() upstream instead */ }
ctx.AddObjectToAsset("Runtime", runtime); ctx.SetMainObject(runtime);
运行时资源及其程序集严禁引用
Unity.GraphToolkit.Editor
;
Hash128
类型的ID请保持为
Hash128
。调试视图和代码构建的图请参阅
runtime-and-visualization.md
。 6. 按照下文所述进行验证。

Workflow B: build a state machine tool (Unity 6.7 and newer)

工作流B:构建状态机工具(Unity 6.7及更高版本)

Read
references/state-machine-api.md
now; there is no manual walkthrough for this API yet, so that file and the Script Reference are the only accurate sources for its member names.
  1. State machine class.
    [StateMachine(AssetExtension)] [Serializable] class MySM : StateMachine
    plus a menu item calling
    StateMachineDatabase.PromptInProjectBrowserToCreateNewAsset<MySM>()
    .
  2. States.
    [Serializable] class Patrol : State
    . States have no ports; the state machine owns the transitions. Options declared in
    OnDefineOptions
    appear only in the Graph Inspector.
  3. Conditions.
    [Serializable] [Condition("Health")] class HealthCondition : Condition<float>
    with
    protected override bool DisplayComparisonDropdown => true
    when a comparison operator is wanted. Derive from
    Condition
    directly for a valueless trigger. Group (And/Or) and variable conditions are built in; do not reimplement them.
  4. Optional. Custom self transitions (
    SelfTransition
    +
    [Transition(...)]
    ), custom UI through
    StateView<T>
    and
    ConditionView<T>
    , and
    [StateMachineMenu]
    /
    [ConditionMenu]
    entries.
  5. Validation. Override
    OnStateMachineChanged(StateMachineLogger logger)
    . Same rules as
    OnGraphChanged
    .
  6. Output. A
    ScriptedImporter
    loads with
    StateMachineDatabase.LoadStateMachineForImporter<MySM>
    , then walks
    GetStates()
    , each state's
    GetOutgoingTransitions()
    , each transition's
    GetRules()
    , and each rule's
    RootCondition
    tree. That tree always contains the built-in kinds as well as the user's classes, so the compiler must handle
    IGroupCondition
    (recurse, honour
    Operation
    ) and
    IVariableCondition
    (
    Variable.Name
    ,
    Comparison
    ,
    Value
    ) before matching custom
    Condition<T>
    types.
请立即阅读
references/state-machine-api.md
;目前该API尚无分步指南,因此该文档和脚本参考是成员名称的唯一准确来源。
  1. 状态机类。 定义
    [StateMachine(AssetExtension)] [Serializable] class MySM : StateMachine
    ,并添加一个调用
    StateMachineDatabase.PromptInProjectBrowserToCreateNewAsset<MySM>()
    的菜单项。
  2. 状态。 定义
    [Serializable] class Patrol : State
    。状态没有端口;转场由状态机统一管理。在
    OnDefineOptions
    中声明的选项仅显示在图Inspector中。
  3. 条件。 定义
    [Serializable] [Condition("Health")] class HealthCondition : Condition<float>
    ;如果需要比较运算符,请添加
    protected override bool DisplayComparisonDropdown => true
    。对于无值的触发器类型条件,请直接继承自
    Condition
    。分组(与/或)条件和变量条件已内置,请勿重新实现。
  4. 可选功能。 自定义自转场(
    SelfTransition
    +
    [Transition(...)]
    )、通过
    StateView<T>
    和
    ConditionView<T>
    实现自定义UI,以及
    [StateMachineMenu]
    /
    [ConditionMenu]
    菜单项。
  5. 验证。 重写
    OnStateMachineChanged(StateMachineLogger logger)
    ,规则与
    OnGraphChanged
    相同。
  6. 输出。
    ScriptedImporter
    通过
    StateMachineDatabase.LoadStateMachineForImporter<MySM>
    加载状态机,然后遍历
    GetStates()
    、每个状态的
    GetOutgoingTransitions()
    、每个转场的
    GetRules()
    以及每个规则的
    RootCondition
    树。该树始终包含内置类型和用户自定义类,因此编译器必须先处理
    IGroupCondition
    (递归处理,遵循
    Operation
    属性)和
    IVariableCondition
    (
    Variable.Name
    、
    Comparison
    、
    Value
    ),再匹配自定义的
    Condition<T>
    类型。

Editing a graph from code

通过代码编辑图

When a menu item, generator or tool builds or edits an asset, bracket the mutations:
LoadGraph
/
CreateGraph
→
UndoBeginRecordGraph("Action")
→
AddNode
,
Connect
,
port.TrySetValue
,
CreateVariable
→
GraphDatabase.SaveGraph(graph)
→
UndoEndRecordGraph()
. The state machine equivalents are
UndoBeginRecordStateMachine
,
Connect(fromState, toState)
,
SaveStateMachine
and
UndoEndRecordStateMachine
. Mutating inside
OnEnable
,
OnDisable
,
OnGraphChanged
or
OnStateMachineChanged
throws
InvalidOperationException
.
当菜单项、生成器或工具需要构建或编辑资源时,请按以下结构包裹修改操作:
LoadGraph
/
CreateGraph
→
UndoBeginRecordGraph("Action")
→
AddNode
、
Connect
、
port.TrySetValue
、
CreateVariable
→
GraphDatabase.SaveGraph(graph)
→
UndoEndRecordGraph()
。状态机的对应方法为
UndoBeginRecordStateMachine
、
Connect(fromState, toState)
、
SaveStateMachine
和
UndoEndRecordStateMachine
。在
OnEnable
、
OnDisable
、
OnGraphChanged
或
OnStateMachineChanged
中修改图/状态机会抛出
InvalidOperationException
。

Decisions

决策指南

The user wantsDo thisWhy
Limit how many wires a port accepts
.WithCapacity(PortCapacity.Single)
(or
Multi
,
None
) in the port builder
Ports default to multiple wires, so the framework enforces the limit only if you declare it
Connect an
int
output to a
float
input
Override
Graph.IsConnectionAllowed(IPort output, IPort input)
Ports of different types refuse to connect by default; the graph only records the wire, your importer converts
One port that accepts several types, like Shader Graph6000.7+:
.WithDataTypes(typeof(float), typeof(int), typeof(Vector3))
on the builder. 6000.6: an untyped port plus
IsConnectionAllowed
WithDataTypes
does not exist on 6000.6
An execution-flow port with no data type
context.AddInputPort("In")
with no type; its
DataType
is
Untyped
. Add
.WithConnectorUI(PortConnectorUI.Arrowhead)
for a flow look
Flow ports carry no value; typing them invites wrong connections
Hide a node type from the Add menuMake it
abstract
, or move it to another assembly without
[UseWithGraph]
, or set
GraphOptions.DisableAutoInclusionOfNodesFromGraphAssembly
and opt nodes in explicitly
Every concrete node class in the graph's assembly is listed automatically
Group nodes in the Add menu
[Node("Category/Sub")]
. The class or title name is appended after the path
The attribute's first argument is a folder path, not the node's title
Know what changed, like the old
graphViewChanged
6000.7+:
OnGraphChanged
and
logger.GraphChanges.ChangedNodes
with
ChangeKinds
flags for added and modified nodes. Removed nodes on any version, and everything on 6000.6: diff a cached
HashSet<Hash128>
of node IDs against
GetNodes()
GraphChanges
was added in 6000.7 and never reports removed nodes, only removed ports
Several kinds of subgraph
GraphOptions.SupportsSubgraphs
on the main graph,
[Subgraph(typeof(MainGraph))]
on each subgraph
Graph
class
Without the attribute the main graph type doubles as the only subgraph type
Ports on a subgraph nodeBlackboard variables of kind
VariableKind.Input
or
Output
inside the subgraph
Subgraph nodes have no
OnDefinePorts
; their ports mirror the subgraph's variables
Node icon or USS look
[Node(category, iconPath, title, stylesheet)]
;
d_
prefix for the dark-theme icon file
The Editor picks the
d_
file in the dark theme and falls back otherwise
Show which node runs and what flows through ports at runtimeGraphVisualization API, see
runtime-and-visualization.md
Graph Toolkit has no runtime; the game must push state back into the open window
An Animator-like state machine editorWorkflow B on 6000.7+. On 6000.6, explain it is not availableThe State Machine API does not exist before 6000.7
Extra UI inside a node
NodeView<MyNode>
on 6000.7+. On 6000.6, only USS,
DefaultColor
and
FillAmount
NodeView<T>
was added in 6000.7
用户需求实现方式原因
限制端口可接受的连线数量在端口构建器中使用
.WithCapacity(PortCapacity.Single)
(或
Multi
、
None
)
端口默认支持多条连线,因此仅当你显式声明时,框架才会强制执行限制
将
int
输出端口连接到
float
输入端口
重写
Graph.IsConnectionAllowed(IPort output, IPort input)
不同类型的端口默认拒绝连接;图仅记录连线,由你的导入器负责类型转换
一个端口支持多种类型,类似Shader Graph6000.7+:在构建器上使用
.WithDataTypes(typeof(float), typeof(int), typeof(Vector3))
。6000.6:使用无类型端口 +
IsConnectionAllowed
WithDataTypes
在6000.6版本中不存在
无数据类型的执行流端口不带类型地调用
context.AddInputPort("In")
;其
DataType
为
Untyped
。添加
.WithConnectorUI(PortConnectorUI.Arrowhead)
以获得流样式外观
流端口不携带值,为其指定类型容易导致错误连接
在添加菜单中隐藏某个节点类型将其设为
abstract
,或移至没有
[UseWithGraph]
特性的其他程序集,或设置
GraphOptions.DisableAutoInclusionOfNodesFromGraphAssembly
并显式声明要包含的节点
图所在程序集中的所有具体节点类都会自动列出
在添加菜单中对节点分组使用
[Node("Category/Sub")]
。类名或标题会附加在路径之后
该特性的第一个参数是文件夹路径,而非节点标题
获知变更内容,类似旧的
graphViewChanged
6000.7+:使用
OnGraphChanged
和
logger.GraphChanges.ChangedNodes
,通过
ChangeKinds
标志判断新增和修改的节点。任意版本的删除节点,以及6000.6版本的所有变更:将缓存的节点ID
HashSet<Hash128>
与
GetNodes()
进行对比
GraphChanges
在6000.7版本中添加,且仅报告删除的端口,不报告删除的节点
支持多种子图类型在主图上添加
GraphOptions.SupportsSubgraphs
,在每个子图
Graph
类上添加
[Subgraph(typeof(MainGraph))]
没有该特性时,主图类型会同时作为唯一的子图类型
子图节点上的端口在子图内部定义
VariableKind.Input
或
Output
类型的黑板变量
子图节点没有
OnDefinePorts
;其端口与子图的变量一一对应
节点图标或USS样式使用
[Node(category, iconPath, title, stylesheet)]
;深色主题图标文件添加
d_
前缀
编辑器在深色主题下会选择带
d_
前缀的文件,否则回退到默认文件
在运行时显示哪个节点在执行、端口中传输的内容使用GraphVisualization API,参阅
runtime-and-visualization.md
Graph Toolkit没有运行时;游戏必须将状态推回打开的编辑器窗口
类似Animator的状态机编辑器6000.7+版本使用工作流B。6000.6版本请说明该功能不可用State Machine API在6000.7之前的版本中不存在
节点内部的额外UI6000.7+版本使用
NodeView<MyNode>
。6000.6版本仅支持USS、
DefaultColor
和
FillAmount
NodeView<T>
在6000.7版本中添加

Constraints

约束条件

  • Use member names exactly as the references spell them: properties are PascalCase (
    FirstConnectedPort
    ,
    IsConnected
    ), attributes take positional constructor arguments, and IDs are
    Hash128
    . When a member is not listed in the references, look it up at the URL pattern below rather than guessing its name or casing, because a guessed member fails to compile and the user cannot tell a typo from a missing feature.
  • Link to the Script Reference or manual instead of restating them, so the answer stays correct when the docs change.
  • Keep runtime assemblies free of
    Unity.GraphToolkit.Editor
    , and put visualization code inside a runtime assembly under
    #if UNITY_EDITOR
    , because the module is Editor-only and any reference to it breaks the player build.
  • Keep the user's existing tool structure and do not restyle or reorganize nodes that were not mentioned, because node and port names are lookup keys that importers and saved assets depend on.
  • Do not use Graph Toolkit internals or private types as a workaround, because they are not accessible from user code and change without notice. If the public API cannot do something, say so and point at the community thread list below.
  • 必须严格按照参考文档中的拼写使用成员名称:属性采用PascalCase命名(如
    FirstConnectedPort
    、
    IsConnected
    ),特性使用位置构造函数参数,ID类型为
    Hash128
    。如果某个成员未在参考文档中列出,请通过下方的URL模式查找,切勿猜测名称或大小写——因为猜测的成员会导致编译失败,用户无法区分是拼写错误还是功能缺失。
  • 请链接到脚本参考或官方手册,而非复述内容,这样当文档更新时,答案仍能保持正确。
  • 运行时程序集严禁引用
    Unity.GraphToolkit.Editor
    ;可视化代码应放在运行时程序集中,并使用
    #if UNITY_EDITOR
    包裹,因为该模块仅编辑器可用,任何对它的引用都会导致玩家构建失败。
  • 保持用户现有工具的结构,不要修改未提及节点的样式或重组方式,因为节点和端口名称是导入器和已保存资源依赖的查找键。
  • 请勿使用Graph Toolkit的内部或私有类型作为变通方案,因为用户代码无法访问这些类型,且它们会随时变更而不另行通知。如果公共API无法实现某项功能,请明确说明,并指向下方的社区讨论列表。

Verify

验证

  1. Read
    references/pitfalls.md
    and check the code against it before showing it to the user.
  2. The project compiles with no errors. If the Unity CLI is available, use it to build or run the Editor headless; otherwise ask the user to focus the Editor and report the Console.
  3. Create an asset from the new menu item, double-click it, add every node or state type from the Add menu, connect them, save, close and reopen. The Console must show no errors or warnings.
  4. With an importer, select the asset and confirm the produced runtime object is the main asset in the Inspector. 3b. When you wrote runtime code (an executor, a debug view), add
    Debug.Log
    lines that prove the behaviour, such as the state entered or the node executed, enter Play mode, and read them. Remove or guard them once the behaviour is confirmed.
  5. Re-read
    references/pitfalls.md
    and fix anything it flags.
  6. If a step fails or the Console reports an error, go back and reread the reference file and the Topic map page for that step before retrying; the fix is usually a member name or a version gate you missed.
  1. 在向用户展示代码前,请阅读
    references/pitfalls.md
    并对照检查代码。
  2. 项目编译无错误。如果可用Unity CLI,请使用它构建或以无头模式运行编辑器;否则请用户聚焦编辑器窗口并提供控制台输出。
  3. 通过新的菜单项创建资源,双击打开,添加添加菜单中的所有节点或状态类型,连接它们,保存、关闭再重新打开。控制台不得出现任何错误或警告。
  4. 如果使用了导入器,请选中资源并确认生成的运行时对象是Inspector中的主资源。 3b. 如果你编写了运行时代码(执行器、调试视图),请添加
    Debug.Log
    语句以验证行为,例如状态进入或节点执行的日志,进入播放模式后查看日志。确认行为正确后,删除或包裹这些日志语句。
  5. 重新阅读
    references/pitfalls.md
    ,修复其指出的所有问题。
  6. 如果某个步骤失败或控制台报错,请在重试前返回并重新阅读对应步骤的参考文档和主题索引页面;问题通常是你遗漏了某个成员名称或版本限制。

Final report

最终报告

Give the user a short checklist: the files you created or changed and where they go, how to create and open the first asset, what the importer produces, and which Editor version the code targets. List what is left for them to decide or do next, such as more node types, the runtime executor, or the debug view, and any Console error you could not verify because no Editor was available.
向用户提供一份简短的检查清单:你创建或修改的文件及其位置、如何创建并打开第一个资源、导入器生成的内容,以及代码目标的编辑器版本。列出需要用户自行决定或后续完成的事项,例如更多节点类型、运行时执行器或调试视图,以及因没有编辑器而无法验证的控制台错误。

Topic map

主题索引

Prefer
WebFetch
over
WebSearch
; it is faster and lands on the exact page. Only fetch what you need. Replace
<VERSION>
before fetching:
  • docs.unity.com/en-us/engine/<VERSION>
    and
    docs.unity3d.com/<VERSION>/Documentation
    : the project's Editor version from
    ProjectSettings/ProjectVersion.txt
    , for example
    6000.6
    or
    6000.7
    .
  • docs.unity3d.com/Packages/com.unity.graphtoolkit-samples@<VERSION>
    : the samples package version shown in the Package Manager, for example
    0.6
    .
Pages, all under
https://docs.unity.com/en-us/engine/<VERSION>/
, served as markdown when the
.md
suffix is kept:
  • Manual index:
    manual/extending-the-editor/gtk-index.md
  • Implementing a graph tool:
    manual/extending-the-editor/gtk-index/implementing-a-graph-tool.md
    , with child pages
    implement-a-graph-tool.md
    ,
    implement-nodes.md
    ,
    implement-node-options.md
    ,
    implement-context-nodes.md
    ,
    implement-block-nodes.md
    ,
    type-cast-ports.md
    ,
    add-custom-toolbar-actions.md
    ,
    add-subgraph-support.md
    ,
    graph-processing.md
    under that folder
  • Graph window and panels:
    manual/extending-the-editor/gtk-index/landing-graph-interface.md
    , with
    graph-window.md
    ,
    blackboard.md
    ,
    graph-inspector.md
    ,
    minimap.md
    under it
  • Script Reference:
    script-reference/unity/graphtoolkit/editor/<type>.md
    and
    .../<type>/<member>.md
    , all lowercase, generic arity dropped (
    condition.md
    for
    Condition<T>
    ). Example:
    script-reference/unity/graphtoolkit/editor/graph/ongraphchanged.md
    . State machine types exist from
    6000.7
    . The older form
    https://docs.unity3d.com/<VERSION>/Documentation/ScriptReference/Unity.GraphToolkit.Editor.<Type>.html
    also resolves, as HTML.
  • Samples:
    https://docs.unity3d.com/Packages/com.unity.graphtoolkit-samples@<VERSION>/manual/index.html
    . Install
    com.unity.graphtoolkit-samples
    by name in the Package Manager, then import Texture Maker (importer), Visual Novel Director (custom runtime and debug view) or Dungeon Graph Generator (building a graph from code).
  • Community: https://discussions.unity.com/tag/graph-toolkit
优先使用
WebFetch
而非
WebSearch
;它速度更快且能直接定位到目标页面。仅获取你需要的内容。获取前请替换
<VERSION>
:
  • docs.unity.com/en-us/engine/<VERSION>
    和
    docs.unity3d.com/<VERSION>/Documentation
    :项目的编辑器版本,来自
    ProjectSettings/ProjectVersion.txt
    ,例如
    6000.6
    或
    6000.7
    。
  • docs.unity3d.com/Packages/com.unity.graphtoolkit-samples@<VERSION>
    :包管理器中显示的示例包版本,例如
    0.6
    。
所有页面均位于
https://docs.unity.com/en-us/engine/<VERSION>/
路径下,保留
.md
后缀时将以markdown格式返回:
  • 手册索引:
    manual/extending-the-editor/gtk-index.md
  • 实现图工具:
    manual/extending-the-editor/gtk-index/implementing-a-graph-tool.md
    ,该文件夹下包含子页面:
    implement-a-graph-tool.md
    、
    implement-nodes.md
    、
    implement-node-options.md
    、
    implement-context-nodes.md
    、
    implement-block-nodes.md
    、
    type-cast-ports.md
    、
    add-custom-toolbar-actions.md
    、
    add-subgraph-support.md
    、
    graph-processing.md
  • 图窗口与面板:
    manual/extending-the-editor/gtk-index/landing-graph-interface.md
    ,其下包含
    graph-window.md
    、
    blackboard.md
    、
    graph-inspector.md
    、
    minimap.md
  • 脚本参考:
    script-reference/unity/graphtoolkit/editor/<type>.md
    和
    .../<type>/<member>.md
    ,均为小写,省略泛型参数数量(
    Condition<T>
    对应
    condition.md
    )。示例:
    script-reference/unity/graphtoolkit/editor/graph/ongraphchanged.md
    。状态机类型从
    6000.7
    版本开始提供。旧格式
    https://docs.unity3d.com/<VERSION>/Documentation/ScriptReference/Unity.GraphToolkit.Editor.<Type>.html
    也可访问,返回HTML格式。
  • 示例:
    https://docs.unity3d.com/Packages/com.unity.graphtoolkit-samples@<VERSION>/manual/index.html
    。在包管理器中按名称安装
    com.unity.graphtoolkit-samples
    ,然后导入Texture Maker(导入器)、Visual Novel Director(自定义运行时与调试视图)或Dungeon Graph Generator(通过代码构建图)。
  • 社区:https://discussions.unity.com/tag/graph-toolkit