0
0

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 1 year has passed since last update.

【Unity】Component が Animator から参照されているか確認する Editor 拡張

Last updated at Posted at 2023-08-14

概要

コンポーネントのコンテキストメニューから Animator の操作対象に設定されているか確認する Editor 拡張です。
output.gif

詳細

親オブジェクトに存在する Animator から AnimationClip, EditorCurveBinding にアクセスし、対象へのパス、型から操作対象になっているか確認します。

コード全文

    [MenuItem( "CONTEXT/Component/Animator Reference Search" )]
    private static void AnimatorReferenceSearch( MenuCommand menuCommand )
    {
        var component = menuCommand.context as Component;
        var animators = component.GetComponentsInParent<Animator>();

        foreach( var animator in animators )
        {
            var path = GetPath( animator.transform, component.transform );
            foreach( var clip in animator.runtimeAnimatorController.animationClips )
            {
                foreach( var binding in AnimationUtility.GetCurveBindings( clip ) )
                {
                    if( path == binding.path && component.GetType() == binding.type )
                    {
                        Selection.activeGameObject = animators.Select( a => a.gameObject ).ToArray().First();
                        EditorGUIUtility.PingObject( Selection.activeGameObject );
                        return;
                    }
                }
            }
        }

        string GetPath( Transform animatorTransform, Transform componentTransform )
        {
            if( animatorTransform == componentTransform )
            {
                return string.Empty;
            }

            var path = componentTransform.name;
            var parent = componentTransform.parent;
            while( parent != animatorTransform )
            {
                path = parent.name + "/" + path;
                parent = parent.parent;
            }

            return path;
        }
    }
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?