如何通过Unity中的脚本更改精灵的alpha

时间:2015-09-16 19:11:35

标签: c# button unity3d alpha flashing

我有4个按钮,当按下按钮时它们各自闪烁。但是,如果您按下按钮,则当按钮较暗(闪烁)时单击另一个按钮,然后该按钮会保持黑暗。我想在禁用它们之前重置精灵的颜色,但它不起作用。按下按钮后,它们不再闪烁....

以下是代码:

void Start () {
        easy.GetComponent<SpriteRenderer>().color = color;
    }

    void Update () {
        if(difficultySelected[0] == true){
            enemyBehavior.shotsPerSecond = 0.2f;
            //Enabling the selected button
            easy.enabled = true;

            //Reseting the colors
            normal.GetComponent<SpriteRenderer>().color = color;
            hard.GetComponent<SpriteRenderer>().color = color;
            expert.GetComponent<SpriteRenderer>().color = color;

            //Disabling the 'unselected' buttons
            normal.enabled = false;
            hard.enabled = false;
            expert.enabled = false;

            easy.Play("Difficulty");
        }else if(difficultySelected[1] == true){
            enemyBehavior.shotsPerSecond = 0.5f;
            normal.enabled = true;

            easy.GetComponent<SpriteRenderer>().color = color;
            hard.GetComponent<SpriteRenderer>().color = color;
            expert.GetComponent<SpriteRenderer>().color = color;

            easy.enabled = false;
            hard.enabled = false;
            expert.enabled = false;

            normal.Play("Difficulty");
        }else if(difficultySelected[2] == true){
            enemyBehavior.shotsPerSecond = 1;
            hard.enabled = true;

            easy.GetComponent<SpriteRenderer>().color = color;
            normal.GetComponent<SpriteRenderer>().color = color;
            expert.GetComponent<SpriteRenderer>().color = color;

            easy.enabled = false;
            normal.enabled = false;
            expert.enabled = false;

            hard.Play("Difficulty");
        }else if(difficultySelected[3] == true){
            enemyBehavior.shotsPerSecond = 2;
            expert.enabled = true;

            easy.GetComponent<SpriteRenderer>().color = color;
            normal.GetComponent<SpriteRenderer>().color = color;
            hard.GetComponent<SpriteRenderer>().color = color;

            easy.enabled = false;
            normal.enabled = false;
            hard.enabled = false;

            expert.Play("Difficulty");
        }
    }

1 个答案:

答案 0 :(得分:0)

你可以写:

color.a = 1f; // value between 0 and 1, where 1 is opaque

//Resetting the colors
normal.GetComponent<SpriteRenderer>().color = color;
hard.GetComponent<SpriteRenderer>().color = color;
expert.GetComponent<SpriteRenderer>().color = color;