C# 游戏画面上的对数淡入淡出
我在Unity3d中创建了一个静态功能,可以在一定数量的帧内淡出音乐。该函数被放置在Unity3d的FixeUpdate()中,以便它根据真实世界的时间进行更新(或者至少是最近的更新) 我目前对线性淡出的计算如下所示:C# 游戏画面上的对数淡入淡出,c#,fade,out,programmatically,C#,Fade,Out,Programmatically,我在Unity3d中创建了一个静态功能,可以在一定数量的帧内淡出音乐。该函数被放置在Unity3d的FixeUpdate()中,以便它根据真实世界的时间进行更新(或者至少是最近的更新) 我目前对线性淡出的计算如下所示: if (CurrentFrames > 0) { AudioSourceObject.volume = ((CurrentFrames / TotalFrames) * TotalVolume); } if (CurrentFrames <= 0) {
if (CurrentFrames > 0) {
AudioSourceObject.volume = ((CurrentFrames / TotalFrames) * TotalVolume);
}
if (CurrentFrames <= 0) {
AudioSourceObject.volume = 0.00f;
return 0;
}
CurrentFrames--;
if(CurrentFrames>0){
AudioSourceObject.volume=((当前帧/总帧)*总音量);
}
如果(CurrentFrames您可以使用Unity的内置类轻松完成此操作。请在类中定义它,并使用Unity检查器绘制对数曲线
然后在淡出类中,执行以下操作:
public AnimationCurve FadeCurve;
//...
if (CurrentFrames > 0) {
float time = CurrentFrames / TotalFrames;
AudioSourceObject.volume = FadeCurve.Evaluate(time) * TotalVolume;
}
//...
它对于实现各种缓和效果非常有用,只需尝试使用不同的曲线即可。非常欢迎您使用内置功能。否则,如果您想要更精细的控制,可以使用调整过的sigmoid函数。通常,sigmoid函数如下所示:()
但您希望淡出功能看起来更像这样:(
其中x是(当前帧/总帧)*3的比率,y是输出音量
但是我是如何从第一个图到第二个图的呢?试着用指数输入(e^(玩这个))的定标器(即3x或5x)和偏移量(即(3x-4)或(3x-6))进行实验在wolfram alpha中,您可以感受到曲线的外观。您不必将曲线与轴的交点与特定数字对齐,因为您可以在代码中调整函数的输入和输出
因此,您的代码如下所示:
if (CurrentFrames > 0) {
// I'd recommend factoring out and simplifying this into a
// function, but I have close to zero C# experience.
inputAdjustment = 3.0
x = ((CurrentFrames / TotalFrames) * inputAdjustment);
sigmoidFactor = 1.0/(1.0 + Math.E ^ ((3.0 * x) - 4.0));
AudioSourceObject.volume = (sigmoidFactor * TotalVolume);
}
if (CurrentFrames <= 0) {
AudioSourceObject.volume = 0.00f;
return 0;
}
CurrentFrames--;
if(CurrentFrames>0){
//我建议将其分解并简化为
//功能,但我几乎没有C#经验。
输入调整=3.0
x=((当前帧/总帧)*输入调整);
sigmoidFactor=1.0/(1.0+Math.E^((3.0*x)-4.0));
AudioSourceObject.volume=(sigmoidFactor*TotalVolume);
}
如果(刚才有人建议我这么做。我一定要试试。谢谢nateslager,我会试试的!:)你最终成功了吗?请随意标记一个正确的答案。