Saturday, August 1, 2015

Reveal unknown Graph and GraphGUI class in Unity3d

Your Mecanim Editor is done by help of code in namespace UnityEditor.Graphs.AnimationStateMachine.


Extend GraphGUI found in UnityEditor.Graphs. This class is responsible for drawing graph.
using System;
using UnityEditor;
using UnityEngine;
using UnityEditor.Graphs;
using System.Collections.Generic;

namespace ws.winx.editor.components
{
 public class GraphGUIEx:GraphGUI{


 }

}
Create EditorWindow
public class GraphEditorWindow : EditorWindow
 { 
  static GraphEditorWindow graphEditorWindow;
  Graph stateMachineGraph;

  GraphGUIEx stateMachineGraphGUI;
  
  [MenuItem("Window/Example")]
  static void Do ()
  {
   graphEditorWindow = GetWindow<grapheditorwindow$gt; ();
  }

....
Create Graph structure. It will contains nodes and edges between nodes.
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);
Create new GraphGUIEx instance and attach graph that should be drawn.
GraphGUIEx graphGUI = ScriptableObject.CreateInstance<GraphGUIEx>();
graphGUI.graph = graph;
Draw Graph.
void OnGUI ()
  {
 
   if (graphEditorWindow && stateMachineGraphGUI != null) {
    stateMachineGraphGUI.BeginGraphGUI (graphEditorWindow, new Rect (0, 0, graphEditorWindow.position.width, graphEditorWindow.position.height));
               stateMachineGraphGUI.OnGraphGUI ();

 
    stateMachineGraphGUI.EndGraphGUI ();
    
   }
  }
Override NodeGUI or EdgeGUI for more styling and drawing control or copy styling from Your MecanimEditor is in namespace UnityEditor.Graphs.AnimationStateMachine.GraphGUI to create Mecanim look like Graph.

1 comment: