前篇: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;}}
}

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

  1. CSS 过渡与动画:transition 属性 ease-in, ease-out, ease-in-out

    简而言之: ease-in 加速, ease-out 减速, ease-in-out 先加速后减速, linear 匀速, ease 与 ease-in-out 类似. stack overflow ...

  2. Unity 动画曲线-AnimationCurve 实例

    Unity 动画曲线-AnimationCurve 实例 以Unity 自带的动画曲线实现一些简单的动画,如往复运动,曲线运动,不规则运动,掉血飘字等. 下面先上代码 using UnityEngin ...

  3. 自定义曲线_完美动力小课堂:AnimationCurve(动画曲线编辑) 如何使用?

    AnimationCurve(动画曲线编辑) 使用简介-支持nuke10及以上版本 在平时制作中偶尔会用到一些曲线,大家在各自的小本本上也会保留一些曲线动画表达式,但是一旦使用时,通常需要改数值比较麻 ...

  4. Unity Dotween插件的运动曲线(Ease)介绍Ease选项Ease效果示例以及C#修改动画曲线功能

    前言 我们在制作动画时经常使用这个Dotween插件,在移动.旋转.透明度等等参数的控制都可以使用该插件,而且在这个插件上的控制动画可以设置曲线,内置的曲线有这些: 内置曲线 以InOutSine的曲 ...

  5. Unity中的Animator动画详解

    Unity中的Animator动画详解 Animator动画导入 Animator动画详解 动画类型选择 Rig面板属性 Mode面板属性 Animation面板属性 动画片段 控制使用 Animat ...

  6. html动画曲线快速结束,CSS3 animation动画

    CSS3 animation动画 1.@keyframes 定义关键帧动画 2.animation-name 动画名称 3.animation-duration 动画时间 4.animation-ti ...

  7. swiftui动画之tab自定义切换动画_Unity动画系统详解1:在Unity中如何制作动画?

    摘要:在场景中加入动态的物体,可以让整个场景更加生动.真实.Unity场景中的物体可以通过制作动画,让物体动起来.简单的动画如物体的移动.旋转(比如旋转的风扇.闪烁不定的灯泡等),复杂的动画如游戏中角 ...

  8. iOS核心动画详解swift版----基础动画

    2019独角兽企业重金招聘Python工程师标准>>> iOS核心动画详解swift版---基础动画 创建工程,添加2个ViewController,通过rootViewContro ...

  9. Unity 3D 动画系统(Mecanim)|| Unity 3D 人形角色动画(Avatar)

    Unity 3D 动画系统(Mecanim) Mecanim 动画系统是 Unity 公司推出的全新动画系统,具有重定向.可融合等诸多新特性,可以帮助程序设计人员通过和美工人员的配合快速设计出角色动画 ...

  10. Unity动画系统详解1:在Unity中如何制作动画?

    摘要:在场景中加入动态的物体,可以让整个场景更加生动.真实.Unity场景中的物体可以通过制作动画,让物体动起来.简单的动画如物体的移动.旋转(比如旋转的风扇.闪烁不定的灯泡等),复杂的动画如游戏中角 ...

最新文章

  1. 《评人工智能如何走向新阶段》后记(再续2)
  2. 使用python重命名某个文件下的所有的文件
  3. soapui返回值类型都有哪些_货架的类型都有哪些呢
  4. 【Linux网络编程】原始套接字能干什么?
  5. 微软同步框架入门开篇(附SnapShot快照Demo)
  6. 论文 | 港中文自动驾驶点云上采样方法
  7. c语言交通灯程序闪烁,用C语言编写的交通灯程序
  8. go 并发的非阻塞缓存
  9. bat批处理命令拨号上网
  10. Iphone革了谁的命?
  11. 远程预付费电能管理系统在工业园的应用,主要功能有哪些?
  12. 华为手机隐藏app图标_华为手机怎么隐藏应用图标
  13. python国外文献_python的英文文献
  14. 白话大数据之HDFS
  15. MySQL高级---04
  16. TCP扫描增强器实现65000端口,10S完成,快准狠(Go语言编程)
  17. Educational Codeforces Round 40千名记
  18. 【ICDAR 2023 X 阿里安全】挑战赛正式启动!篡改文本分类和检测两大赛题!
  19. RocketMQ吐血整理
  20. 震惊!!C++居然可以发出声音!

热门文章

  1. Minimum supported Gradle version is 6.1.1. Current version is 5.6.4.
  2. 在线 excel 产品技术调研
  3. 大数据就业方向_学大数据就业前景如何,就业方向有哪些?
  4. win7系统电脑蓝屏怎么解决,如何解决win7电脑蓝屏
  5. Win11系统Windows.old怎么删?Windows.old删不了怎么办?
  6. C语言从入门到入土---初识C语言
  7. Jenkins的分布式构建及部署(master~slaver)
  8. springBoot2学习
  9. zencart模板制作步骤详解
  10. 李峋同款爱心代码!跳动的心,给你爱的人一个惊喜!