如何将Panels统一随机化

时间:2016-04-12 09:22:59

标签: c# unity5

我正在做一个简单的问答游戏。我有四个小组(问题),我想随机化这些小组。更重要的是,我只想激活一个面板,一旦生成面板,就无法再次生成面板。

注意每个面板都是一个问题,包含一个问题图像和三个按钮(答案)。

所以我有四个小组q1q2q3q4

我知道代码含糊不清,但我是新手,我真的很想知道。

public class GameManager : MonoBehaviour {
public GameObject StartScreen;
public GameObject QuestionScreen;
public GameObject ResultsScreen;
public GameObject[] questions;
public static int CurrentQuestionIndex;

private void ShowstartScreen(){
    StartScreen.SetActive (true);
    QuestionScreen.SetActive (false);
    ResultsScreen.SetActive (false);    
}
private void ShowQuestionScreen(){
    StartScreen.SetActive (false);
    QuestionScreen.SetActive (true);
    ResultsScreen.SetActive (false);    
}public void StartButtonHandler(){
    ShowQuestionScreen ();
}
public void RandomizeQuestion (){
}
public void DisplayQuestion(){
}
void Start (){

    ShowstartScreen ();
}

在函数randomizeQuestionsdisplayquestion中,我需要什么?

1 个答案:

答案 0 :(得分:1)

从显示它们的UI中断开问题(和答案)。 有一个单独的问题类和所有问题的集合。

这是一个简单的例子

using System.Collections.Generic;

public class Question {

    public string question;
    public string[] answer;
    public int correctAnswer;

    public Question(string question, string[] answer, int correctAnswer) {
        this.question = question;
        this.answer = answer;
        this.correctAnswer = correctAnswer;
    }
}

//this could also be part of your monobehaviour, i just like things seperate
public class QuestionCollection {
    List<Question> questions;

    public QuestionCollection() {
        questions = new List<Question>();
    }

    public void AddQuestion(string question, string[] answers, int     correctAnswer) {
        questions.Add(new Question(question, answers, correctAnswer));
    }

    //here's where you get your random question
    public Question GetRandomQuestion() {
        //if theres any questions
        if(questions.Count > 0) {
            //we get a random index
            int randomIndex = Random.Range(0, questions.Count);
            //temporarily store the corresponding question
            Question q = questions[randomIndex];
            //remove it from the collection so it doesnt come up twice
            questions.RemoveAt(randomIndex);
            //and return it to the caller
            return q;
        }
       //there was no questions (either none has been added or were done) so we return null
       return null;
   }
}

您只需要找到一种方法来填充集合(AddQuestion(...)方法),或者查看如何从text / xml / json / ...文件中读取,或者查看编辑器脚本和从脚本化对象派生类。

Ofc用于测试目的,您可以手动填写。要适应上述代码段中的所有内容,您需要执行以下操作。

public class GameManager : MonoBehaviour {
public GameObject StartScreen;
public GameObject QuestionScreen;
public GameObject ResultsScreen;

//those two are not needed anymore
//public GameObject[] questions;
//public static int CurrentQuestionIndex;

//a reference to the collection containing all the questions
QuestionCollection questions;
//the current question, or null if everything has been asked
Question currentQuestion;

private void ShowstartScreen(){
    StartScreen.SetActive (true);
    QuestionScreen.SetActive (false);
    ResultsScreen.SetActive (false);    
}
private void ShowQuestionScreen(){
    StartScreen.SetActive (false);
    QuestionScreen.SetActive (true);
    ResultsScreen.SetActive (false);    
}
public void StartButtonHandler(){
    ShowQuestionScreen ();
}
public void RandomizeQuestion (){
    //get a new random question
    currentQuestion = questions.GetRandomQuestion();
}
public void DisplayQuestion(){
    if(currentQuestion != null) {
         //fill out your Panels with currentQuestion.question, currentQuestion.answer[#]
    }
    else {
         //there is no question left to be asked.
    }

}
void Start (){

    //create the collection
    questions = new QuestionCollection();
    //fill the collection using this scheme
    questions.AddQuestion("Was the question answered?", new string[] { "Yes", "No" }, 0);

    ShowstartScreen ();
}

我希望没有错字,这是写在这里,而不是在IDE中,所以更好的双重检查:D