当前位置: 代码迷 >> 综合 >> Unity,C#版的动画曲线,Tween:EaseIn,EaseOut,EaseInOut(语法逻辑整理版本,含测试代码)
  详细解决方案

Unity,C#版的动画曲线,Tween:EaseIn,EaseOut,EaseInOut(语法逻辑整理版本,含测试代码)

热度:94   发布时间:2023-11-24 16:51:06.0

前篇:Unity,C#版的动画曲线,Tween:EaseIn,EaseOut,EaseInOut(编程语言翻译版本)
https://blog.csdn.net/qq_37776196/article/details/114667670

整理了语法和逻辑,拆分了会让人难以理解的连写代码。
我是很讨厌连写代码的, 大量的三元运算和一行代码中多个赋值操作。

下面再附送一段测试代码

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);time = time / nDuration;return nChange * time * time + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / nDuration;return -nChange * time * (time - 2) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / (nDuration / 2);if (time < 1){return nChange / 2 * time * time + nBegin;}time--;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);time = time / nDuration;return nChange * time * time * time + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / nDuration - 1;return nChange * (time * time * time + 1) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / (nDuration / 2);if (time < 1){return nChange / 2 * time * time * time + nBegin;}time -= 2;return nChange / 2 * (time * time * time + 2) + nBegin;}}public class Quart{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / nDuration;return nChange * time * time * time * time + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / nDuration - 1;return -nChange * (time * time * time * time - 1) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / (nDuration / 2);if (time < 1){return nChange / 2 * time * time * time * time + nBegin;}time -= 2;return -nChange / 2 * (time * time * time * time - 2) + nBegin;}}public class Quint{public static float easeIn(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / nDuration;return nChange * time * time * time * time * time + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / nDuration - 1;return nChange * (time * time * time * time * time + 1) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / (nDuration / 2);if (time < 1){return nChange / 2 * time * time * time * time * time + nBegin;}time -= 2;return nChange / 2 * (time * 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;}time = time /(nDuration / 2);if (time < 1){return nChange / 2 * Mathf.Pow(2, 10 * (time - 1)) + nBegin;}time--;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);time = time / nDuration;return -nChange * (Mathf.Sqrt(1 - time * time) - 1) + nBegin;}public static float easeOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / nDuration - 1;return nChange * Mathf.Sqrt(1 - time * time) + nBegin;}public static float easeInOut(float time, float nBegin, float nChange, float nDuration){time = GetTime(time, nDuration);time = time / (nDuration / 2);if (time < 1){return -nChange / 2 * (Mathf.Sqrt(1 - time * time) - 1) + nBegin;}time -= 2;return nChange / 2 * (Mathf.Sqrt(1 - time * 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;}time = time / nDuration;if (time == 1){return nBegin + nChange;}if (p == 0){p = nDuration * 0.3f;}float s;if (a == 0 || a < Mathf.Abs(nChange)){a = nChange;s = p / 4;}else{s = p / (2 * Mathf.PI) * Mathf.Asin(nChange / a);}time -= 1;return -(a * Mathf.Pow(2, 10 * time) * 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;}time = time / nDuration;if (time == 1){return nBegin + nChange;}if (p == 0){p = nDuration * 0.3f;}float s;if (a == 0 || a < Mathf.Abs(nChange)){a = nChange;s = p / 4;}else{s = 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;}time = time / (nDuration / 2);if (time == 2){return nBegin + nChange;}if (p == 0){p = nDuration * (0.3f * 1.5f);}float s;if (a == 0 || a < Mathf.Abs(nChange)){a = nChange;s = p / 4;}else{s = p / (2 * Mathf.PI) * Mathf.Asin(nChange / a);}if (time < 1){time -= 1;return -0.5f * (a * Mathf.Pow(2, 10 * time) * Mathf.Sin((time * nDuration - s) * (2 * Mathf.PI) / p)) + nBegin;}time -= 1;return a * Mathf.Pow(2, -10 * time) * 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;}time = time / nDuration;return nChange * time * 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;}time = time / nDuration - 1;return nChange * (time * 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;}time = time / (nDuration / 2);if (time < 1){return nChange / 2 * (time * time * (((s *= 1.525f) + 1) * time - s)) + nBegin;}time -= 2;return nChange / 2 * (time * 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);time = time / nDuration;if (time < (1 / 2.75f)){return nChange * (7.5625f * time * time) + nBegin;}else if (time < (2 / 2.75f)){time -= 1.5f / 2.75f;return nChange * (7.5625f * time * time + 0.75f) + nBegin;}else if (time < (2.5f / 2.75f)){time -= 2.25f / 2.75f;return nChange * (7.5625f * time * time + 0.9375f) + nBegin;}else{time -= 2.625f / 2.75f;return nChange * (7.5625f * time * 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;}}
}

测试代码:


public class NewBehaviourScript : MonoBehaviour
{private bool m_isPlay = false;public Transform[] m_trans = null;private string[] m_strNames = new string[]{"Quad","Cubic","Quart","Quint","Sine","Expo","Circ","Elastic","Back","Bounce"};private void Start(){int nCount = m_strNames.Length * 3;int n3Count = 0;int nNameIndex = 0;m_trans = new Transform[nCount];for (int i = 0; i < nCount; i++){GameObject newObj = GameObject.CreatePrimitive(PrimitiveType.Sphere);if (n3Count >= 3){n3Count = 0;nNameIndex++;}newObj.name = m_strNames[nNameIndex] + n3Count.ToString();n3Count++;m_trans[i] = newObj.transform;m_trans[i].position = new Vector3(i, 0, 0);}}private float nTime = 0;private void Update(){if (Input.GetKeyDown(KeyCode.A)){nTime = 0;m_isPlay = true;}if (m_isPlay){if (nTime >= 2){m_isPlay = false;}float nValue = 0;int nIndex = 0;nValue = Tween.Quad.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Quad.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Quad.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Cubic.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Cubic.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Cubic.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Quart.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Quart.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Quart.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Quint.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Quint.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Quint.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Sine.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Sine.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Sine.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Expo.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Expo.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Expo.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Circ.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Circ.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Circ.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Elastic.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Elastic.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Elastic.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Back.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Back.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Back.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Bounce.easeIn(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Bounce.easeOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nValue = Tween.Bounce.easeInOut(nTime, 0, 5, 1);m_trans[nIndex].position = new Vector3(nIndex, nValue, 0);nIndex++;nTime += Time.deltaTime;}}
}
  相关解决方案