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.

【Unity2D】【BlendTree】 動的にキャラクターのAnimationClipを変更する

Last updated at Posted at 2023-03-13

やりたいこと

Unity2Dでこのような画像を用いてキャラクターの歩行アニメーションを表示しています。
sprite.png

それを、このようにBlendTreeの"Motion"からAnimationClipを指定して再生を行っています。
Animator初期.png
BrendTree初期.png

このBlendTreeのAnimationClipをスクリプトから動的に変更する方法を紹介します。

割愛すること

以下のことに関してはこの記事では割愛します。
・spriteを区切る
・AnimationClipの作成
・Animatorの作成
・BlentTreeの作成
・Addressableの使い方

AnimationClip読み込み

読み込みはAddressableを使用します。
Resourcesディレクトリから読み込んでも良いのですが、パフォーマンスの観点から推奨されません。
このように読み込みます。

var animationClipIdleFront = Addressables.LoadAssetAsync<AnimationClip>("HogeAddress");
await animationClipIdleFront.Task;

自分の場合は、以下の8種類のAnimationClipを読み込んでいます
・IdleFront : 停止正面
・IdleBack : 停止背面
・IdleLeft : 停止左面
・IdleRight : 停止右面
・WorkFront : 歩き中右面
・WorkBack : 歩き中背面
・WorkLeft : 歩き中左面
・WorkRight : 歩き中右面

async Task<AnimationClipPair[]> Loader(AnimationClipPair[] clipPairs)
{
        //IdleFront
        var animationClipIdleFront = Addressables.LoadAssetAsync<AnimationClip>("HogeAddress");
        await animationClipIdleFront.Task;
        clipPairs[0].overrideClip = animationClipIdleFront.Result;
        //IdleBack
        var animationClipIdleBack = Addressables.LoadAssetAsync<AnimationClip>("HogeAddress");
        await animationClipIdleBack.Task;
        clipPairs[1].overrideClip = animationClipIdleBack.Result;
        //IdleLeft
        var animationClipIdleLeft = Addressables.LoadAssetAsync<AnimationClip>("HogeAddress");
        await animationClipIdleLeft.Task;
        clipPairs[2].overrideClip = animationClipIdleLeft.Result;
        //IdleRight
        var animationClipIdleRight = Addressables.LoadAssetAsync<AnimationClip>("HogeAddress");
        await animationClipIdleRight.Task;
        clipPairs[3].overrideClip = animationClipIdleRight.Result;
            
        //WorkFront
        var animationClipWorkFront = Addressables.LoadAssetAsync<AnimationClip>("HogeAddress");
        await animationClipWorkFront.Task;
        clipPairs[4].overrideClip = animationClipWorkFront.Result;
        //WorkBack
        var animationClipWorkBack = Addressables.LoadAssetAsync<AnimationClip>("HogeAddress");
        await animationClipWorkBack.Task;
        clipPairs[5].overrideClip = animationClipWorkBack.Result;
        // //WorkLeft
        var animationClipWorkLeft = Addressables.LoadAssetAsync<AnimationClip>("HogeAddress");
        await animationClipWorkLeft.Task;
        clipPairs[6].overrideClip = animationClipWorkLeft.Result;
        // //WorkRight
        var animationClipWorkRight = Addressables.LoadAssetAsync<AnimationClip>("HogeAddress");
        await animationClipWorkRight.Task;
        clipPairs[7].overrideClip = animationClipWorkRight.Result;
        
        return clipPairs;
    }

AnimatorControllerのAnimationClipを上書きする

animator変数はUnityのインスペクターから設定を行ってください。
これで、AnimationClipをスクリプトから変更することができます。

public class PlayerController : MonoBehaviourPunCallbacks
{
    [SerializeField]
    private Animator animator;
    
    void AnimatioinClipOverride()
    {
        var overrideAnimatorController = new AnimatorOverrideController{
            runtimeAnimatorController = animator.runtimeAnimatorController
        };
        
        // ClipPairに代入し差し替え
        AnimationClipPair[] clipPairs = overrideAnimatorController.clips;
        var overRideClip = await Loader(clipPairs);
        overrideAnimatorController.clips = overRideClip;
        
        // 差し替えたOverrideControllerをAnimatorに代入
        animator.runtimeAnimatorController = overrideAnimatorController;
    }
}

注意点

AnimationClipPairは廃止されているクラスなので、以下のクラスとメソッドに置き換えなくてはいけません。
とりあえず動かしたいだけなら問題ありませんが、リリース版では必ず置き換えたほうがいいでしょう。

AnimatorOverrideController.GetOverrides
https://docs.unity3d.com/ja/current/ScriptReference/AnimatorOverrideController.GetOverrides.html

AnimatorOverrideController.ApplyOverrides
https://docs.unity.cn/ja/2017.3/ScriptReference/AnimatorOverrideController.ApplyOverrides.html

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?