如何从Firebase数据库下载图像并将其统一加载到图像中 [英] How to download image from firebase database, and load it into image in unity
问题描述
我使用Firebase Realtime数据库处理Unity移动应用程序中的某些UI.我可以从数据库中保存和检索数据,没有任何问题. 当我想从Firebase Storage加载图像时会出现问题. 我已将URL和图像名称保存在服务器上,现在尝试实例化预制件. 该预制件包含一个面板,其中包含每个Firebase子级的文本和图像. 我尝试从数据库加载的图像结构如下:
I use Firebase Realtime database to handle some of the UI in my Unity mobile application. I save and retrieve data from my database without any problems. The problem comes when i want to load an image from Firebase Storage. I have saved the URL's and names of the images' on my server, and now i try to Instantiate a prefab. This prefab contains a panel with text and an image for each Firebase-child. The structure of the image i try to load from my database looks like this:
因此,在我的代码中,我转到了子代"Byer",找到了名字并添加了密钥,因为新实例化的预制件的名称可以正常工作.但是,当我尝试将imageURL加载到图像中时,却遇到了麻烦-我的代码如下所示:
So in my code I go to the child "Byer", find the first name and add the key as the name of my newly instantiated prefab works fine. But when I try to load the imageURL into the image, I get the trouble - my code looks like this:
DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
FirebaseDatabase.DefaultInstance.GetReference("Byer").ChildAdded += Handle_ChildAdded;
void Handle_ChildAdded(object sender, ChildChangedEventArgs e)
{
if (e.Snapshot.Value != null)
{
var dict = e.Snapshot.Value as Dictionary<string, object>;
Transform scrollViewObj = Instantiate(prefab, new Vector3(0, (downSize * Global.citiesCount) - firstY, 0), Quaternion.identity);
scrollViewObj.transform.Find("Text").gameObject.GetComponent<Text>().text = e.Snapshot.Key;
scrollViewObj.name = e.Snapshot.Key;
//HERE I TRY TO LOAD IMAGE FROM URL (This is my problem)
string data_URL = dict["ImageURL"] as string;
//Start coroutine to download image
StartCoroutine(AccessURL(data_URL, scrollViewObj.transform.Find("Image").gameObject.GetComponent<Image>()));
}
}
//Function to download (This could might also be my problem)
IEnumerator AccessURL(string url, Image img)
{
//Debug.Log("Accessing texture URL in database");
using (WWW www = new WWW(url))
{
yield return www;
Renderer r = GetComponent<Renderer>();
r.material.mainTexture = www.texture;
img.material.mainTexture = www.texture;
Debug.Log("Texture URL: " + www.url);
}
}
有人可以看到我在做什么错吗?
Can anyone see what I am doing wrong?
推荐答案
- 确保您导入了
FirebaseStorage.unitypackage
-
您将需要
StorageReference
而不是DatabaseReference
- Make sure you imported
FirebaseStorage.unitypackage
You will need
StorageReference
notDatabaseReference
Firebase.Storage.StorageReference storageReference = Firebase.Storage.FirebaseStorage.DefaultInstance.GetReferenceFromUrl("storage_url"); storageReference.Child("resource_name").GetBytesAsync(1024*1024). ContinueWith((System.Threading.Tasks.Task<byte[]> task) => { if (task.IsFaulted || task.IsCanceled) { Debug.Log(task.Exception.ToString()); } else { byte[] fileContents = task.Result; Texture2D texture = new Texture2D(1, 1); texture.LoadImage(fileContents); //if you need sprite for SpriteRenderer or Image Sprite sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f,texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f); Debug.Log("Finished downloading!"); } });
-
storage_url
:在firebase-> storage部分中找到.将类似于gs://project_name.appspot.com/
storage_url
: Found in firebase->storage section. will be something likegs://project_name.appspot.com/
这篇关于如何从Firebase数据库下载图像并将其统一加载到图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Make sure you imported