// update cache (optimized) public override void updateCache(ITarget target) { base.updateCache(target); for (int i = 0; i < keys.Count; i++) { RotationEulerKey key = keys[i] as RotationEulerKey; key.GeneratePath(this, i); //invalidate some keys in between if (key.path != null) { int endInd = i + key.keyCount - 1; if (endInd < keys.Count - 1 || key.interp != keys[endInd].interp) //don't count the last element if there are more keys ahead { endInd--; } for (int j = i + 1; j <= endInd; j++) { var _key = keys[j] as RotationEulerKey; _key.interp = key.interp; _key.easeType = key.easeType; _key.Invalidate(); } i = endInd; } } }
// update cache (optimized) public override void updateCache(ITarget target) { base.updateCache(target); for (int i = 0; i < keys.Count; i++) { RotationEulerKey key = keys[i] as RotationEulerKey; key.version = version; if (keys.Count > (i + 1)) { key.endFrame = keys[i + 1].frame; } else { if (i > 0 && !keys[i - 1].canTween) { key.interp = Key.Interpolation.None; } key.endFrame = -1; } } }
// add a new key public void addKey(ITarget target, int _frame, Vector3 _rotation) { foreach (RotationEulerKey key in keys) { // if key exists on frame, update key if (key.frame == _frame) { key.rotation = _rotation; // update cache updateCache(target); return; } } var a = new RotationEulerKey(); a.frame = _frame; a.rotation = _rotation; // set default ease type to linear a.easeType = Ease.Linear; // add a new key keys.Add(a); // update cache updateCache(target); }
// preview a frame in the scene view public override void previewFrame(ITarget target, float frame, int frameRate, bool play, float playSpeed) { Transform t = GetTarget(target) as Transform; if (!t) { return; } if (keys == null || keys.Count <= 0) { return; } // if before or equal to first frame, or is the only frame RotationEulerKey firstKey = keys[0] as RotationEulerKey; if (firstKey.endFrame == -1 || (frame <= firstKey.frame && !firstKey.canTween)) { ApplyRot(t, firstKey.rotation); return; } // if lies on rotation action for (int i = 0; i < keys.Count; i++) { RotationEulerKey key = keys[i] as RotationEulerKey; RotationEulerKey keyNext = i + 1 < keys.Count ? keys[i + 1] as RotationEulerKey : null; if (frame >= (float)key.endFrame && keyNext != null && (!keyNext.canTween || keyNext.endFrame != -1)) { continue; } // if no ease if (!key.canTween || keyNext == null) { ApplyRot(t, key.rotation); return; } // else easing function float numFrames = (float)key.getNumberOfFrames(frameRate); float framePositionInAction = Mathf.Clamp(frame - key.frame, 0f, numFrames); Vector3 qStart = key.rotation; Vector3 qEnd = keyNext.rotation; if (key.hasCustomEase()) { ApplyRot(t, Vector3.Lerp(qStart, qEnd, Utility.EaseCustom(0.0f, 1.0f, framePositionInAction / numFrames, key.easeCurve))); } else { var ease = Utility.GetEasingFunction(key.easeType); ApplyRot(t, Vector3.Lerp(qStart, qEnd, ease(framePositionInAction, numFrames, key.amplitude, key.period))); } return; } }
Vector3 getRotationAtFrame(int frame, int frameRate) { // if before or equal to first frame, or is the only frame RotationEulerKey firstKey = keys[0] as RotationEulerKey; if (firstKey.endFrame == -1 || (frame <= (float)firstKey.frame && !firstKey.canTween)) { return(firstKey.rotation); } // if lies on rotation action for (int i = 0; i < keys.Count; i++) { RotationEulerKey key = keys[i] as RotationEulerKey; RotationEulerKey keyNext = i + 1 < keys.Count ? keys[i + 1] as RotationEulerKey : null; if (frame >= (float)key.endFrame && keyNext != null && (!keyNext.canTween || keyNext.endFrame != -1)) { continue; } // if no ease if (!key.canTween || keyNext == null) { return(key.rotation); } // else easing function float numFrames = (float)key.getNumberOfFrames(frameRate); float framePositionInAction = Mathf.Clamp(frame - (float)key.frame, 0f, numFrames); Vector3 qStart = key.rotation; Vector3 qEnd = keyNext.rotation; if (key.hasCustomEase()) { return(Vector3.Lerp(qStart, qEnd, Utility.EaseCustom(0.0f, 1.0f, framePositionInAction / numFrames, key.easeCurve))); } else { var ease = Utility.GetEasingFunction(key.easeType); return(Vector3.Lerp(qStart, qEnd, ease(framePositionInAction, numFrames, key.amplitude, key.period))); } } Debug.LogError("Animator: Could not get rotation at frame '" + frame + "'"); return(Vector3.zero); }
// add a new key public void addKey(ITarget target, int _frame, Vector3 _rotation) { RotationEulerKey prevKey = null; foreach (RotationEulerKey key in keys) { // if key exists on frame, update key if (key.frame == _frame) { key.rotation = _rotation; // update cache updateCache(target); return; } else if (key.frame < _frame) { prevKey = key; } } var a = new RotationEulerKey(); a.frame = _frame; a.rotation = _rotation; // copy interpolation and ease type from previous if (prevKey != null) { a.interp = prevKey.interp; a.easeType = prevKey.easeType; a.easeCurve = prevKey.easeCurve; } else //set default { a.interp = Key.Interpolation.Curve; a.easeType = Ease.Linear; } // add a new key keys.Add(a); // update cache updateCache(target); }