Unity Quiz Game,选择正确答案时突出显示按钮

时间:2018-02-20 07:37:18

标签: c# unity3d

我想在选择正确答案时将按钮突出显示为绿色,我该怎么做?

AnswerButton附加到脚本

public class AnswerButton : MonoBehaviour 
{
    public Text answerText;

    private GameController gameController;
    private AnswerData answerData;

    void Start()
    {
        gameController = FindObjectOfType<GameController>();
    }

    public void SetUp(AnswerData data)
    {
        answerData = data;
        answerText.text = answerData.answerText;
    }

    public void HandleClick()
    {
        gameController.AnswerButtonClicked(answerData.isCorrect);
    }
}

我的游戏控制器,其中包含按钮和Q n A

public void AnswerButtonClicked(bool isCorrect)
{
    if (isCorrect)
    {
        Debug.Log("Your Answer is Correct");
        playerScore += currentRoundData.pointsAddedForCorrectAnswer;
        // If the AnswerButton that was clicked was the correct answer, add points
        scoreDisplay.text = playerScore.ToString();

    }

    if (qNumber < questionPool.Length - 1)
    // If there are more questions, show the next question
    {
        qNumber++;
        ShowQuestion();
    }
    else
    // If there are no more questions, the round ends
    {
        EndRound();
    }
}

1 个答案:

答案 0 :(得分:0)

非常简单,如果按钮上有图像,这样的东西应该可以使用

GetComponent<Image>().color = Color.green;

我也见过这样的代码

    ColorBlock colors = GetComponent<Button> ().colors;
    colors.normalColor = Color.red;
    GetComponent<Button> ().colors = colors;

请注意,如果按钮已经着色,则会将绿色与现有颜色混合,因此请为您的按钮选择中性色,如白色或浅灰色。