• 主页
  • Unity:益智游戏多选按钮颜色

Unity:益智游戏多选按钮颜色

我正在做一个多项选择问答游戏,如果点击了错误的答案,我会尝试用绿色突出显示正确的答案。例如,如果我有A,B,C和D,"A“是正确的答案。我想如果我选择"B“将B变为红色,并显示绿色的正确答案,在本例中为"A”。现在,如果我点击正确的答案,它会显示为绿色,这是我想要的。如果我按错了答案,它就会显示红色。我缺少的是,当我点击错误的答案时,我也想突出显示正确的答案。

下面是我的函数:

在游戏控制器中:

public bool theAnswerIsCorrect;

public bool IsCorrected()
{
    return theAnswerIsCorrect;
}

public void AnswerButtonClick(bool isCorrect)
{
    if (isCorrect)
    {
        Debug.Log("I'm Correct");
        theAnswerIsCorrect = true;
        playerScore += currentRoundData.pointAddedForCorrectAnswer;
        scoreDisplayText.text = "Score: " + playerScore.ToString();


    }

    else
        theAnswerIsCorrect = false;



    // Do we still have questions?
    if (questionPool.Length > questionIndex + 1)
    {
        //questionIndex++;

        UpdateQuestionIndex();
        StartCoroutine(DelayTime(3));
      //  ShowQuestion();
    }

    else
    {
        EndRound();
    }
}

这是一个只保存我的数据的类

public class answerData {

public string answerTxt;
public bool isCorrect;


}

这在->这一行的ButtonClick函数(请求问题的代码)中使用

        gameController.AnswerButtonClick (AnswerData.isCorrect);

在检查员中,我根据布尔值指定的正确答案是什么

*这里的新脚本“拿起正确的按钮”

// store reference to btn text
public Text answerText;

private answerData AnswerData;
private GameController gameController;

public static AnswerButton _instace;

private void Awake()
{
    _instace = this;
}

// Use this for initialization
void Start () 
{
    gameController = FindObjectOfType<GameController> ();


}

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

IEnumerator ReturnButtonColor()
{
    yield return new WaitForSeconds(2.9f);
    GetComponent<Button>().image.color = Color.white;
    Debug.Log("HiB");


}


public void HandleClick()
{
    gameController.AnswerButtonClick (AnswerData.isCorrect);

    if (gameController.IsCorrected())
    {
       GetComponent<Button>().image.color = Color.green;
        Debug.Log("im true");
        StartCoroutine(ReturnButtonColor());


    }

    else
    {
        GetComponent<Button>().image.color = Color.red;
      //  gameController.IsCorrected().image.color = Color.green;
        StartCoroutine(ReturnButtonColor());

    }


}

谢谢

转载请注明出处:http://www.jxbyjx.net/article/20230524/1658695.html