質問(および回答)を表示する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
出典
2016-04-18 13:39:46
yes