【问题标题】:Unity: Quiz game multiple choice button colorsUnity:问答游戏多项选择按钮颜色
【发布时间】:2017-01-17 06:51:30
【问题描述】:

我正在做一个多项选择测验游戏,如果单击错误答案,我会尝试以绿色突出显示正确答案。例如,如果我有 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());

    }


}

谢谢

【问题讨论】:

  • 您需要有一种方法来获取对正确答案按钮的引用,以便将其变为绿色。也许你可以向你的游戏控制器添加一个方法来返回正确的按钮,然后在你的 else 子句中调用它。从那里,你知道如何切换它的颜色。

标签: c# user-interface unity3d unity5


【解决方案1】:

我假设所有选项按钮都附加了相同的脚本。

创建一个委托并将其注册到附加到选项按钮的脚本中。

这样

创建委托和事件

#region DELEGATES
public delegate void OnQuestionOptionClicked ();
public static event OnQuestionOptionClicked onQuestionOptionClicked;
#endregion

#region DELEGATE_CALLS
private void RaiseOnQuestionOptionClicked ()
{
    if (onQuestionOptionClicked != null)
        onQuestionOptionClicked ();
}
#endregion

注册

void OnEnable ()
{
    onQuestionOptionClicked += OnQuestionOptionClicked;
}

void OnDisable ()
{
    onQuestionOptionClicked -= OnQuestionOptionClicked;
}

#region DELEGATE_EVENT_LISTENER
void OnQuestionOptionClicked ()
{
    GetComponent<Button>().interactable = false;
    if (AnswerData.isCorrect){
        GetComponent<Button>().image.color = Color.green;
        Debug.Log("im true");
    }
}
#endregion

你的设置方法

public void Setup(answerData data)
{
    AnswerData = data;
    answerText.text = AnswerData.answerTxt;
    GetComponent<Button>().interactable = true;
    GetComponent<Button>().image.color = Color.white;

}

在按钮点击事件上

#region BUTTON_CLICK_LISTENER
public void ButtonClick()
{
    gameController.AnswerButtonClick (AnswerData.isCorrect);

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

    RaiseOnQuestionOptionClicked ();
} 
#endregion

希望此解决方案对您有所帮助。;)

【讨论】:

  • ty,但我实际上不明白你的解决方案。每当我点击错误的按钮时,我想将点击的按钮显示为红色并将正确的按钮更改为绿色。这样我会在转到下一个问题之前向用户显示正确的答案
  • 您的解决方案给出的行为与我目前的行为完全相同。我的问题是,如果用户选择了错误的 asnwer,我无法弄清楚如何将正确的按钮更改为绿色
  • 你有没有在你的项目中添加这个代码。我的意思是使用这个代码??
  • What - "gameController.IsCorrected()" Return 你能分享一下这个函数吗?
  • 已经在问题中了,在问题的第一部分,第一个函数
【解决方案2】:

就像在if 语句中使用GetComponent&lt;Button&gt;().image.color = Color.green; 一样,您只需在else 语句中添加/*Button A*/.image.color = Color.green;

【讨论】:

  • 如果点击了错误的答案,我基本上需要正确的答案才能变成绿色。在第一个 if 语句中这很容易,因为我只是调用检查答案是否正确的函数。但我不确定它在 else 语句中会如何..
  • 如果你学过泛型,只需在 GetComponent&lt;Button&gt; 中,你只需在 &lt;Button&gt; 中输入你命名的 ButtonA(或类)
  • 我正在汇集我所有的按钮。所以实际上我第一次实例化它们..所以我不知道如何获得“BUTTON A”
  • 在初始化按钮的那一刻,在Button 上设置一个Tag,它包含正确的答案
  • 您需要找到一种方法来获取对正确答案按钮的引用。你可以创建一个新的方法/函数来返回正确的按钮来看看你得到了什么。
【解决方案3】:

cmets 中的@BadWolf 所说的是正确答案。如果您需要更多编码帮助,则必须包含更多信息。你如何确定Correct Button?它在场景中有一个特殊的名称吗? gameController 知道哪个按钮是正确的吗?似乎 gameController 知道哪个 Button 是正确的,所以你应该创建一个看起来像这样的方法:

public Button GetCorrectButton(){
    return theCorrectButton;
}

我不知道您如何查看它是否是正确按钮的代码,但我猜您有某种方法可以使用正确按钮。找到正确的按钮并在 GetCorrectButton 方法中返回。

然后您将在代码中使用它,例如:

public void ButtonClick()
{
    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;
        // Like this:
        gameController.GetCorrectButton().image.color = Color.green;
    }
}

如果我得到更多信息/代码,我可以更具体!

【讨论】:

  • 再次嗨 :),你能检查更新的问题吗?
  • 我不能做这个游戏Controller.GetCorrectButton().image.color = Color.green;因为我没有返回按钮我只是返回一个布尔值,按钮本身在不同的脚本中
  • 你需要以某种方式找到按钮。在不同的脚本中,你如何找到它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多