@export_exp_easing
2026/4/14大约 4 分钟
最后更新日期:2026-04-16
最后同步日期:2026-04-15 | Godot 官方原文 — @export_exp_easing
@export_exp_easing
定义
@export_exp_easing 是 GDScript 中的一个注解(annotation),用来在 Godot 编辑器的检查器面板中暴露一个缓动曲线编辑器。与普通的 @export_range 不同,它提供了一个可视化的曲线编辑控件,让你通过拖动控制点来调整缓动效果(比如"先慢后快"、"先快后慢"等动画过渡曲线)。
简单说,如果你做过 PPT 动画,应该见过"淡入淡出"效果里那些"缓入缓出"的曲线选项。@export_exp_easing 就是让你在 Godot 里也能通过一个直观的曲线控件来调整动画的"节奏感",而不是手动输入抽象的数字。
在 C# 中,对应的写法是 [Export(PropertyHint.ExpEasing)]。
语法
C#
// 基本缓动曲线导出
[Export(PropertyHint.ExpEasing)]
public float ExEasingValue = 1.0f;
// 带额外标志
[Export(PropertyHint.ExpEasing, "attenuation")]
public float ExAttenuation = 1.0f;GDScript
# 基本缓动曲线导出
@export_exp_easing var ex_easing_value: float = 1.0
# 带额外标志:attenuation(衰减)
@export_exp_easing("attenuation") var ex_attenuation: float = 1.0
# 带额外标志:positive_only(仅正值)
@export_exp_easing("positive_only") var ex_positive_easing: float = 1.0参数说明
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
"attenuation" | String | 否 | 额外提示字符串。将曲线方向反转,用于"衰减"效果(如声音渐弱) |
"positive_only" | String | 否 | 额外提示字符串。限制只能选择正值区域,不允许负值 |
"attenuation,positive_only" | String | 否 | 可以同时使用多个标志,用逗号分隔 |
返回值
@export_exp_easing 是一个注解,没有返回值。变量的值是一个 float,表示缓动曲线的参数。
代码示例
C#
using Godot;
public partial class TweenDemo : Node
{
// ===== 基础用法:缓动曲线参数 =====
[Export(PropertyHint.ExpEasing)]
public float ExEasingValue = 1.0f;
public override void _Ready()
{
GD.Print($"缓动参数: {ExEasingValue}");
}
// 运行结果: 缓动参数: 1
// ===== 实际场景:控制动画过渡效果 =====
[Export(PropertyHint.ExpEasing)]
public float ExFadeInEasing = 2.0f;
[Export(PropertyHint.ExpEasing, "attenuation")]
public float ExFadeOutEasing = 2.0f;
private Node3D _targetNode;
public void FadeIn()
{
var tween = CreateTween();
// 用 ease 函数配合缓动值实现平滑淡入
tween.TweenProperty(_targetNode, "position:y", 100f, 0.5f)
.SetEase(Tween.EaseType.Out)
.SetTrans(Tween.TransitionType.Expo);
GD.Print($"使用缓动值 {ExFadeInEasing} 播放淡入动画");
}
// 运行结果: 使用缓动值 2 播放淡入动画
// ===== 进阶用法:自定义缓动计算 =====
[Export(PropertyHint.ExpEasing)]
public float ExCustomEasing = 1.5f;
[Export(PropertyHint.ExpEasing, "attenuation,positive_only")]
public float ExAttenuation = 1.0f;
public float ApplyEasing(float t)
{
// t 是 0 到 1 之间的插值因子
if (t <= 0.0f) return 0.0f;
if (t >= 1.0f) return 1.0f;
// 使用指数缓动公式
if (ExCustomEasing > 0.0f)
{
return Mathf.Pow(t, ExCustomEasing);
}
else
{
return 1.0f - Mathf.Pow(1.0f - t, -ExCustomEasing);
}
}
public void DemoEasing()
{
float[] values = { 0.0f, 0.25f, 0.5f, 0.75f, 1.0f };
foreach (float t in values)
{
GD.Print($" t={t} -> {ApplyEasing(t):F3}");
}
}
// 运行结果: t=0 -> 0.000
// 运行结果: t=0.25 -> 0.125
// 运行结果: t=0.5 -> 0.354
// 运行结果: t=0.75 -> 0.650
// 运行结果: t=1 -> 1.000
}GDScript
extends Node
# ===== 基础用法:缓动曲线参数 =====
@export_exp_easing var ex_easing_value: float = 1.0
func _ready():
print("缓动参数: %.1f" % ex_easing_value)
# 运行结果: 缓动参数: 1.0
# ===== 实际场景:控制动画过渡效果 =====
@export_exp_easing var ex_fade_in_easing: float = 2.0
@export_exp_easing("attenuation") var ex_fade_out_easing: float = 2.0
func fade_in():
var tween := create_tween()
tween.tween_property(self, "position:y", 100.0, 0.5)\
.set_ease(Tween.EASE_OUT)\
.set_trans(Tween.TRANS_EXPO)
print("使用缓动值 %.1f 播放淡入动画" % ex_fade_in_easing)
# 运行结果: 使用缓动值 2.0 播放淡入动画
# ===== 进阶用法:自定义缓动计算 =====
@export_exp_easing var ex_custom_easing: float = 1.5
@export_exp_easing("attenuation,positive_only") var ex_attenuation: float = 1.0
func apply_easing(t: float) -> float:
if t <= 0.0:
return 0.0
if t >= 1.0:
return 1.0
if ex_custom_easing > 0.0:
return pow(t, ex_custom_easing)
else:
return 1.0 - pow(1.0 - t, -ex_custom_easing)
func demo_easing():
var values := [0.0, 0.25, 0.5, 0.75, 1.0]
for t in values:
print(" t=%.2f -> %.3f" % [t, apply_easing(t)])
# 运行结果: t=0.00 -> 0.000
# 运行结果: t=0.25 -> 0.125
# 运行结果: t=0.50 -> 0.354
# 运行结果: t=0.75 -> 0.650
# 运行结果: t=1.00 -> 1.000注意事项
@export_exp_easing只能用于float类型的变量。- 编辑器中显示的是一个可视化的缓动曲线控件,你可以拖动曲线的控制点来调整效果。
attenuation标志用于"衰减"场景(如音量渐弱),它会反转曲线的方向。positive_only标志限制曲线只能取正值,防止出现负值导致的异常。- 变量的值是一个
float参数,需要配合缓动函数(如ease()或自定义公式)才能产生实际的缓动效果。 - C# 中使用
[Export(PropertyHint.ExpEasing)]或[Export(PropertyHint.ExpEasing, "attenuation")]的形式。
