private Sprite[] GetCurrentFrames() { FrameAnimatorData frameAnimatorData = GetCurrentFrameAnimatorData(); if (frameAnimatorData == null) { return(null); } return(frameAnimatorData.Frames); }
////当前帧率,通过曲线计算而来 //private float currentFramerate = 20.0f; /// <summary> /// 重设动画 /// </summary> public void Reset() { FrameAnimatorData frameAnimatorData = GetCurrentFrameAnimatorData(); if (frameAnimatorData == null) { return; } currentFrameIndex = frameAnimatorData.Framerate < 0 ? frameAnimatorData.Frames.Length - 1 : 0; }
void Update() { //帧数据无效,禁用脚本 FrameAnimatorData frameAnimatorData = GetCurrentFrameAnimatorData(); if (frameAnimatorData == null) { this.enabled = false; return; } if (frameAnimatorData.Frames == null || frameAnimatorData.Frames.Length == 0) { this.enabled = false; return; } //从曲线值计算当前帧率 Sprite[] frames = frameAnimatorData.Frames; float curveValue = frameAnimatorData.curve.Evaluate((float)currentFrameIndex / frames.Length); float curvedFramerate = curveValue * frameAnimatorData.Framerate; //帧率有效 if (curvedFramerate != 0) { //获取当前时间 float time = ignoreTimeScale ? Time.unscaledTime : Time.time; //计算帧间隔时间 float interval = Mathf.Abs(1.0f / curvedFramerate); //满足更新条件,执行更新操作 if (time - timer > interval) { //执行更新操作 DoUpdate(curvedFramerate); } } #if UNITY_EDITOR else { Debug.LogWarning("Framerate got '0' value, animation stopped."); } #endif }
//具体更新操作 private void DoUpdate(float curvedFramerate) { //计算新的索引 int nextIndex = currentFrameIndex + (int)Mathf.Sign(curvedFramerate); FrameAnimatorData frameAnimatorData = GetCurrentFrameAnimatorData(); Sprite[] frames = frameAnimatorData.Frames; //索引越界,表示已经到结束帧 if (nextIndex < 0 || nextIndex >= frames.Length) { //广播事件 if (FinishEvent != null) { FinishEvent(); } //非循环模式,禁用脚本 if (frameAnimatorData.Loop == false) { currentFrameIndex = Mathf.Clamp(currentFrameIndex, 0, frames.Length - 1); this.enabled = false; return; } } //钳制索引 currentFrameIndex = nextIndex % frames.Length; //更新图片 if (image != null) { image.sprite = frames[currentFrameIndex]; } else if (spriteRenderer != null) { spriteRenderer.sprite = frames[currentFrameIndex]; } //设置计时器为当前时间 timer = ignoreTimeScale ? Time.unscaledTime : Time.time; }