Created
December 27, 2012 02:55
-
-
Save kimsama/4385037 to your computer and use it in GitHub Desktop.
WaitForAnimationTrigger class for AIBehavior, Unity3D package.
It waits until the animation is done by the given ratio, and changes to the specified state.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
/// | |
/// WaitForAnimationTrigger class. | |
/// | |
/// It waits until the animation is done by the given ratio, and changes to the specified state. | |
/// | |
/// 2012. 12. @kimsama | |
/// | |
public class WaitForAnimationTrigger : BaseTrigger | |
{ | |
public float duration = 1.0f; | |
private Animation animation = null; | |
private bool isStarted = false; | |
/// | |
/// Awake | |
/// | |
void Awake() | |
{ | |
animation = this.gameObject.GetComponent<Animation>(); | |
if (animation == null) | |
animation = this.gameObject.transform.parent.GetComponent<Animation>(); | |
} | |
/// | |
/// | |
/// | |
protected override void Init() | |
{ | |
this.isStarted = false; | |
} | |
/// | |
/// | |
/// | |
protected override bool Evaluate(AIBehaviors fsm) | |
{ | |
bool ret = false; | |
if (this.isStarted == false) | |
{ | |
this.isStarted = true; | |
string name = ""; | |
for(int i=0; i<fsm.currentState.animationStates.Length; i++) | |
{ | |
name = fsm.currentState.animationStates[i].name; | |
if (animation.IsPlaying(name)) | |
{ | |
StartCoroutine(WaitForAnimation(name, this.duration, ()=>{ | |
fsm.ChangeActiveState(transitionState); | |
ret = true; | |
})); | |
break; | |
} | |
} | |
} | |
return ret; | |
} | |
/// | |
/// Wait for the animation is done till the given ratio. | |
/// The ratio has the value between [0, 1]. | |
/// | |
public IEnumerator WaitForAnimation(string name, float ratio, Action onComplete) | |
{ | |
var state = animation[name]; | |
return WaitForAnimation(state, ratio, onComplete); | |
} | |
/// | |
/// Wait for the animation is done till the given ratio. | |
/// The ratio has the value between [0, 1]. | |
/// | |
public static IEnumerator WaitForAnimation(AnimationState state, float ratio, Action onComplete) | |
{ | |
state.wrapMode = WrapMode.ClampForever; | |
state.enabled = true; | |
state.speed = state.speed == 0 ? 1 : state.speed; | |
var t = state.time; | |
while((t/state.length) + float.Epsilon < ratio) | |
{ | |
t += Time.deltaTime * state.speed; | |
yield return null; | |
} | |
if (onComplete != null) | |
onComplete(); | |
} | |
#if UNITY_EDITOR | |
// Implement GUI editor script for specifying animation ratio. | |
public override void DrawInspectorProperties(SerializedObject sObject) | |
{ | |
SerializedProperty prop; | |
prop = sObject.FindProperty("duration"); | |
if (prop.floatValue < 0.0f) | |
prop.floatValue = 0.0f; | |
if (prop.floatValue > 1.0f) | |
prop.floatValue = 1.0f; | |
EditorGUILayout.PropertyField(prop); | |
} | |
#endif | |
} |
why not just WaitFoSeconds(animationTime) ?
I don't understand what WaitFoSeconds(animationTime)
it is. Thus, it's been a looong time since this post. :-) Even I can't recall what AIBehaviors
was. Sorry for that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why not just WaitFoSeconds(animationTime) ?