/// <summary> /// Load random tip from web /// There is a specific format, but you can write your own method for this /// </summary> /// <returns></returns> IEnumerator LoadTipFromWeb() { /// json request body (get one random tip between 0 and 15 from web string jsonBody = "[{\"method\":\"tasks.search\",\"params\":[\"\",\"createdAt\",\"desc\"," + Random.Range(0, 15) + ",1]}]"; var www = new UnityWebRequest(url, "POST"); byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonBody); www.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw); www.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); www.SetRequestHeader("Content-Type", "application/json"); www.timeout = 3; yield return(www.SendWebRequest()); if (www.isHttpError || www.isNetworkError) { Debug.Log("Error"); } else { // Prepaire json for JsonUtility string result = www.downloadHandler.text.Replace("\"results\":[[", "\"results\":{\"tips\":[").Replace("]]}", "]}}"); /// create UrlResponse object from json response UrlResponse response = JsonUtility.FromJson <UrlResponse>(result); // if tip is successful downloaded if (response.success && !response.results.IsEmpty()) { Tip tip = response.results.GetRandom(); // invoke event OnTipLoaded?.Invoke(tip); yield break; } } // if tip is not loaded from web get one from resource LoadTipFromResources(); }
/// <summary> /// Show tip on the panel /// </summary> /// <param name="tip"></param> public void ShowTip(Tip tip) { tipTitle.text = tip.title; tipDescription.text = tip.description; }
/// <summary> /// When tip was loaded, show it and load scene /// </summary> /// <param name="t"></param> private void TipLoaded(Tip t) { _loadingScreen.ShowTip(t); StartCoroutine(LoadSceneCoroutine()); }