using UnityEngine;
using System.Collections;
public class SimpleExample : MonoBehaviour {
Animator anim =
null
;
AnimatorStateInfo currentBaseStage;
int idle = Animator.StringToHash(
"Base Layer.Idle"
);
int hit = Animator.StringToHash(
"Base Layer.Hit"
);
bool isHit =
false
;
bool isPlayBack =
false
;
void Start () {
anim = GetComponent<Animator>();
}
void Update () {
currentBaseStage = anim.GetCurrentAnimatorStateInfo(0);
if
(currentBaseStage.nameHash == idle && !anim.IsInTransition(0))
{
anim.SetBool(
"hit"
,
false
);
isHit =
false
;
}
if
(!isHit && Input.GetKey(KeyCode.H) && !anim.IsInTransition(0) && currentBaseStage.nameHash == idle)
{
anim.SetBool(
"hit"
,
true
);
anim.StartRecording(0);
isHit =
true
;
}
if
(isHit && Input.GetKey(KeyCode.H) && currentBaseStage.nameHash == hit)
{
isPlayBack =
true
;
anim.StopRecording();
anim.StartPlayback();
anim.playbackTime = 0f;
}
if
(isPlayBack)
{
float newPlaybackTime = anim.playbackTime + Time.deltaTime;
if
(newPlaybackTime > anim.recorderStopTime) {
newPlaybackTime = 0;
anim.StopPlayback();
isPlayBack =
false
;
}
anim.playbackTime = newPlaybackTime;
}
}
}