| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- public class QuestionCard : MonoBehaviour
- {
- public Text questionText;
- public Text answerText;
- private string questionString = "";
- private string answerString = "";
- private string idString = "";
- private string categoryString = "";
- private Color32 unsafeColor = new Color32(255, 1, 1, 150);
- private Color32 safeColor = new Color32(1, 255, 1, 75);
- [Serializable]
- public class Question {
- public string question;
- public string answer;
- public string id;
- public string category;
- }
- [Serializable]
- public class Questions {
- public List<Question> questionsList = new List<Question>();
- }
- // Start is called before the first frame update
- void Start()
- {
- }
- public void PrepareQuestion()
- {
- questionText = GameObject.Find("QuestionText").GetComponent<Text>();
- answerText = GameObject.Find("AnswerText").GetComponent<Text>();
- }
- private IEnumerator GetQuestionData(bool showAnswer)
- {
- UnityWebRequest www = UnityWebRequest.Get("nordh.xyz/narKampen/dbAccess.php");
- yield return www.SendWebRequest();
- if (www.isNetworkError || www.isHttpError)
- {
- Debug.Log(www.error);
- } else
- {
- // Show result
- string jsonData = www.downloadHandler.text;
- jsonData = "{\"questionsList\" : [ " + jsonData + " ]}";
- Questions qe = new Questions();
- JsonUtility.FromJsonOverwrite(jsonData, qe);
- if (qe.questionsList.Count > 0 && questionText != null )
- {
- if (showAnswer && answerText != null)
- {
- answerText.text = qe.questionsList[0].answer;
- }
- questionText.text = qe.questionsList[0].question;
- }
- questionString = qe.questionsList[0].question;
- answerString = qe.questionsList[0].answer;
- idString = qe.questionsList[0].id;
- categoryString = qe.questionsList[0].category;
- }
- }
- public void GetQuestion(bool showAnswer)
- {
- StartCoroutine(GetQuestionData(showAnswer));
- }
- public void SetQuestionSafe()
- {
- this.GetComponent<Image>().color = safeColor;
- }
- }
|