协助 "breathing" 字体大小(使用协同程序增加和减少)

Assitance with "breathing" font size (increasing and decreasing using coroutines)

我正在尝试使我的 'Start Game' 按钮的字体“呼吸”,使用协同程序增加和减小大小。

字体大小从 2.0 开始,onStart 增加到 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());
        }
    }

}

}

我认为你的逻辑不对,所以你总是在增加你的字体试试下面的方法,让我知道你的进展如何。

此外,我相信您需要手动告诉协程停止,否则它会 运行 无休止地查看 运行ning StopCoroutine

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());
    }
  }

}

Coroutine 对于这个任务(一个简单的循环程序)来说似乎有点矫枉过正。为什么不尝试更简单的方法?

例如:

public class Breathe : MonoBehaviour
{

    public TextMeshProUGUI Text;

    public float Size1;
    public float Size2;

    public AnimationCurve Ease;
    public float Speed;

    // Update is called once per frame
    void Update()
    {
        var minSize = Mathf.Min(Size1, Size2);
        var totalOffset = Mathf.Abs(Size1 - Size2);

        var factor = Ease.Evaluate(Mathf.PingPong(Time.time * Speed, 1));

        Text.fontSize = minSize + totalOffset * factor;
    }
}

具体回答你的协程问题,我似乎找不到它的直接错误,但我会做一些更改以排除可能性。

你有:

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());
}