//private IEnumerator FaceToDirection(string animationName, EntityAnimationDirection direction, float fps, Action<string> callback) //{ // if (direction.Equals(lastDirection)) // { // if (callback != null) callback(animationName); // yield break; // } // var currentFrame = DirectionToFrame(lastDirection); // var targetFrame = DirectionToFrame(direction); // animator.Sprite.FlipX = false; // var deltaFrame = targetFrame - currentFrame; // if (deltaFrame < 0) deltaFrame += 16; // if (deltaFrame > 8) // { // if (reversedStand == null) // { // var stand = animator.GetClipByName(animationName); // reversedStand = new tk2dSpriteAnimationClip(); // reversedStand.CopyFrom(stand); // System.Array.Reverse(reversedStand.frames); // } // currentFrame = 15 - currentFrame; // targetFrame = 15 - targetFrame; // animator.PlayFromFrame(reversedStand, currentFrame); // } // else // { // animator.PlayFromFrame(animationName, currentFrame); // } // SetAnimatorFpsFactor(fps); // Assert.Should(animator.CurrentClip.frames.Length >= 16, "invalid frame count ... " + this.gameObject.name); // while (animator.CurrentFrame != targetFrame) // { // yield return 0; // } // animator.Stop(); // lastDirection = direction; // if (callback != null) // { // callback(animationName); // } //} private int DirectionToFrame(EntityAnimationDirection direction) { var frame = 0; switch (direction.direction) { case EntityDirection.Top: frame = 0; break; case EntityDirection.TopRight: frame = 2; break; case EntityDirection.Right: frame = 4; break; case EntityDirection.BottomRight: frame = 6; break; case EntityDirection.Bottom: frame = 8; break; } if (direction.flipX) { frame = 16 - frame; } return(frame); }
private void FaceToDirection(string animationName, EntityAnimationDirection direction, float fps, Action <string> callback) { if (direction.Equals(lastDirection)) { if (callback != null) { callback(animationName); } return; } var currentFrame = DirectionToFrame(lastDirection); var targetFrame = DirectionToFrame(direction); animator.Sprite.FlipX = false; var deltaFrame = targetFrame - currentFrame; if (deltaFrame < 0) { deltaFrame += 16; } if (deltaFrame > 8) { if (reversedStand == null) { var stand = animator.GetClipByName(animationName); reversedStand = new tk2dSpriteAnimationClip(); reversedStand.CopyFrom(stand); System.Array.Reverse(reversedStand.frames); } currentFrame = 15 - currentFrame; targetFrame = 15 - targetFrame; animator.PlayFromFrame(reversedStand, currentFrame); } else { animator.PlayFromFrame(animationName, currentFrame); } SetAnimatorFpsFactor(fps); Assert.Should(animator.CurrentClip.frames.Length >= 16, "invalid frame count ... " + this.gameObject.name); // 设置 updater m_updateRotate = (dt) => { if (animator.CurrentFrame != targetFrame) { return; } animator.Stop(); lastDirection = direction; if (callback != null) { callback(animationName); } m_updateRotate = null; }; }
public override void PlayAnimation(string animationName, EntityAnimationDirection animationDirection, float fps = 0.0f, Action <string> callback = null) { StopAllCoroutines(); if (animationDirection != null && animationName.Contains(AnimationNames.Stand)) { FaceToDirection(animationName, animationDirection, fps, callback); } else { lastDirection = animationDirection; base.PlayAnimation(animationName, animationDirection, fps, callback); } }
virtual public void PlayAnimation(string animationName, EntityAnimationDirection animationDirection, float fps = 0.0f, Action <string> callback = null) { tk2dSpriteAnimationClip clip; if (animationDirection == null) { clip = animator.GetClipByName(animationName); } else { if (animationDirection.flipX) { animator.transform.localScale = new Vector3(-1, 1, 1); } else { animator.transform.localScale = new Vector3(1, 1, 1); } //animator.Sprite.FlipX = animationDirection.flipX; clip = animator.GetClipByName(animationName + "_" + animationDirection.direction); } Assert.Should(clip != null, animationName + ",animationDirection:" + animationDirection); if (callback != null) { if (_animationFinishCallbacks == null) { _animationFinishCallbacks = new Dictionary <tk2dSpriteAnimationClip, UserAnimationCallbackInfos>(); } UserAnimationCallbackInfos infos = new UserAnimationCallbackInfos() { func = callback, animationName = animationName }; _animationFinishCallbacks.Add(clip, infos); } animator.Play(clip); SetAnimatorFpsFactor(fps); }
public override void PlayAnimation(string animationName, EntityAnimationDirection animationDirection, float fps = 0.0f, Action <string> callback = null) { if (animationDirection != null) { if (animationDirection.direction == EntityDirection.Top)//使用右上替代上 { animationDirection.direction = EntityDirection.TopRight; if (lastDirection != null)//减少左右频繁转向的问题 { animationDirection.flipX = lastDirection.flipX; } } else if (animationDirection.direction == EntityDirection.Bottom)//使用左下替代下 { animationDirection.direction = EntityDirection.BottomRight; if (lastDirection != null)//减少左右频繁转向的问题 { animationDirection.flipX = lastDirection.flipX; } } } lastDirection = animationDirection; base.PlayAnimation(animationName, animationDirection, fps, callback); }