コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Animator Controller のいずれかのステートの Motion が null になっている場合に true を返す拡張メソッド

概要

using System.Linq;
using UnityEditor.Animations;

public static class AnimatorControllerExt
{
    public static bool HasNullMotion( this AnimatorController self )
    {
        foreach ( var n in self.layers )
        {
            if ( n.stateMachine.states.Any( c => c.state.motion == null ) )
            {
                return true;
            }
        }
        return false;
    }
}
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;

public static class Example
{
    [MenuItem( "Tools/Hoge" )]
    private static void Hoge()
    {
        var path = AssetDatabase.GetAssetPath( Selection.activeObject );
        var controller = AssetDatabase.LoadAssetAtPath<AnimatorController>( path );

        Debug.Log( controller.HasNullMotion() );
    }
}

f:id:baba_s:20190324142213p:plain

Animator Controller のいずれかのステートの Motion が
null になっている場合に true を返します