Unity:问答游戏多选按钮颜色
Unity: Quiz game multiple choice button colors
我正在做一个多项选择问答游戏,如果点击了错误的答案,我会尝试用绿色突出显示正确答案。例如,如果我有 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();
}
}
这是一个 class 只保存我的数据
public class answerData {
public string answerTxt;
public bool isCorrect;
}
ButtonClick 函数中正在使用此代码(问题开头的代码)-> 这一行
gameController.AnswerButtonClick (AnswerData.isCorrect);
在检查器中,我通过布尔指定正确答案是什么
*** 此处有新脚本 "Picking up the Correct button"
// 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());
}
}
谢谢
就像在 if
语句中使用 GetComponent<Button>().image.color = Color.green;
一样,您可以简单地在 else
语句中添加 /*Button A*/.image.color = Color.green;
。
@BadWolf 在评论中说的是正确答案。如果您需要更多编码帮助,则必须包含更多信息。你如何确定Correct Button
?它在场景中有特殊的名称吗? gameController
知道哪个按钮是正确的吗?似乎 gameController 知道哪个 Button 是正确的,所以你应该创建一个看起来像这样的方法:
public Button GetCorrectButton(){
return theCorrectButton;
}
我不知道您如何查看按钮是否正确的代码,但我猜您有一些方法可以使用正确的按钮。在 GetCorrectButton 方法中找到正确的按钮并return它。
然后您将在您的代码中使用它,例如:
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;
}
}
如果我再弄一些information/code看看,我可以更具体一点!
我假设所有选项按钮都附加了相同的脚本。
创建一个委托并在附加到您的选项按钮的脚本中注册它。
像这样
创建委托和事件
#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
希望此解决方案对您有所帮助。;)
我正在做一个多项选择问答游戏,如果点击了错误的答案,我会尝试用绿色突出显示正确答案。例如,如果我有 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();
}
}
这是一个 class 只保存我的数据
public class answerData {
public string answerTxt;
public bool isCorrect;
}
ButtonClick 函数中正在使用此代码(问题开头的代码)-> 这一行
gameController.AnswerButtonClick (AnswerData.isCorrect);
在检查器中,我通过布尔指定正确答案是什么
*** 此处有新脚本 "Picking up the Correct button"
// 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());
}
}
谢谢
就像在 if
语句中使用 GetComponent<Button>().image.color = Color.green;
一样,您可以简单地在 else
语句中添加 /*Button A*/.image.color = Color.green;
。
@BadWolf 在评论中说的是正确答案。如果您需要更多编码帮助,则必须包含更多信息。你如何确定Correct Button
?它在场景中有特殊的名称吗? gameController
知道哪个按钮是正确的吗?似乎 gameController 知道哪个 Button 是正确的,所以你应该创建一个看起来像这样的方法:
public Button GetCorrectButton(){
return theCorrectButton;
}
我不知道您如何查看按钮是否正确的代码,但我猜您有一些方法可以使用正确的按钮。在 GetCorrectButton 方法中找到正确的按钮并return它。
然后您将在您的代码中使用它,例如:
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;
}
}
如果我再弄一些information/code看看,我可以更具体一点!
我假设所有选项按钮都附加了相同的脚本。
创建一个委托并在附加到您的选项按钮的脚本中注册它。
像这样
创建委托和事件
#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
希望此解决方案对您有所帮助。;)