![見出し画像](https://assets.st-note.com/production/uploads/images/55045332/rectangle_large_type_2_29ee2b27661de9dcff59d518dd436320.png?width=1200)
Unityアニメーションバンドルアセットの中身を素早く確認するためのAnimatorController切り替えスクリプト
Unityのアセットストアから購入したアニメーションバンドル品。
大量にアニメーションが入っているのはよいが、それを自身が使いたい3Dモデルにあてはめてどんな動きになるか確認したい場合、1個ずつアニメーションをオブジェクトにアタッチして……という作業が面倒くさい。
なので楽に確認する方がないか調べてシンプルなスクリプトを書いた。
仕様
右クリックを押すと登録したアニメーターコントローラーが順々に切り替わっていく ※Humanoid型限定 ※ついでにログもだす。
環境 Win10 + Unity 2019.4.22f1 (64-bit)
スクリプトソース
ChangeAcon.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeAcon : MonoBehaviour
{
[SerializeField]RuntimeAnimatorController[] controller;
private int index = 0;
private int a_max = 0;
Animator animator;
void Start()
{
animator = this.GetComponent<Animator> ();
a_max = controller.Length;
}
void Update()
{
if(Input.GetButtonDown("Fire2")){//右クリック押すごとにコントローラーをチェンジ
animator.runtimeAnimatorController = controller[index];
Debug.Log(controller[index].name);//Debugコンソールに要素名出力
index++;
if(index == a_max){index = 0;}
}
}
}
※エラー処理を入れていないシンプルな例
スクリプト使用方法
アニメーションを切り替えたいオブジェクト(Animatorが存在するオブジェクト)のコンポーネントにアタッチするだけ。アタッチしたら、そのコンポーネントのControllerのパラメータに確認したいアニメーターコントローラーを一括ドラック&ドロップ。
下図の辺りに落とすと一括で登録できる。
インスペクターウィンドウをロックしておくとやりやすい。
今回は右クリックを押すとアニメーションコントローラーが切り替わる仕様だけど用途に応じてカスタムしていく。
使用するとこんな感じ。
Humanoid型3Dモーションバンドル系Unityアセットの
— 梅しらす@Arcanite Links (@ume_white) June 17, 2021
モーションを手っ取り早く確認する用
Animator"Controller"切り替えスクリプト作成した。
スクリプトソースとかは後でまとめる。#unity初心者 向け。こういうやつ↓ pic.twitter.com/AVIcEP2w3N
アニメーションの初期座標へのリセット考慮すると(初期座標x,y,z=0,0,0とする)
ChangeAcon.cs ※Ver.02
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeAcon : MonoBehaviour
{
[SerializeField]RuntimeAnimatorController[] controller;
private int index = 0;
private int a_max = 0;
private Vector3 default_pos = new Vector3(0f, 0f, 0f);
Animator animator;
void Start()
{
transform.position = default_pos;
animator = this.GetComponent<Animator> ();
a_max = controller.Length;
}
void Update()
{
if(Input.GetButtonDown("Fire2")){
transform.position = default_pos;
animator.runtimeAnimatorController = controller[index];
Debug.Log(controller[index].name);
index++;
if(index == a_max){index = 0;}
}
}
}
使用するとこんな感じ。
アニメーションアセットの中には再生後オブジェクトの位置が初期値と異なるものもあるので、Controller切り替え時に原点に戻すようにした。
— 梅しらす@Arcanite Links (@ume_white) June 18, 2021
使用モーションは先日セールやってた↓https://t.co/WirvkKAYzd#Unity pic.twitter.com/jIqX5CwiqL
参考
コンソールのデバッグログをゲーム画面表示については
https://qiita.com/_udonba/items/0b4db4fd35a27d58b6d7
こちらの記事のCatchLog.csを使用させてもらった。
コントローラ切り替えの参考にした記事はこちら
https://robamemo.hatenablog.com/entry/2017/07/06/114102