アニメーションイベントを使用することでアニメーションクリップを最大限に活用することができます。アニメーションイベントは、オブジェクトのスクリプト内の関数をタイムラインの特定のタイミングで呼び出すことができます。
関数はアニメーションイベントにより呼び出され、オプションとしてパラメーターをひとつ付加することができます。パラメーターは float
、string
、int
、object
、AnimationEvent オブジェクト
です。AnimationEvent オブジェクトはメンバー変数を持ち、float、string、int、objectをすべて一緒に、関数呼び出しをトリガーしたイベントに関する情報とともに、関数に受け渡すことができます。
// この C# 関数はアニメーションイベントによって呼び出されます
public void PrintFloat (float theValue) {
Debug.Log ("PrintFloat is called with a value of " + theValue);
}
現在の再生ヘッドの位置にあるクリップにアニメーションイベントを加えるには、 Event ボタンをクリックします。アニメーションの任意の位置にアニメーションイベントを追加するには、イベントを発生させたい位置で Event ラインをダブルクリックします。一旦加えると、マウスをドラッグしてイベントの位置を変えることができます。イベントを削除するには、イベントを選択して Delete キーを押すか、イベントを右クリックして Delete Event を選択してください。
イベントを追加すると、インスペクターウィンドウにいくつかのフィールドが表示されます。このフィールドで、呼び出したい関数の名前と渡したいパラメーターの値を指定します。
クリップに追加されたイベントはイベントラインでマーカー表示されます。マーカーの上をマウスオーバーすると、関数名とパラメーター値のツールチップが表示されます。
タイムラインで複数のイベントを選択できます。
複数のイベントを選択するには、Shift キーを押しながらイベントマーカーをひとつずつ選択して加えます。また、選択するイベント部分の選択ボックスをドラッグすることもできます。つまり、下の図のようにイベントマーカーエリアをクリックしてドラッグします。
この例は、アニメーションイベントを簡単なゲームオブジェクトに加える方法を示しています。すべてのステップを行うと、再生モードの間、キューブが x 軸に沿って前後にアニメーションします。そして、イベントメッセージが 0.8 秒の時点から毎秒ごとにコンソールに表示されます。
この例には、PrintEvent()
関数を含む短いスクリプトが必要です。その関数には文字列 (“called at:”) と時間が含まれています。
//この C# 関数はアニメーションイベントによって呼び出すことができます。
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public void PrintEvent(string s)
{
Debug.Log("PrintEvent: " + s + " called at: " + Time.time);
}
}
このサンプルコードを含むスクリプトファイルを作成し、Project フォルダーに置きます (Unity のProject ウィンドウ内で右クリックし Create > C# Script の順に選択し、上のコードサンプルを作成されたファイルにコピーアンドペーストし、保存します)。
Unity では、Cube ゲームオブジェクトを作成します ( GameObject > 3D Object > Cube)。新しいスクリプトファイルを加えるには、ファイルを Project ウィンドウからインスペクターウィンドウにドラッグ & ドロップします。
Cube ゲームオブジェクトを選び、 Animation ウィンドウを開きます (Window > Animation > Animation または Ctrl + 6)。 X 座標に Position カーブを設定します。
次に、X 座標のアニメーションを 1 秒ほどの間に 0.4 ほどに増加してから、0 に戻るように設定します。それから、約 0.8 秒の時点でアニメーションイベントを作成します。再生ボタンを押すと、アニメーションが実行されます。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.