1

我是一名教育研究员,为我的博士课程开发游戏。我正在通过教程和论坛随时随地学习。

我有这个带有测验面板的场景(即 4 个按钮 - 1 个正确答案和 3 个错误答案)

无论单击了哪个错误按钮,我都想知道如何按顺序提供反馈提示。

这些是面板的一些图片和我目前拥有的代码。

带有画布元素的问题面板图像

 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。


EXTRA REQUEST = ) : 有没有办法以更经济有效的方式管理这些面板游戏对象?

// 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);

         }

任何帮助将不胜感激!(特别是因为我已经烧毁了大量的神经元试图自己弄清楚......哈哈!)

4

2 回答 2

1

要回答您的额外请求,请创建一个父容器对象并将所有容器放在该父容器中,然后以这种方式循环它们

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-07T21:18:52.390 回答
1

让我们用伪代码来看看这个

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

鉴于此伪代码,您将需要一些东西:

  1. 一个计数器来跟踪给出的错误答案的数量

    public int numberOfWrongAnswers = 0;

  2. 提示的数组(或列表),以便给出

    public string[] hints;

  3. 包含对您的按钮的引用的数组

    public GameObject[] answerButtons;

  4. 确定按下哪个按钮的通用方法

    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,下一个按钮设置为 1,等等。

于 2019-02-07T21:39:07.030 回答