6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity拡張】Animatorのノードを整列させる

Last updated at Posted at 2016-11-23

はじめに

AnimatorControllerを作成すると、デフォルトの状態ではノードがかなり離れた位置に作られてしまいます。
image

個人的にはこれがかなり気に入らないので、今回はこれらのノードを整列させるエディター拡張を作ってみました。

Unityのバージョンは5.5.0b7です

ソースコード

NewBehaviourScript.cs
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Reflection;

public class NewBehaviourScript
{
    [MenuItem("Assets/Format Animator")]
    static void FormatAnimator()
    {
        // Animatorウィンドウ取得
        var asm = Assembly.Load("UnityEditor.Graphs");
        var editorGraphModule = asm.GetModule("UnityEditor.Graphs.dll");
        var typeAnimatorWindow = editorGraphModule.GetType("UnityEditor.Graphs.AnimatorControllerTool");
        var animatorWindow = EditorWindow.GetWindow(typeAnimatorWindow);

        // 現在選択しているAnimatorControllerの取得
        var animator = Selection.activeObject as UnityEditor.Animations.AnimatorController;

        // ノードを整列
        animator.layers.ToList().ForEach(layer =>
        {
            Vector3 offset = new Vector3(0f, 0f, 0f);
            layer.stateMachine.anyStatePosition = offset;
            layer.stateMachine.entryPosition = offset + new Vector3(0f, 36f, 0f);
            layer.stateMachine.exitPosition = offset + new Vector3(0f, 72f, 0f);
        });

        // Animatorウィンドウのノードグラフ リセット
        animatorWindow.GetType()
        .GetMethod("RebuildGraph", BindingFlags.Public | BindingFlags.Instance)
        .Invoke(animatorWindow, null);
    }

    [MenuItem("Assets/Format Animator", true)]
    static bool ValidateFormatAnimator()
    {
        return Selection.activeObject.GetType() == typeof(UnityEditor.Animations.AnimatorController);
    }
}

上記スクリプトをプロジェクトのEditorフォルダ内へ入れてください。

動かしてみる

AnimatorControllerを右クリックして"Format Animator"を選ぶとノードが整列されます。

整列前
image

整列後
image

キレイになりました (完)

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?