Переглянути джерело

get random start question from server

Axel Nordh 7 роки тому
батько
коміт
c8e6bf65e0
2 змінених файлів з 52 додано та 8 видалено
  1. 47 5
      Assets/Scripts/QuestionCard.cs
  2. 5 3
      Assets/narKampen.unity

+ 47 - 5
Assets/Scripts/QuestionCard.cs

@@ -1,18 +1,60 @@
-using System.Collections;
+using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine;
+using UnityEngine.Networking;
+using UnityEngine.UI;
 
 
 public class QuestionCard : MonoBehaviour
 public class QuestionCard : MonoBehaviour
 {
 {
+    public Text questionText;
+    public Text answerText;
+    
+    [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
     // Start is called before the first frame update
     void Start()
     void Start()
     {
     {
-        
+        questionText = GameObject.Find("QuestionText").GetComponent<Text>();
+        answerText = GameObject.Find("AnswerText").GetComponent<Text>();
+
+        StartCoroutine(GetQuestion(true));
     }
     }
 
 
-    // Update is called once per frame
-    void Update()
+    IEnumerator GetQuestion(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);
+
+            answerText.text = qe.questionsList[0].answer;
+            questionText.text = qe.questionsList[0].question;
+        }
+
+        yield return null;
     }
     }
 }
 }

+ 5 - 3
Assets/narKampen.unity

@@ -313,6 +313,8 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: f299b2d58078e8d44b925d2d8c48a498, type: 3}
   m_Script: {fileID: 11500000, guid: f299b2d58078e8d44b925d2d8c48a498, type: 3}
   m_Name: 
   m_Name: 
   m_EditorClassIdentifier: 
   m_EditorClassIdentifier: 
+  questionText: {fileID: 0}
+  answerText: {fileID: 0}
 --- !u!1 &901041274
 --- !u!1 &901041274
 GameObject:
 GameObject:
   m_ObjectHideFlags: 0
   m_ObjectHideFlags: 0
@@ -491,7 +493,7 @@ MonoBehaviour:
   m_TargetGraphic: {fileID: 1133529224}
   m_TargetGraphic: {fileID: 1133529224}
   m_HandleRect: {fileID: 1133529223}
   m_HandleRect: {fileID: 1133529223}
   m_Direction: 0
   m_Direction: 0
-  m_Value: 1
+  m_Value: 0
   m_Size: 1
   m_Size: 1
   m_NumberOfSteps: 0
   m_NumberOfSteps: 0
   m_OnValueChanged:
   m_OnValueChanged:
@@ -1136,7 +1138,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMax: {x: 1, y: 1}
   m_AnchorMax: {x: 1, y: 1}
-  m_AnchoredPosition: {x: -0.000071436305, y: -0.000030517354}
+  m_AnchoredPosition: {x: 0.000026486614, y: -0.000010714011}
   m_SizeDelta: {x: 0, y: 0}
   m_SizeDelta: {x: 0, y: 0}
   m_Pivot: {x: 0, y: 1}
   m_Pivot: {x: 0, y: 1}
 --- !u!114 &1715970008
 --- !u!114 &1715970008
@@ -1316,7 +1318,7 @@ RectTransform:
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMin: {x: 0, y: 0}
   m_AnchorMax: {x: 1, y: 1}
   m_AnchorMax: {x: 1, y: 1}
-  m_AnchoredPosition: {x: 0, y: -4.6}
+  m_AnchoredPosition: {x: 0, y: -4.600006}
   m_SizeDelta: {x: 0, y: 9.1}
   m_SizeDelta: {x: 0, y: 9.1}
   m_Pivot: {x: 0.5, y: 0.5}
   m_Pivot: {x: 0.5, y: 0.5}
 --- !u!114 &1956041114
 --- !u!114 &1956041114