QuestionCard.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using UnityEngine.UI;
  7. public class QuestionCard : MonoBehaviour
  8. {
  9. public Text questionText;
  10. public Text answerText;
  11. private string questionString = "";
  12. private string answerString = "";
  13. private string idString = "";
  14. private string categoryString = "";
  15. private Color32 unsafeColor = new Color32(255, 1, 1, 150);
  16. private Color32 safeColor = new Color32(1, 255, 1, 75);
  17. [Serializable]
  18. public class Question {
  19. public string question;
  20. public string answer;
  21. public string id;
  22. public string category;
  23. }
  24. [Serializable]
  25. public class Questions {
  26. public List<Question> questionsList = new List<Question>();
  27. }
  28. // Start is called before the first frame update
  29. void Start()
  30. {
  31. }
  32. public void PrepareQuestion()
  33. {
  34. questionText = GameObject.Find("QuestionText").GetComponent<Text>();
  35. answerText = GameObject.Find("AnswerText").GetComponent<Text>();
  36. }
  37. private IEnumerator GetQuestionData(bool showAnswer)
  38. {
  39. UnityWebRequest www = UnityWebRequest.Get("nordh.xyz/narKampen/dbAccess.php");
  40. yield return www.SendWebRequest();
  41. if (www.isNetworkError || www.isHttpError)
  42. {
  43. Debug.Log(www.error);
  44. } else
  45. {
  46. // Show result
  47. string jsonData = www.downloadHandler.text;
  48. jsonData = "{\"questionsList\" : [ " + jsonData + " ]}";
  49. Questions qe = new Questions();
  50. JsonUtility.FromJsonOverwrite(jsonData, qe);
  51. if (qe.questionsList.Count > 0 && questionText != null )
  52. {
  53. if (showAnswer && answerText != null)
  54. {
  55. answerText.text = qe.questionsList[0].answer;
  56. }
  57. questionText.text = qe.questionsList[0].question;
  58. }
  59. questionString = qe.questionsList[0].question;
  60. answerString = qe.questionsList[0].answer;
  61. idString = qe.questionsList[0].id;
  62. categoryString = qe.questionsList[0].category;
  63. }
  64. }
  65. public void GetQuestion(bool showAnswer)
  66. {
  67. StartCoroutine(GetQuestionData(showAnswer));
  68. }
  69. public void SetQuestionSafe()
  70. {
  71. this.GetComponent<Image>().color = safeColor;
  72. }
  73. }