Here’s a reusable script for preloading AnimationClips with AnimationEvents: (in Unity 2.6 you can do this directly in the new Animation Editor, but in Unity iPhone this is a nice way to do it)
//PreloadAnimClipWithEvent.js
var targetClipName : String = "idle"; //Where should the Event be added? (the name of the AnimationClip)
var throwTime : float = 1.3; //When should the function be called?
var throwFunctionName : String = "MyFunction"; //What's the name of the function?
function Start () {
var throwEvent : AnimationEvent = new AnimationEvent();
throwEvent.time = throwTime;
throwEvent.functionName = throwFunctionName;
animation[targetClipName].clip.AddEvent(throwEvent);
}
And since my scripts are never in the right place to catch messages sent by AnimationEvents, here’s a script to relay a message. If this script catches a message called RelayMessage it sends message to target. So just set the AnimationEvent to send a message named “RelayMessage” and then enter real target and real functionName here.
//RelayMessage.js
var target : GameObject;
var message : String = "FunctionName";
function RelayMessage () {
target.SendMessage(message);
}
To sum it up: With the first scripts you can add an AnimationEvent to an AnimationClip and with the second script you can easily relay that message from the gameObject that holds the animation to some other gameObject that is the intended recipient.