けいごのなんとか

Unityユーザーとしてのブログ。ギリギリ路線走ってます。

Animatorウィンドウを自作する ~背景の描画~

f:id:anchan828:20130716001553p:plain

Animatorウィンドウの背景とか、StateMachineかっこいいですよね。

この背景とStateMachineを使ってEditor拡張したいですよね。

これ、実は自分で作れます。APIも用意されています。

UnityEditor.Graphs

UnityEditor.Graphsがそうです

背景の表示

f:id:anchan828:20130716001919p:plain

見て欲しいのは「Graph」と「GraphGUI」です

GraphGUI.BeginGraphGUIとGraphGUI.EndGraphGUIの中にStateMachineを書けばOKだけど今日はここまで

using UnityEditor;
using UnityEngine;
using UnityEditor.Graphs;

public class Example : EditorWindow
{ 
    static Example example;
    Graph stateMachineGraph;
    GraphGUI stateMachineGraphGUI;

    [MenuItem("Window/Example")]
    static void Do ()
    {
        example = GetWindow<Example> ();
    }

    void OnEnable ()
    {
        if (stateMachineGraph == null) {
            stateMachineGraph = ScriptableObject.CreateInstance<Graph> ();
            stateMachineGraph.hideFlags = HideFlags.HideAndDontSave;
        }
        if (stateMachineGraphGUI == null) {
            stateMachineGraphGUI = (GetEditor (stateMachineGraph));
        }
    }

    void OnDisable ()
    {
        example = null;
    }

    void OnGUI ()
    {
        if (example && stateMachineGraphGUI != null) {
            stateMachineGraphGUI.BeginGraphGUI (example, new Rect (0, 0, example.position.width, example.position.height));
//            stateMachineGraphGUI.OnGraphGUI ();
            stateMachineGraphGUI.EndGraphGUI ();
           
        }
    }

    GraphGUI GetEditor (Graph graph)
    {
        GraphGUI graphGUI = CreateInstance ("GraphGUI") as GraphGUI;
        graphGUI.graph = graph;
        graphGUI.hideFlags = HideFlags.HideAndDontSave;
        return graphGUI;
    }
}