QuestionCard.cs 2.5 KB

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