Created
July 12, 2017 19:37
-
-
Save unity3dcollege/cb3d061e68e35b38bb29b34948b013de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using UnityEngine; | |
public class QuestionCollection : MonoBehaviour | |
{ | |
private QuizQuestion[] allQuestions; | |
private void Awake() | |
{ | |
LoadAllQuestions(); | |
} | |
private void LoadAllQuestions() | |
{ | |
allQuestions = Resources.LoadAll<QuizQuestion>("Questions"); | |
} | |
public QuizQuestion GetUnaskedQuestion() | |
{ | |
ResetQuestionsIfAllHaveBeenAsked(); | |
var question = allQuestions | |
.Where(t => t.Asked == false) | |
.OrderBy(t => UnityEngine.Random.Range(0, int.MaxValue)) | |
.FirstOrDefault(); | |
question.Asked = true; | |
return question; | |
} | |
private void ResetQuestionsIfAllHaveBeenAsked() | |
{ | |
if (allQuestions.Any(t => t.Asked == false) == false) | |
{ | |
ResetQuestions(); | |
} | |
} | |
private void ResetQuestions() | |
{ | |
foreach (var question in allQuestions) | |
question.Asked = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment