Guest User

Untitled

a guest
Jan 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // ******************************************************************
  6. // See: https://gamedev.stackexchange.com/questions/166759/trying-to-load-json-questions-and-answers-into-a-multiple-choice-quiz-game-in-u
  7. [System.Serializable]
  8. public class QuestionCollection
  9. {
  10. public QuestionChoicesAnswerHintExplanation[] questions;
  11. }
  12. // ******************************************************************
  13.  
  14.  
  15. [System.Serializable]
  16. public class QuestionChoicesAnswerHintExplanation : MonoBehaviour {
  17.  
  18. public string[] question; // An array of strings (rather than just a string) so that line spacing, which doesn't play well with .json, could be preserved
  19. public string[] answerChoices;
  20. public int correctAnswerIndex;
  21. public string hint;
  22. public string explanation;
  23.  
  24. }
  25.  
  26. public QuestionCollection questionCollection; // See: https://gamedev.stackexchange.com/questions/166759/trying-to-load-json-questions-and-answers-into-a-multiple-choice-quiz-game-in-u
  27.  
  28. public QuestionChoicesAnswerHintExplanation[] questions; // This is the fixed size array of the questions, answer choices, answers, hints, and explanations.
  29. private static List<QuestionChoicesAnswerHintExplanation> questionsCopy; // This is the resizeable list (originally a copy of the array). The size of this list will diminish as we remove items after presenting each question.
  30. private QuestionChoicesAnswerHintExplanation currentQuestion; // The current question drawn at random.
  31. private int randomIndex; // the index used to pick the random question.
  32.  
  33. string path; // The path to the .json file storing the questions, answer choices, correct answer index, hints, explanations.
  34. string jsonString; // The contents of the .json file.
  35.  
  36. path = Application.streamingAssetsPath + "/questionsAndAnswers.json";
  37. jsonString = File.ReadAllText(path);
  38.  
  39. questionCollection = JsonUtility.FromJson<QuestionCollection>(jsonString); // questionCollection contains an array of question-answer choices-answer-hint-explanation objects
  40. questions = questionCollection.questions;
  41.  
  42.  
  43. //questions = JsonUtility.FromJson<QuestionChoicesAnswerExplanation[]>(jsonString);
  44.  
  45. Debug.LogWarning("questionCollection:");
  46. Debug.LogWarning(questionCollection);
  47.  
  48. Debug.LogWarning("Questions:");
  49. Debug.LogWarning(questions);
  50.  
  51. if (questionsCopy == null || questionsCopy.Count == 0) // A list with zero elements isn't always null.
  52. {
  53. questionsCopy = questions.ToList<QuestionChoicesAnswerExplanation>();
  54.  
  55. Debug.LogWarning("questionsCopy:");
  56. Debug.LogWarning(questionsCopy);
  57. Debug.LogWarning("questionsCopy.Count:");
  58. Debug.LogWarning(questionsCopy.Count);
  59. Debug.LogWarning("questionsCopy[0]:");
  60. Debug.LogWarning(questionsCopy[0]);
  61. Debug.LogWarning("questionsCopy[1]:");
  62. Debug.LogWarning(questionsCopy[1]);
  63. Debug.LogWarning("questionsCopy[2]:");
  64. Debug.LogWarning(questionsCopy[2]);
  65.  
  66. }
  67.  
  68. PickRandomQuestion();
  69.  
  70. public void PickRandomQuestion()
  71. {
  72. randomIndex = Random.Range(0, questionsCopy.Count);
  73. currentQuestion = questionsCopy[randomIndex];
  74. questionsCopy.RemoveAt(randomIndex);
  75. }
  76.  
  77. {
  78. "questions":
  79.  
  80. [
  81.  
  82. {
  83. "question": ["Trinculo admits 'I have been in such a pickle...' in this comedy:"],
  84. "answerChoices": ["'The Tempest'", "'Henry 4, Part 1'", "'Henry 5'", "'The Merry Wives of Windsor'"],
  85. "correctAnswerIndex": 0,
  86. "explanation": "'Henry 4, Part 1,' and 'Henry 5, are both histories. To be in 'a pickle' usually means to be in a quandary. A broader view of this passage reveals Trinculo to be drunk: nnAlonso: nAnd Trinculo is reeling ripe: where should they nFind this grand liquor that hath gilded 'em? nHow camest thou in this pickle? nnTrinculo: nI have been in such a pickle since I nsaw you last that, I fear me, will never out of nmy bones: I shall not fear fly-blowing.' nn'The Tempest' 5.1."
  87. },
  88. {
  89. "question": ["In 'Hamlet,' this character remarks: 'Neither a borrower nor a lender be; nFor loan oft loses both itself and friend, nAnd borrowing dulls the edge of husbandry. nThis above all: to thine own self be true,nAnd it must follow, as the night the day,nThou canst not then be false to any man:'"],
  90. "answerChoices": ["Fortinbras", "Gertrude", "Polonius", "The Ghost of Hamlet's Father"],
  91. "correctAnswerIndex": 2,
  92. "explanation": "The verbose Polonius is speaking to his son Laertes. 'Hamlet' 1.3."
  93. },
  94. {
  95. "question": ["Roger Daltrey, well into his career with The Who, played the roles for both Dromios, the twin servants, in a 1983 BBC production of:"],
  96. "answerChoices": ["'King John'", "'King Lear'", "'Coriolanus'", "'The Comedy of Errors'"],
  97. "correctAnswerIndex": 3,
  98. "explanation": "'The Comedy of Errors.'"
  99. }
  100.  
  101. ]
  102. }
Add Comment
Please, Sign In to add comment