[Unity]AnimatorControllerメモ ハッシュ値をキャッシュして軽くする(非エンジニア)
Unity version 2021.3.14f1
Animatorの再生処理などでハッシュ値(ID)を使うと文字列を使うより軽いらしい
文字列で書いてもハッシュ値(ID)に変換されるので先にハッシュ値に変換しておくということらしい
参考元
Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SampleScript : MonoBehaviour
{
// 変数の宣言
private Animator animator; // Animator用の変数
// ハッシュ値用の変数
private static readonly int MoveStateHash = Animator.StringToHash("Base Layer.move"); // StateはLayer名を含める
private static readonly int MoveTagHash = Animator.StringToHash("Move"); // Tagのハッシュ値
private static readonly int SpeedParameterHash = Animator.StringToHash("Speed"); // Parameterのハッシュ値
void Start()
{
// AnimatorのComponentを取得して変数へ代入
animator = this.GetComponent<Animator>();
}
void Update()
{
// アニメーションをBase LayerのmoveのStateへ遷移
animator.Play(MoveStateHash);
animator.CrossFade(MoveStateHash, 0.1f, 0, 0);
if(animator.GetCurrentAnimatorStateInfo(0).fullPathHash == MoveStateHash){
// 処理:条件 StateがBase Layerのmoveの時
}
if(animator.GetCurrentAnimatorStateInfo(0).tagHash == MoveTagHash){
// 処理:条件 タグがMoveのStateの時
}
// ParameterのSpeedに1を代入
animator.SetFloat(SpeedParameterHash, 1);
// レイヤー0のウェイトを1に変更
animator.SetLayerWeight (0, 1);
// レイヤー0のウェイトを取得してConsoleに出力
Debug.Log(animator.GetLayerWeight(0));
}
}
Scriptの補足
private static readonly int MoveStateHash = Animator.StringToHash("Base Layer.move");
int型のMoveStateHashという変数にAnimatorの「Base Layer.move」という文字列をハッシュ値に変換して代入
※レイヤー名はAnimatorウィンドウのLayersタブで確認できる
private static readonly int MoveTagHash = Animator.StringToHash("Move");
int型のMoveTagHashという変数にAnimatorの「Move」という文字列をハッシュ値に変換して代入
private static readonly int SpeedParameterHash = Animator.StringToHash("Speed");
int型のSpeedParameterHashという変数にAnimatorの「Speed」という文字列をハッシュ値に変換して代入
※パラメータはAnimatorウィンドウのParametersタブで追加できる
animator = this.GetComponent<Animator>();
AddComponentされているAnimatorを取得してanimatorという変数に代入
animator.Play(MoveStateHash);
MoveStateHashという変数に入っているハッシュ値でアニメーションの遷移を指定している(フェードは行わない)
animator.Play("move");という処理をハッシュ値に変換して行っている
animator.CrossFade(MoveStateHash, 0.1f, 0, 0);
MoveStateHashという変数に入っているハッシュ値でアニメーションの遷移を指定している(フェードを行う)
animator.CrossFade("move", 0.1f, 0, 0);という処理をハッシュ値に変換して行っている
CrossFade
Stateの遷移を行なう際にアニメーションをスムーズに切り替えるための処理
数字は遷移時間(割合?)0~1, レイヤー番号, アニメーション開始位置0~1を指定している
遷移時間は遷移先のアニメーションの長さに依存するので1フレームしかないアニメーションに遷移する場合はフェードは起こらない
レイヤー番号はAnimatorウィンドウのLayersタブにあるレイヤーに上から順に0,1,2…とわり振られている
if(animator.GetCurrentAnimatorStateInfo(0).fullPathHash == MoveStateHash){}
MoveStateHashという変数に入っているハッシュ値と現在のアニメーションStateのハッシュ値が同じかどうかをif文の条件として指定している
()内の数字はレイヤー番号
if(animator.GetCurrentAnimatorStateInfo(0).IsName("move")){}という処理をハッシュ値に変換して行っている
if(animator.GetCurrentAnimatorStateInfo(0).tagHash == MoveTagHash){}
MoveTagHashという変数に入っているハッシュ値と現在のアニメーションStateのTagのハッシュ値が同じかどうかをif文の条件として指定している
()内の数字はレイヤー番号
※TagはStateを選択してInspectorウィンドウで指定できる
if(animator.GetCurrentAnimatorStateInfo(0).IsTag("Move")){}という処理をハッシュ値に変換して行っている
animator.SetFloat(SpeedParameterHash, 1);
SpeedParameterHashという変数に入っているハッシュ値でパラメータの値を1に更新している
animator.SetFloat("Speed", 1);という処理をハッシュ値に変換して行っている
animator.SetLayerWeight (0, 1);
レイヤーのウェイトの更新する処理
数字はレイヤー番号とレイヤーウェイト0~1
animator.SetLayerWeight (animator.GetLayerIndex("Base Layer"), 1);と同じ処理
Debug.Log(animator.GetLayerWeight(0));
レイヤーのウェイトを取得する(SetをGetに変更するだけ)
数字はレイヤー番号
Debug.Log(animator.GetLayerWeight (animator.GetLayerIndex("Base Layer")));と同じ処理
▼過去に書いたAnimatorControllerについてのメモ
勉強中ですので説明が間違っている可能性があります自分でも調べて確認してみてください
この記事が気に入ったらサポートをしてみませんか?