我正在做一个多项选择测验游戏,如果点击了错误的答案,我会尝试以绿色突出显示正确的答案。例如,如果我有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;
}
这是在Button Click功能中使用的(问题的乞讨中的代码) - >这一行
gameController.AnswerButtonClick (AnswerData.isCorrect);
在检查员我用bool指定什么是正确的答案
***新脚本在这里“拿起正确的按钮”
// 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());
}
}
谢谢
我假设所有选项按钮都附加了相同的脚本。
创建一个委托并在脚本中注册,该脚本附加到您的选项按钮。
像这样
创建委托和事件
#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
希望这个解决方案可以帮到你。;)
就像在GetComponent<Button>().image.color = Color.green;
语句中使用if
一样,你可以简单地在/*Button A*/.image.color = Color.green;
语句中添加else
。
评论中@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;
}
}
如果我得到更多信息/代码,我可以更具体一点!