Unity Quiz Game,选择正确答案时突出显示按钮 [英] Unity Quiz Game, Highlight Button when Correct Answer is chosen

查看:215
本文介绍了Unity Quiz Game,选择正确答案时突出显示按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择正确答案后,我想将按钮突出显示为绿色?

I want to highlight the button into green when the correct answer is chosen how can I make that?

附加到脚本中的AnswerButton

AnswerButton attached into the Script

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的我的游戏控制器

My Game Controller which holds the button and 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();
    }
}

推荐答案

非常简单,如果按钮上有图像,类似的东西应该可以工作

Pretty simple, something like this should work if you have an image on your button

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

我也看到过这样的代码

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

请注意,如果按钮已经着色,则会将绿色混入现有颜色,因此请为按钮选择一种中性色,如白色或浅灰色.

Note if the button is already colored, this will mix green into the existing color, so choose a neutral color for your button initially like white or light grey.

这篇关于Unity Quiz Game,选择正确答案时突出显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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