public void StartFade(int frame) { this.prevX = this.target.localPosition.x; this.prevY = this.target.localPosition.y; this.prevScaleX = this.target.localScale.x / this.initScaleX; this.prevScaleY = this.target.localScale.y / this.initScaleX; this.prevRotation = this.target.eulerAngles.z; this.prevR = this.color.r; this.prevG = this.color.g; this.prevB = this.color.b; this.prevA = this.color.a; Keyframe2D begin = this.keyframes[0]; Keyframe2D end = null; for (int i = 0; i < this.keyframes.Length; i++) { Keyframe2D k = this.keyframes[i]; if (k.frame <= frame) { begin = k; } if (k.frame > frame && end == null) { end = k; } } float p; if (end == null) { end = begin; p = 1; } else { p = ((float)frame - (float)begin.frame) / ((float)end.frame - (float)begin.frame); } this.fadeX = begin.x + (p * (end.x - begin.x)); this.fadeY = begin.y + (p * (end.y - begin.y)); this.fadeScaleX = begin.scaleX + (p * (end.scaleX - begin.scaleX)); this.fadeScaleY = begin.scaleY + (p * (end.scaleY - begin.scaleY)); // find shortest rotation direction, could be more optimized float r1 = begin.rotation; float r2 = end.rotation; while (r1 < 0f) { r1 += 360f; } while (r2 < 0f) { r2 += 360f; } float r3 = r2 - 360f; if (Mathf.Abs(r2 - r1) > Mathf.Abs(r3 - r1)) { r2 = r3; } r3 = r2 + 360f; if (Mathf.Abs(r2 - r1) > Mathf.Abs(r3 - r1)) { r2 = r3; } this.fadeRotation = r1 + (p * (r2 - r1)); this.fadeR = begin.r + (p * (end.r - begin.r)); this.fadeG = begin.g + (p * (end.g - begin.g)); this.fadeB = begin.b + (p * (end.b - begin.b)); this.fadeA = begin.a + (p * (end.a - begin.a)); }
public void Update(float frame) { Keyframe2D begin = null; Keyframe2D end = null; for (int i = 0; i < this.keyframes.Length; i++) { Keyframe2D k = this.keyframes[i]; if (k.frame <= frame) { begin = k; } if (k.frame > frame && end == null) { end = k; } } float p; if (end == null) { end = begin; p = 1; } else { p = (frame - begin.frame) / (end.frame - begin.frame); } float x = begin.x + (p * (end.x - begin.x)); float y = begin.y + (p * (end.y - begin.y)); this.target.localPosition = new Vector3(x, y, this.target.localPosition.z); x = begin.scaleX + (p * (end.scaleX - begin.scaleX)); y = begin.scaleY + (p * (end.scaleY - begin.scaleY)); this.target.localScale = new Vector3(x * this.initScaleX, y * this.initScaleY, this.target.localScale.z); // find shortest rotation direction, could be more optimized float r1 = begin.rotation; float r2 = end.rotation; while (r1 < 0f) { r1 += 360f; } while (r2 < 0f) { r2 += 360f; } float r3 = r2 - 360f; if (Mathf.Abs(r2 - r1) > Mathf.Abs(r3 - r1)) { r2 = r3; } r3 = r2 + 360f; if (Mathf.Abs(r2 - r1) > Mathf.Abs(r3 - r1)) { r2 = r3; } this.target.eulerAngles = new Vector3(this.target.eulerAngles.x, this.target.eulerAngles.y, r1 + (p * (r2 - r1))); if (mesh != null && (this.color.r != end.r || this.color.g != end.g || this.color.b != end.b || this.color.a != end.a)) { this.color = new Color( begin.r + (p * (end.r - begin.r)), begin.g + (p * (end.g - begin.g)), begin.b + (p * (end.b - begin.b)), begin.a + (p * (end.a - begin.a)) ); this.colors[0] = this.colors[1] = this.colors[2] = this.colors[3] = this.color; mesh.colors = this.colors; } }
public void AddKeyframe(Keyframe2D keyframe) { this.keyframes[count] = keyframe; count++; }