当前位置: 代码迷 >> 综合 >> Unity,C#版的动画曲线,Tween:EaseIn,EaseOut,EaseInOut(编程语言翻译版本)
  详细解决方案

Unity,C#版的动画曲线,Tween:EaseIn,EaseOut,EaseInOut(编程语言翻译版本)

热度:95   发布时间:2023-11-24 16:51:26.0

原文地址:
https://blog.csdn.net/jebe7282/article/details/7521067/

原因是因为现在在Unity里用曲线,要不就自己AnimationCurve自己扭一个曲线出来。要不就下载插件。然鹅,插件都封装了DLL, 我只用曲线,也不需要其他功能,买个插件岂不是浪费(不喜欢用盗版)。 找到一篇不是C#的tween算法,于是进行一波翻译。

public class Tween
{public static float GetTime(float time, float nDuration){return time > nDuration ? nDuration : time;}public static float Linear(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange * time / nDuration + nBegin;}public class Quad{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange * (time /= nDuration) * time + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return -nChange * (time /= nDuration) * (time - 2) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if ((time /= nDuration / 2) < 1){return nChange / 2 * time * time + nBegin;}return -nChange / 2 * (--time * (time - 2) - 1) + nBegin;}}public class Cubic{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange * (time /= nDuration) * time * time + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange * ((time = time / nDuration - 1) * time * time + 1) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if ((time /= nDuration / 2) < 1) return nChange / 2 * time * time * time + nBegin;return nChange / 2 * ((time -= 2) * time * time + 2) + nBegin;}}public class Quart{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange * (time /= nDuration) * time * time * time + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return -nChange * ((time = time / nDuration - 1) * time * time * time - 1) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if ((time /= nDuration / 2) < 1) return nChange / 2 * time * time * time * time + nBegin;return -nChange / 2 * ((time -= 2) * time * time * time - 2) + nBegin;}}public class Quint{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange * (time /= nDuration) * time * time * time * time + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange * ((time = time / nDuration - 1) * time * time * time * time + 1) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if ((time /= nDuration / 2) < 1)return nChange / 2 * time * time * time * time * time + nBegin;return nChange / 2 * ((time -= 2) * time * time * time * time + 2) + nBegin;}}public class Sine{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return -nChange * Mathf.Cos(time / nDuration * (Mathf.PI / 2)) + nChange + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange * Mathf.Sin(time / nDuration * (Mathf.PI / 2)) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return -nChange / 2 * (Mathf.Cos(Mathf.PI * time / nDuration) - 1) + nBegin;}}public class Expo{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if (time == 0){return nBegin;}else{return nChange * Mathf.Pow(2, 10 * (time / nDuration - 1)) + nBegin;}}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if (time == nDuration){return nBegin + nChange;}else{return nChange * (-Mathf.Pow(2, -10 * time / nDuration) + 1) + nBegin;}}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if (time == 0)return nBegin;if (time == nDuration)return nBegin + nChange;if ((time /= nDuration / 2) < 1)return nChange / 2 * Mathf.Pow(2, 10 * (time - 1)) + nBegin;return nChange / 2 * (-Mathf.Pow(2, -10 * --time) + 2) + nBegin;}}public class Circ{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return -nChange * (Mathf.Sqrt(1 - (time /= nDuration) * time) - 1) + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange * Mathf.Sqrt(1 - (time = time / nDuration - 1) * time) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if ((time /= nDuration / 2) < 1)return -nChange / 2 * (Mathf.Sqrt(1 - time * time) - 1) + nBegin;return nChange / 2 * (Mathf.Sqrt(1 - (time -= 2) * time) + 1) + nBegin;}}public class Elastic{public static float easeIn(float time, float nBegin, float nChange, float nDuration, float a = 0, float p = 0){time = GetTime(time, nDuration);if (time == 0)return nBegin;if ((time /= nDuration) == 1)return nBegin + nChange;if (p == 0)p = nDuration * 0.3f;float s = 0;if (a == 0 || a < Mathf.Abs(nChange)){a = nChange;s = p / 4;}elses = p / (2 * Mathf.PI) * Mathf.Asin(nChange / a);return -(a * Mathf.Pow(2, 10 * (time -= 1)) * Mathf.Sin((time * nDuration - s) * (2 * Mathf.PI) / p)) + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration, float a = 0, float p = 0){time = GetTime(time, nDuration);if (time == 0)return nBegin;if ((time /= nDuration) == 1)return nBegin + nChange;if (p == 0)p = nDuration * 0.3f;float s = 0;if (a == 0 || a < Mathf.Abs(nChange)){a = nChange;s = p / 4;}elses = p / (2 * Mathf.PI) * Mathf.Asin(nChange / a);return (a * Mathf.Pow(2, -10 * time) * Mathf.Sin((time * nDuration - s) * (2 * Mathf.PI) / p) + nChange + nBegin);}public static float easeInOut(float time, float nBegin, float nChange, float nDuration, float a = 0, float p = 0){time = GetTime(time, nDuration);if (time == 0)return nBegin;if ((time /= nDuration / 2) == 2)return nBegin + nChange;if (p == 0)p = nDuration * (0.3f * 1.5f);float s = 0;if (a == 0 || a < Mathf.Abs(nChange)){a = nChange;s = p / 4;}elses = p / (2 * Mathf.PI) * Mathf.Asin(nChange / a);if (time < 1)return -0.5f * (a * Mathf.Pow(2, 10 * (time -= 1)) * Mathf.Sin((time * nDuration - s) * (2 * Mathf.PI) / p)) + nBegin;return a * Mathf.Pow(2, -10 * (time -= 1)) * Mathf.Sin((time * nDuration - s) * (2 * Mathf.PI) / p) * 0.5f + nChange + nBegin;}}public class Back{public static float easeIn(float time, float nBegin, float nChange, float nDuration, float s = 0){time = GetTime(time, nDuration);if (s == 0)s = 1.70158f;return nChange * (time /= nDuration) * time * ((s + 1) * time - s) + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration, float s = 0){time = GetTime(time, nDuration);if (s == 0)s = 1.70158f;return nChange * ((time = time / nDuration - 1) * time * ((s + 1) * time + s) + 1) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration, float s = 0){time = GetTime(time, nDuration);if (s == 0)s = 1.70158f;if ((time /= nDuration / 2) < 1)return nChange / 2 * (time * time * (((s *= (1.525f)) + 1) * time - s)) + nBegin;return nChange / 2 * ((time -= 2) * time * (((s *= (1.525f)) + 1) * time + s) + 2) + nBegin;}}public class Bounce{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);return nChange - easeOut(nDuration - time, 0, nChange, nDuration) + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if ((time /= nDuration) < (1 / 2.75f)){return nChange * (7.5625f * time * time) + nBegin;}else if (time < (2 / 2.75f)){return nChange * (7.5625f * (time -= (1.5f / 2.75f)) * time + 0.75f) + nBegin;}else if (time < (2.5 / 2.75f)){return nChange * (7.5625f * (time -= (2.25f / 2.75f)) * time + 0.9375f) + nBegin;}else{return nChange * (7.5625f * (time -= (2.625f / 2.75f)) * time + 0.984375f) + nBegin;}}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);if (time < nDuration / 2)return easeIn(time * 2, 0, nChange, nDuration) * 0.5f + nBegin;else return easeOut(time * 2 - nDuration, 0, nChange, nDuration) * 0.5f + nChange * 0.5f + nBegin;}}
}

程序学无止尽。
欢迎大家沟通,有啥不明确的,或者不对的,也可以和我私聊
我的QQ 334524067 神一般的狄狄

  相关解决方案