コガネブログ

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

【Unity】Animator のパラメータへの文字列参照を避けることができる「AnimatorParameterReference」紹介

はじめに

「AnimatorParameterReference」を Unity プロジェクトに導入することで
Animator のパラメータへの文字列参照を避けることができるようになります

使い方

f:id:baba_s:20181222153350p:plain

例えばこのように「value」というパラメータを持つ Animator が存在する場合に

f:id:baba_s:20181222153354p:plain

その Animator を右クリックして
「Animator>Generate Parameters References」を選択すると

f:id:baba_s:20181222153357p:plain

その Animator が持つパラメータの名前とハッシュ値を管理する
ScriptableObject が生成されます

using UnityEngine;

public class Example : MonoBehaviour
{
    public Animator                   m_animator ;
    public AnimatorParameterReference m_reference;

    private void Start()
    {
        m_animator.SetFloat( m_reference, 0 );
    }
}

あとはこのようなコードを記述することで使用できます