Hello.
Why coroutine not started?
I use Firebase Realtime database to handle some data in my Unity mobile application It’s work fine when i get url and press ‘S’.
But I want start coroutine from code. Can anyone see what I am doing wrong? Thanks.
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using UnityEngine;
using UnityEngine.UI;
using SimpleJSON;
using UnityEngine.Networking;
using System.Collections;
using Firebase.Storage;
using System.Threading.Tasks;
using System;
public class Connector : MonoBehaviour
{
[SerializeField] string _db;
[SerializeField] int _cuurentIndex; //current page index
[SerializeField] RawImage _rawTexture;
FirebaseStorage _storage;
[SerializeField] string _url;
void Start()
{
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(_db);
_storage = FirebaseStorage.DefaultInstance;
FirebaseDatabase.DefaultInstance
.GetReference(_cuurentIndex.ToString())
.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
print(task.Exception.Message);
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
print(snapshot.GetRawJsonValue());
ParseJson(snapshot.GetRawJsonValue());
}
});
}
// THIS WORKING WHEN I GET URL AND PRESS 'S'
private void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
StartCoroutine(GetTexture(_url));
}
}
private void ParseJson(string jsonString)
{
var N = JSON.Parse(jsonString);
print(N["url"].Value);
StorageReference gs_reference =
_storage.GetReferenceFromUrl(N["url"].Value);
gs_reference.GetDownloadUrlAsync().ContinueWith((Task<Uri> task) =>
{
if (!task.IsFaulted && !task.IsCanceled)
{
Debug.Log("Download URL: " + task.Result.OriginalString);
_url = task.Result.OriginalString; // for test
StartGetTexture(task.Result.OriginalString);
}
});
}
private void StartGetTexture(string url)
{
StartCoroutine(GetTexture(url));
}
IEnumerator GetTexture(string textureUrl)
{
UnityWebRequest www = UnityWebRequestTexture.GetTexture(textureUrl);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
Debug.Log(www.error);
else
_rawTexture.texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
}
}