你错了伙计。您在 UnityEditor 中看到的所有内容都必须在某处有代码。您的 MecanimEditor 在命名空间 UnityEditor.Graphs.AnimationStateMachine 中。
扩展 UnityEditor.Graphs 中的 GraphGUI。该类负责绘制图形。
using System;
using UnityEditor;
using UnityEngine;
using UnityEditor.Graphs;
using System.Collections.Generic;
namespace ws.winx.editor.components
{
public class GraphGUIEx:GraphGUI{
}
}
创建新的编辑器窗口。
public class GraphEditorWindow : EditorWindow
{
static GraphEditorWindow graphEditorWindow;
Graph stateMachineGraph;
GraphGUIEx stateMachineGraphGUI;
[MenuItem("Window/Example")]
static void Do ()
{
graphEditorWindow = GetWindow<grapheditorwindow> ();
}
....
创建图结构。它将包含节点和节点之间的边。
stateMachineGraph = ScriptableObject.CreateInstance<Graph> ();
stateMachineGraph.hideFlags = HideFlags.HideAndDontSave;
//create new node
Node node=ScriptableObject.CreateInstance<Node>();
node.title="mile2";
node.position=new Rect(400,34,300,200);
node.AddInputSlot("input");
start=node.AddOutputSlot("output");
node.AddProperty(new Property(typeof(System.Int32),"integer"));
stateMachineGraph.AddNode(node);
//create new node
Node node=ScriptableObject.CreateInstance<Node>();
node.title="mile";
node.position=new Rect(0,0,300,200);
Slot end=node.AddInputSlot("input");
node.AddOutputSlot("output");
node.AddProperty(new Property(typeof(System.Int32),"integer"));
stateMachineGraph.AddNode(node);
//create edge
stateMachineGraph.Connect(start,end);
graphGUI = ScriptableObject.CreateInstance<GraphGUIEx>();
graphGUI.graph = graph;
绘制图形。
void OnGUI ()
{
if (graphEditorWindow && stateMachineGraphGUI != null) {
stateMachineGraphGUI.BeginGraphGUI (graphEditorWindow, new Rect (0, 0, graphEditorWindow.position.width, graphEditorWindow.position.height));
stateMachineGraphGUI.OnGraphGUI ();
stateMachineGraphGUI.EndGraphGUI ();
}
}
覆盖 NodeGUI 或 EdgeGUI 以获得更多样式和绘图控制。从 UnityEditor.Graphs.AnimationStateMachine.GraphGUI 样式复制粘贴代码,在 NodeGUI 和 EdgeGUI 中完成。