与“呼吸"相关联字体大小(使用协程增加和减少) [英] Assitance with "breathing" font size (increasing and decreasing using coroutines)

查看:103
本文介绍了与“呼吸"相关联字体大小(使用协程增加和减少)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使开始游戏"按钮的字体呼吸",使用协同程序来增大和减小字体大小.

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

}

}

解决方案

协程对于此任务(一个简单的循环车)似乎有点过分杀伤力.为什么不尝试更简单的东西?

例如:

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

I'm trying to make the font of my 'Start Game' button "breath", increasing and decreasing in size using Coroutines.

Font size starts with 2.0, onStart it increases to 2.20 (progresively by +0.01 till it reaches 2.20) and then drops back to 2.0 (progresively by -0.01 till it reaches 2.0) and then repeat.

My code works for the first part perfectly where it increase all the way to 2.20 but for some reason it is not decreasing back to 2.0 from 2.20. Does anyone know what i'm doing wrong?

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

}

}

解决方案

A Coroutine seems a bit overkill for this task (a simple cycler). Why not try something simpler?

eg:

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


To answer your coroutine question specifically, I can't seem to find a direct fault with it, but I would make some changes to rule out possibilities.

You had:

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

I would make it:

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

这篇关于与“呼吸"相关联字体大小(使用协程增加和减少)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆