Unity3d “协助”;呼吸“;字体大小(使用协同程序增加和减少)

Unity3d “协助”;呼吸“;字体大小(使用协同程序增加和减少),unity3d,fonts,coroutine,Unity3d,Fonts,Coroutine,我试图使我的“开始游戏”按钮的字体“呼吸”,增加和减少大小使用协同程序 字体大小从2.0开始,开始时增加到2.20(递增+0.01直到达到2.20),然后回落到2.0(递增-0.01直到达到2.0),然后重复 我的代码在第一部分工作得很好,它一直增加到2.20,但出于某种原因,它没有从2.20减少到2.0。有人知道我做错了什么吗 public class Font_Breathing : MonoBehaviour { public TMP_Text startGame; private fl

我试图使我的“开始游戏”按钮的字体“呼吸”,增加和减少大小使用协同程序

字体大小从2.0开始,开始时增加到2.20(递增+0.01直到达到2.20),然后回落到2.0(递增-0.01直到达到2.0),然后重复

我的代码在第一部分工作得很好,它一直增加到2.20,但出于某种原因,它没有从2.20减少到2.0。有人知道我做错了什么吗

public class Font_Breathing : MonoBehaviour {

public TMP_Text startGame;
private float change = 0.01f;
private float delay = 0.0f;

void Start()
{
    StartCoroutine(IncreaseFont());
}

void Update()
{

}

IEnumerator IncreaseFont()
{

    while (startGame.fontSize >= 2.0f)
    {
        yield return new WaitForSeconds(delay);

        startGame.fontSize += change;

        if (startGame.fontSize >= 2.20f)
        {
            StartCoroutine(DecreaseFont());
        }
    }

}

IEnumerator DecreaseFont()
{
    while (startGame.fontSize >= 2.20f)
    {
        yield return new WaitForSeconds(delay);

        startGame.fontSize -= change;

        if (startGame.fontSize <= 2.0f)
        {
            StartCoroutine(IncreaseFont());
        }
    }

}
公共类字体\u呼吸:单一行为{
公共TMP_文本开始名称;
私人浮动变化=0.01f;
专用浮点延迟=0.0f;
void Start()
{
start例程(IncreaseFont());
}
无效更新()
{
}
IEnumerator递增字体()
{
而(startGame.fontSize>=2.0f)
{
产生返回新WaitForSeconds(延迟);
startGame.fontSize+=更改;
如果(startGame.fontSize>=2.20f)
{
Start例程(DecreaseFont());
}
}
}
IEnumerator DecreaseFont()
{
而(startGame.fontSize>=2.20f)
{
产生返回新WaitForSeconds(延迟);
startGame.fontSize-=更改;

如果(startGame.fontSize我认为您的逻辑是错误的,因此您总是增加字体,请尝试下面的方法,并告诉我您的操作方法

此外,我认为您需要手动告诉协同程序停止,否则它将无休止地运行,因此请查看运行
stopcroutine

IEnumerator IncreaseFont()
{

  while (startGame.fontSize < 2.20f)
  {
    yield return new WaitForSeconds(delay);

    startGame.fontSize += change;

    if (startGame.fontSize >= 2.20f)
    {
        StartCoroutine(DecreaseFont());
        StopCoroutine(IncreaseFont());
    }
  }
}


IEnumerator DecreaseFont()
{
  while (startGame.fontSize > 2.0f)
  {
    yield return new WaitForSeconds(delay);

    startGame.fontSize -= change;

    if (startGame.fontSize <= 2.0f)
    {
        StartCoroutine(IncreaseFont());
        StopCoroutine(DecreaseFont());
    }
  }

}
IEnumerator IncreaseFont()
{
而(startGame.fontSize<2.20f)
{
产生返回新WaitForSeconds(延迟);
startGame.fontSize+=更改;
如果(startGame.fontSize>=2.20f)
{
Start例程(DecreaseFont());
StopCorroutine(IncreaseFont());
}
}
}
IEnumerator DecreaseFont()
{
而(startGame.fontSize>2.0f)
{
产生返回新WaitForSeconds(延迟);
startGame.fontSize-=更改;

如果(startGame.fontSize一个协同程序对于这个任务(一个简单的循环器)来说似乎有点过分了。为什么不尝试一些更简单的方法呢

例如:


为了具体回答您的协同程序问题,我似乎找不到它的直接缺点,但我会做一些更改以排除可能性

你有:

IEnumerator IncreaseFont() {

    // !!! This check looks like a copy-paste error from your DecreaseFont()
    //     Also, do not check for equality otherwise you will go PAST your cut-off
    while (startGame.fontSize >= 2.0f) {
        yield return new WaitForSeconds(delay);

        startGame.fontSize += change;

        //  !!! This check is redundant -> Move it outside the loop
        if (startGame.fontSize >= 2.20f) {
            StartCoroutine(DecreaseFont());
        }
    }
}
我想:

IEnumerator IncreaseFont() {

    while (startGame.fontSize < 2.20f) {
        // yielding for null just waits for next frame, gives smoother animation
        yield return null; 

        startGame.fontSize += change;
    }

    // If you reach this point, the while loop has exited and can assumed that fontSize >= 2.2f
    StartCoroutine(DecreaseFont());
}
IEnumerator IncreaseFont(){
而(startGame.fontSize<2.20f){
//为null让步只需等待下一帧,即可提供更平滑的动画
收益返回空;
startGame.fontSize+=更改;
}
//如果达到此点,则while循环已退出,可以假定fontSize>=2.2f
Start例程(DecreaseFont());
}

Hi Dylan,谢谢你的评论!我试着用你的代码进行更改,但在增加后仍然停止,保持在2.20,并且没有开始减少。这包括我对
stopCoroutine()所做的编辑吗
??@RamKillnaniYes我在评论之前添加了这一点。嗯,很抱歉,这并没有解决您的问题,我建议您最后添加一些调试,以确保您按照预期输入的函数可以帮助您缩小问题。无忧!我将尝试调试一点,看看我能做些什么。感谢您的尝试:)我尝试过这种方法,但效果很好,我认为我调整变量的方法不正确。如果我错了,请纠正我:Size1是字体的最小大小,所以在我的例子中,我会使用2。Size2是最大值,所以我会使用2.20,因为我们使用的是Mathf,它从0-1开始,我们将其重置为2-2.20?,然后乘以速度。当
minSize
totalOffset
计算出哪一个更小以及差异是什么时,哪一个大小更大并不重要。如果动画太快,请使用较小的速度值。如果希望从增大到减小的过渡更平滑,请更改缓和度(通常使用S曲线,也称为“缓入缓出”)因此,
Size1=2.0f
Size2=2.2f
来满足您的需要。然后
Speed=1f
将在一秒钟内从最小值变为最大值(并在一秒钟内返回)。
Speed=2f
将在半秒钟内从最小值变为最大值。(将
Speed
想象为“每秒半个周期”)@拉姆基尔纳尼补充道
IEnumerator IncreaseFont() {

    while (startGame.fontSize < 2.20f) {
        // yielding for null just waits for next frame, gives smoother animation
        yield return null; 

        startGame.fontSize += change;
    }

    // If you reach this point, the while loop has exited and can assumed that fontSize >= 2.2f
    StartCoroutine(DecreaseFont());
}