Skip to content

Operating Guide

EMT edited this page Dec 26, 2024 · 1 revision

节点菜单

在继承EditorUniversalGraphAsset时菜单的构建有两种
通过NodeToEditorAttribute或NodeToRuntimeAttribute特性决定菜单的构建方式

在使用NodeToEditorAttribute时是根据编辑器节点来进行构建菜单
baseEditorNodeType用来指定编辑器节点的基类

在使用NodeToRuntimeAttribute时是根据运行时的节点来构建菜单
baseRuntimeNodeType指定运行时节点的基类 baseEditorNodeType为所有运行时节点指定一个编辑器的节点基类

如果有对特定的运行时节点重写编辑器节点类那么则用重写的如果没有则默认使用所指定的编辑器节点基类

例1

在写状态机时运行时只有一个节点,那就是状态节点,但编辑器中想要有多个不同的节点(进入,退出,任意)这时候就使用NodeToEditorAttribute特性去使用多个编辑器节点来构建不同的运行时节点

[NodeToEditor(typeof(StateMachineNodeAsset))]
public abstract class EditorStateMachineAsset : EditorUniversalGraphAsset
{
...
}

详细可以去SpineAnimator查看

例2

在写流图时因为运行时节点会不断进行拓展,并且编辑器样式都是大同小异的,这时候就使用NodeToRuntimeAttribute特性,就算遇到特殊节点也可以单独去重写特殊节点的编辑器实现

[NodeToRuntime(typeof(FlowNodeAsset), typeof(EditorFlowNodeAsset))]
public abstract class EditorFlowAsset : EditorUniversalGraphAsset
{
    ...
}

详细可以去Statescript查看

运行时节点指定编辑器节点

通过继承CreateNodeHandle进行实现

public class TestCreateNodeHandle : CreateNodeHandle<TestAsset>//指定运行时节点类型
{
    protected object _userData;
    protected string _path;
    protected int _priority;

    public override object userData => _userData;
    public override string path => _path;
    public override int priority => _priority;
    public override Type editorNodeType => typeof(EditorCompositeNodeAsset);//指定编辑器节点类型

    public override void Initialize(object weakSmartValue)
    {
        base.Initialize(weakSmartValue);

        TestMenuAttribute menuAttribute = this.value.nodeType.GetCustomAttribute<TestMenuAttribute>();
        if (menuAttribute != null)
        {
            this._path = menuAttribute.path;
            this._priority = menuAttribute.priority;
        }

        _userData = ReflectUtility.CreateInstance(this.value.nodeType);
    }
}

界面

继承GraphPanelHandle来控制界面的加载

public class TestGraphPanelHandle : GraphPanelHandle<TestAsset>
{
    public override void LoadPanel(GraphPanelSystem system)
    {
        //在这里添加想要添加的Panel
    }
}

工具栏

public class TestToolbarView : ToolbarView
{
    protected override void InitControls()
    {
        //添加Label控件并设置为右边
         AddControl(new LabelToolbarViewControl("TestLabel"), ToolbarViewControlPosition.RightOrBottom);
        
        //添加按钮控件
        AddControl(new ButtonToolbarViewControl("TestButton", () => Debug.Log("Test Click")));
        
        //添加自定义GUI控件
        AddControl(new CustomToolbarViewControl(() => {

            if (GUILayout.Button("Custom Button"))
            {
                Debug.Log("Test Click");
            }
            
        }));
    }
}

打开方式

public class TestGraphPanelHandle : GraphPanelHandle<TestAsset>
{
    public override void LoadPanel(GraphPanelSystem system)
    {
        //打开停靠在边缘的面板
        system.OpenDockPanel<TestToolbarView>(20, GraphDockPosition.Top);//宽度为20,设置停靠位置为顶部(Top)
    }
}

在EditorGraphAsset中使用特性ToolbarAttribute也可以添加控件到工具栏中

[CreateAssetMenu(menuName = "Emilia/Examples/Test"), NodeToEditor(typeof(TestBaseNodeAsset))]
public class TestAsset : EditorUniversalGraphAsset
{
    [ButtonToolbar("测试", ToolbarViewControlPosition.RightOrBottom)]
    public void TestButton()
    {
        Debug.Log("Test");
    }
}

层级栏

public class TestGraphPanelHandle : GraphPanelHandle<TestAsset>
{
    public override void LoadPanel(GraphPanelSystem system)
    {
        system.OpenDockPanel<LayerView>(22, GraphDockPosition.Top);
    }
}

小地图

public class TestGraphPanelHandle : GraphPanelHandle<TestAsset>
{
    public override void LoadPanel(GraphPanelSystem system)
    {
        //打开为浮动的面板
        system.OpenFloatPanel<MiniMapView>();
    }
}

操作菜单

方法1:在EditorGraphAsset中使用特性MenuAttribute

[CreateAssetMenu(menuName = "Emilia/Examples/Test"), NodeToEditor(typeof(TestBaseNodeAsset))]
public class TestAsset : EditorUniversalGraphAsset
{
    [Menu("测试", 0)]
    public void OperateMenu()
    {
        Debug.Log("Test");
    }
}

方法2:继承OperateMenuHandle

public class TestOperateMenuHandle : OperateMenuHandle<TestAsset>
{
    public override void InitializeCache()
    {
        base.InitializeCache();

        GeneralOperateMenuAction action = new GeneralOperateMenuAction();
        OperateMenuActionInfo actionInfo = action.ToActionInfo("Test", "", 0);
        smartValue.operateMenu.actionInfoCache.Add(actionInfo);
    }
}

布局

LayoutUtility.RunLayout(EditorGraphView)
Clone this wiki locally