在Unity中重复音频剪辑,间隔越来越小 [英] Repeat audio clip in Unity, with increasingly smaller intervals
问题描述
我希望倒数计时器随着时间的倒数而更快地滴答. 滴答声"是一个很短的音频剪辑.
I want a countdown timer to tick faster as the time counts down. the 'tick' is a very short audioclip.
我已经有了计时器,它每帧都会调用playTickManager().
I've got the timer working which calls playTickManager() every frame.
但是,通过此实现,在取消/重新调用的某些转换中,滴答之间的间隙不一致或重叠,从而导致滴答声不自然.
However with this implementation, at some transitions of cancel/re-invoke, the gap between ticks is inconsistent or overlaps causing an unnatural sounding tick.
有没有更好的方法来实现此目的而又不加速实际的音频剪辑,从而保持音调等?
Is there a better way to implement this without speeding up the actual audioclip, thus maintaining pitch etc?
// This is called every frame by the 'timer' script as it counts down to 0.
// 'seconds' is a float.
void playTickManager()
{
Debug.Log(seconds);
if (seconds >= 20 && tickPlaySpeed != 1)
{
InvokeRepeating("playTickAudio", 1f, 1f);
tickPlaySpeed = 1;
}
else if (seconds >= 16 && seconds < 20 && tickPlaySpeed != 2)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .85f);
tickPlaySpeed = 2;
}
else if (seconds >= 12 && seconds < 16 && tickPlaySpeed != 3)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .7f);
tickPlaySpeed = 3;
}
else if (seconds > 8 && seconds < 12 && tickPlaySpeed != 4)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .55f);
tickPlaySpeed = 4;
}
else if (seconds >= 4 && seconds < 8 && tickPlaySpeed != 5)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .4f);
tickPlaySpeed = 5;
}
else if (seconds > 0 && seconds < 3 && tickPlaySpeed != 6)
{
CancelInvoke("playTickAudio");
InvokeRepeating("playTickAudio", 0f, .25f);
tickPlaySpeed = 6;
}
else if(seconds <= 0)
{
CancelInvoke("playTickAudio");
tickPlaySpeed = 0;
}
}
void playTickAudio()
{
AudioSource.PlayClipAtPoint(tickAudio, gameObject.transform.position, 1f);
}
推荐答案
这是学习非常常见的模式的好机会.
This is a good chance to learn a very common pattern.
这是软件中的 再次打电话给自己" 模式.
This is the "call yourself again" pattern in software.
请注意,这在游戏中非常普遍-尤其是每种武器类型都使用再次致电自己"图案. (简而言之,您不需要使用"invokeRepeating"之类的东西,因为您需要对触发循环进行更多控制.)
Note that this is very, very common in games - notably every type of weapon uses the "call yourself again" pattern. (In short, you don't use something like "invokeRepeating" since you need more control over the firing loop.)
因此,请尝试此教学示例:
So try this ... teaching example:
private float gapToNext;
private void ExcitingCountdown()
{
gapToNext = 2.5f;
PlayOneStep();
}
private void PlayOneStep()
{
if (gapToNext < 0.01f) return;
YourBeepSound.Play();
gapToNext = gapToNext - 0.1f;
Invoke("ExcitingCountdown", gapToNext);
}
简单吧?
请注意,实际上,您的代码如下所示:
Note that in reality, your code would look like this:
只需调整ExcitingCountdown
中的值即可获得所需的探测结果.
Just adjust the values in ExcitingCountdown
to get the sounding result you want.
private float gapToNext;
private int safetyCounter;
void Start()
{
ExcitingCountdown();
}
private void ExcitingCountdown()
{
gapToNext = 2.5f;
safetyCounter = 30;
PlayOneStep();
}
private void PlayOneStep()
{
if (gapToNext < 0.05f || safetyCounter<0)
{
Debug.Log("we're done...!");
return;
}
YourBeepSound.Play();
gapToNext = gapToNext * 0.85f;
--safetyCounter;
Debug.Log("gap is now " +gapToNext.ToString("f4"));
Invoke("ExcitingCountdown", gapToNext);
}
这篇关于在Unity中重复音频剪辑,间隔越来越小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!