【问题标题】:Assitance with "breathing" font size (increasing and decreasing using coroutines)协助“呼吸”字体大小(使用协程增加和减少)
【发布时间】:2021-02-01 18:17:09
【问题描述】:

我正在尝试使“开始游戏”按钮的字体“呼吸”,使用协程增加和减少大小。

字体大小从 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());
        }
    }

}

}

【问题讨论】:

    标签: unity3d fonts coroutine


    【解决方案1】:

    协程对于这个任务来说似乎有点矫枉过正(一个简单的循环器)。为什么不尝试一些更简单的东西?

    例如:

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

    【讨论】:

    • 我尝试了这种方法并且它有效,但它非常活泼,我认为我错误地调整了变量。如果我错了,请纠正我:Size1 是字体的最小大小,所以在我的情况下,我会使用 2。Size2 是最大的,所以我会输入 2.20,因为我们使用的是从 0-1 变化的 Mathf。重新将其重置为 2-2.20?然后乘以速度。
    • 无论哪个 Size 更大,minSizetotalOffset 可以计算出哪个更小,区别是什么。如果动画太快,请使用较小的 Speed 值。如果您想要从增加到减少的更平滑过渡,请更改缓动(通常您会使用 S 曲线,也就是“缓入缓出”)。
    • 所以,Size1 = 2.0fSize2 = 2.2f 可以满足您的需求。然后Speed = 1f 将在一秒钟内从最小到最大(再过一秒钟)。 Speed = 2f 将在半秒内从最小变为最大。
    • (将Speed 视为“每秒半个周期”)
    • @RamKillnani 添加回答
    【解决方案2】:

    我认为你的逻辑是错误的,所以你总是增加你的字体试试下面,让我知道你是怎么做的。

    另外我相信你需要手动告诉协程停止,否则它将无休止地运行,所以看看运行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());
        }
      }
    
    }
    

    【讨论】:

    • 嗨迪伦,感谢您的评论!我尝试使用您的代码进行更改,但它在增加后仍然停止并保持在 2.20 并且不会开始减少。
    • 是否包括我对stopCoroutine() 所做的编辑? @RamKillnani
    • 是的,我在评论之前添加了。
    • 嗯,抱歉,这并没有解决您的问题,我建议的最后一件事是添加一些调试,以确保您按预期输入可以帮助您缩小问题范围的功能
    • 别担心!我会尝试调试一下,看看我能做什么。感谢您的尝试:)
    猜你喜欢
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    相关资源
    最近更新 更多