字符变成纯黑色,而不是还原为原始字符

时间:2019-01-25 10:21:49

标签: unity3d colors transparent

字符变为透明,直到计时器结束,然后不再变回原始颜色,而是变为纯黑色。

    private Color c;

    void Start(){
    c = thePlayer.rend.color;
    }

public void HurtPlayer(int damageToTake)
    {
        healthCount -= damageToTake;
        UpdateHeartMeter ();
        StartCoroutine ("iFrames");
    }

    IEnumerator iFrames(){
            Physics2D.IgnoreLayerCollision (10, 11, true);
            c.a = 0.5f;
            thePlayer.rend.color = c;
            yield return new WaitForSeconds (2f);
            c.a = 1f;
            thePlayer.rend.color = c;
            Physics2D.IgnoreLayerCollision (10, 11, false);
    }

示例:https://imgur.com/a/jFKXvhW

1 个答案:

答案 0 :(得分:0)

这段代码对我有用,我不确定您可能如何解决SpriteRenderer问题。我仅通过单击按钮来启动协程,因为我不知道何时应用此程序。此代码附加到Sprite。

private Color c;
// Use this for initialization
void Start () {
    c = gameObject.GetComponent<SpriteRenderer>().color;
}

// Update is called once per frame
void Update () {
    if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        StartCoroutine("ChangeColor");
    }
}
IEnumerator ChangeColor()
{
    c.a = 0.5f;
    gameObject.GetComponent<SpriteRenderer>().color = c;
    yield return new WaitForSeconds(2);
    c.a = 1f;
    gameObject.GetComponent<SpriteRenderer>().color = c;
}