我是一名教育研究者,为我的博士课程开发了一个游戏。我正在通过教程和论坛来学习。
我有一个小测验面板(即四个按钮-一个正确的答案和三个错误的答案)。
我想知道如何提供反馈提示的顺序,无论哪个错误的按钮被点击。
这些是面板的一些图片和我目前拥有的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PPLQuizManager : MonoBehaviour
{
public GameObject HNSCanvas;
[Header("PPL Left Panel")]
public GameObject PPLLeftPanel;
public RectTransform PPLParentLeftPanel;
public GameObject PPLTextBox;
public GameObject PPLContinue;
public GameObject PPLReturn;
//Pictures that illustrate the tutor's reaction
public List<Sprite> questionimages;
public List<Sprite> HintImages;
public List<Sprite> CorrectImages;
public GameObject PPLStringImages;
[Header("PPL Right Panel")]
public GameObject PPLRightPanel;
public GameObject PPLChoice1;
public GameObject PPLChoice2;
public GameObject PPLChoice3;
public GameObject PPLChoice4;
public int PPLChoiceMade;
[Header("PPL Bottom Panel")]
public GameObject PPLBottomPanel;
public GameObject PPLHint1TextBox;
public GameObject PPLHint2TextBox;
public GameObject PPLHint3TextBox;
public GameObject PPLWinTextBox;
public GameObject PPLHintPic;
public GameObject PPLBadge;
public GameObject PPLMedalPic;
[Header("PPL Center Panel")]
public GameObject PPLCenterPanel;
public GameObject PPLHintPic1;
public GameObject PPLHintPic2;
public GameObject PPLHintPic3;
public GameObject PPLWinPic;
public void PPLChoiceOption1()
{
PPLChoice1.GetComponent<Button>().interactable = false;
PPLTextBox.GetComponent<Text>().text = "Read the hint. Try again.";
showRandomHintImage();
PPLChoiceMade = 1;
}
public void PPLChoiceOption2()
{
PPLChoice2.GetComponent<Button>().interactable = false;
PPLTextBox.GetComponent<Text>().text = "Congratulations!";
showRandomCorrectImage();
PPLChoiceMade = 2;
}
public void PPLChoiceOption3()
{
PPLChoice3.GetComponent<Button>().interactable = false;
PPLTextBox.GetComponent<Text>().text = "Here's a hint!";
showRandomHintImage();
PPLChoiceMade = 3;
}
public void PPLChoiceOption4()
{
PPLChoice4.GetComponent<Button>().interactable = false;
PPLTextBox.GetComponent<Text>().text = "Hint time. Go for it!";
showRandomHintImage();
PPLChoiceMade = 4;
}
public void showRandomQuestionImage()
{
int count = questionimages.Count;
int index = Random.Range(0, count);
Image image1 = PPLStringImages.GetComponent<Image>();
image1.sprite = questionimages[index];
}
public void showRandomCorrectImage()
{
int count = CorrectImages.Count;
int index = Random.Range(0, count);
Image image2 = PPLStringImages.GetComponent<Image>();
image2.sprite = CorrectImages[index];
}
public void showRandomHintImage()
{
int count = HintImages.Count;
int index = Random.Range(0, count);
Image image3 = PPLStringImages.GetComponent<Image>();
image3.sprite = HintImages[index];
}
private void Start()
{
showRandomQuestionImage();
}
// Update is called once per frame
void Update()
{
//check the rest of this post
}
}我所做的是让它发挥作用,就是把每个提示分配给一个错误的答案按钮。但是这个方法对我的研究来说并不有趣,特别是因为第三个提示实际上是给出了答案。因此,如果一个学生点击一个错误的答案,我手动分配了第三个提示,他们会得到答案的银盘,我的数据是糟糕的。=(
只是为了记住:,我需要显示提示1,2和3,无论学生单击哪个错误按钮。
是否有一种更经济有效的方法来管理这些面板游戏对象?
// Update is called once per frame
void Update()
{
//PPL buttons and answer management
if (PPLChoiceMade == 1)
{
PPLCenterPanel.SetActive(true);
PPLHintPic1.SetActive(true);
PPLHintPic2.SetActive(false);
PPLHintPic3.SetActive(false);
PPLHint1TextBox.SetActive(true);
PPLHint2TextBox.SetActive(false);
PPLHint3TextBox.SetActive(false);
}
if (PPLChoiceMade == 2)
{
PPLCenterPanel.SetActive(true);
PPLHintPic1.SetActive(false);
PPLHintPic2.SetActive(false);
PPLHintPic3.SetActive(false);
PPLWinPic.SetActive(true);
PPLChoice1.SetActive(false);
PPLChoice2.SetActive(true);
PPLChoice3.SetActive(false);
PPLChoice4.SetActive(false);
PPLContinue.SetActive(true);
PPLReturn.SetActive(false);
PPLWinTextBox.SetActive(true);
PPLHint1TextBox.SetActive(false);
PPLHint2TextBox.SetActive(false);
PPLHint3TextBox.SetActive(false);
PPLBadge.SetActive(true);
PPLMedalPic.SetActive(true);
}
if (PPLChoiceMade == 3)
{
PPLCenterPanel.SetActive(true);
PPLHintPic1.SetActive(false);
PPLHintPic2.SetActive(true);
PPLHintPic3.SetActive(false);
PPLHint1TextBox.SetActive(false);
PPLHint2TextBox.SetActive(true);
PPLHint3TextBox.SetActive(false);
}
if (PPLChoiceMade == 4)
{
PPLCenterPanel.SetActive(true);
PPLHintPic1.SetActive(false);
PPLHintPic2.SetActive(false);
PPLHintPic3.SetActive(true);
PPLHint1TextBox.SetActive(false);
PPLHint2TextBox.SetActive(false);
PPLHint3TextBox.SetActive(true);
}任何帮助都将不胜感激!(尤其是因为我已经烧掉了无数的神经元,试图自己找出答案.哈哈!)
发布于 2019-02-07 21:18:52
要回答额外的请求,请创建一个父容器对象,并将所有容器放置在该父容器中,然后以这种方式遍历它们。
public GameObject hintBoxContainer;
private List<GameObject> hintBoxes = new List<GameObject>();
private void Start(){
for(int i =0; i < hintBoxContainer.transform.childCount; i++){
hintBoxes.Add(hintBoxContainer.transform.getChild(i).gameObject);
}
}
private void Update(){
if(PPLChoiceMade == 1){
//Since we know the first hintbox is at the index of 0
for(int i =0; i < hintBoxes.Count; i++){
if(i == 0){
hintBox[i].SetActive(true);
}else{
hintBox[i].SetActive(false);
}
}
}
}作为额外的注意
您应该创建一个单独的函数来初始化列表,这样就不会拥挤您的开始函数,并将更新中的内容分解为函数,从而保持代码的整洁和模块化。记住,要花一些时间来优化,并记住优化和模块化设计技术--在我的早期,许多项目因快速设计系统而不是“正确”而分崩离析:)
发布于 2019-02-07 21:39:07
让我们来看看这个伪码
if student clicks wrong
Increment the number of times they have given wrong answers by 1
What number of incorrect answers have they given?
if 1 then display hint 1
if 2 then display hint 2
if 3 then display hint 3
Allow them to answer again考虑到这个伪代码,您需要一些东西:
public int numberOfWrongAnswers = 0;public string[] hints;public GameObject[] answerButtons;public void PPLChoiceOption(int selection)从这个开始,我会让你的方法像这样
public void PPLChoiceOption(int selection)
{
buttons[selection].GetComponent<Button>().interactable = false;
PPLTextBox.GetComponent<Text>().text = hints[numberOfWrongAnswers - 1];
showRandomHintImage();
PPLChoiceMade = selection;
}请确保将按钮上的引用更改为指向此方法,并在参数中为第一个按钮设置一个0,为下一个按钮设置一个参数,等等。
https://stackoverflow.com/questions/54582138
复制相似问题