アニメーション終了時に任意のメソッドを呼び出す方法
Animation Event の作成
手順1
「Assets」フォルダ内の「Animation」ファイルをダブルクリックして中身を開く。
![](https://tsubasamusu.com/wp-content/uploads/2023/05/image-30-1024x461.png)
手順2
「Animation」内の左上の「▶▶❙」を押して白線を右端に移動させる。
![](https://tsubasamusu.com/wp-content/uploads/2023/05/image-31-1024x529.png)
手順3
「Animation」内の左上の「■+」を押して「Animation Event」を作成する。
下画像の矢印の位置に白または青の小さな長方形が出現していれば良い。
![](https://tsubasamusu.com/wp-content/uploads/2023/05/image-32-1024x532.png)
呼び出したいメソッドの作成
手順4
「Assets」フォルダ内で C# スクリプトファイルを作成し、対象の Animation を含む Animator がアタッチされているゲームオブジェクトに、その C# スクリプトファイルをアタッチする。
アニメーション終了時に呼び出したいメソッドが記述されている C# スクリプトファイルと、Animation Event を追加した Animation を含む Animator は必ず同じゲームオブジェクトにアタッチしなければならない。
![](https://tsubasamusu.com/wp-content/uploads/2023/05/image-33.png)
手順5
アニメーション終了時に呼び出したいメソッドを「手順4」で作成したスクリプトに記述する。
記述するメソッドの修飾子は「public」でなくても良いらしいが、「このメソッドは外部から呼び出されている」という事を明示的に示すためにも「public」にするべきである。
using UnityEngine;
public class AnimationController : MonoBehaviour
{
public void OnAnimationEnd()
{
//アニメーション終了時の処理
}
}
Animation Event の設定
手順6
「Assets」フォルダ内の Animation ファイルをダブルクリックして中身を開き、「手順3」で作成した Animation Event をクリックし、インスペクターに詳細を表示させる。
![](https://tsubasamusu.com/wp-content/uploads/2023/05/image-34-1024x448.png)
手順7
「手順5」で作成したメソッドの名前をインスペクターで「Function」に入力する。
![](https://tsubasamusu.com/wp-content/uploads/2023/05/image-37.png)
その他
補足
当記事では Animation Event をアニメーション終了判定用に用いているが、Animation Event 自体はどのタイミングでも何度でも使用できる。