2016-04-12 17 views
0

私は簡単なクイズゲームを作っています。私は4つのパネル(質問)を持っており、これらのパネルを無作為化したい。もっと重要なのは、パネルを1つだけ活性化し、パネルが生成されるとそれを再び生成することができないようにすることです。パネルを無作為にランダム化する方法

各パネルは質問であり、質問画像と3つのボタン(回答)が含まれています。

私は4つのパネルを持っています。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(); 
} 
機能 randomizeQuestions

displayquestion私は何が必要でしょうか?

答えて

1

質問(および回答)を表示するUIから接続を解除します。 質問クラスと質問のコレクションがあります。ここで

はどちらか、あなただけのコレクション(AddQuestion(...)メソッドを)埋めるための方法を見つける必要がある簡単な例

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; 
    } 
} 

のテキスト/ 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(); 
} 

は、私はそう良くdoublecheck、これがないIDEで、ここに書かれた、theresのノータイプミスを願っていません:D

関連する問題