malith priyashan
malith priyashan

Reputation: 21

cant load asset from firebase storage to unity

I am creating an augmented reality app using vuforia and unity. i have uploaded my assetbundle to firebase storage and i want to retrieve a 3d model saved inside it and load it as a child on my image target game object but it i cant retrieve it.i also installed firebase unity sdk.

using Firebase;
using Firebase.Storage;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System;
using UnityEngine.Networking;
using UnityEngine.UI;

public class loadmodel2 : MonoBehaviour
{
    public GameObject test;

    void Start()
    {
        FirebaseStorage storage = FirebaseStorage.DefaultInstance;

        Firebase.Storage.StorageReference reference =storage.GetReferenceFromUrl("gs://fit-union-221609.appspot.com/assettest1/myasset");

        reference.GetDownloadUrlAsync().ContinueWith((Task<Uri> task) => {
            if (!task.IsFaulted && !task.IsCanceled)
            {
                Debug.Log("Download URL: " + task.Result);
                // ... now download the file via WWW or UnityWebRequest.
                StartCoroutine(Loadcoroutine());
            }
        });
    }

    IEnumerator Loadcoroutine()
    {
        string url = "gs://fit-union-221609.appspot.com/assettest1/myasset";
        WWW www = new WWW(url);
        while (!www.isDone)
            yield return null;
        AssetBundle myasset = www.assetBundle;

        GameObject mya1 = myasset.LoadAsset("Barbarian Variant") as GameObject;

        Instantiate(mya1).transform.parent = test.transform;
    }
}

this is what i want

i created a new asset bundle with the same name "myasset" and uploaded it to firebase storage ialso changed the name of the 3d model to "BarbarianVariant" removing the space

enter image description here

enter image description here

enter image description here

in here i have enabled loadmodel2 script in the main camera and for the test game object i assigned maincamera.you can also see the output i get in the console

Upvotes: 0

Views: 1185

Answers (1)

derHugo
derHugo

Reputation: 90704

First of all your component loadmodel2 is disabled in the Inspector so its Start will never be called ...


I'm no Firebase expert but you first use GetDownloadUrlAsync and then anyway never use the result but start a new UnityWebRequest for downloading it from the same reference URL.

Don't you rather want to use the retrieved download URL from task.Result like e.g.

StartCoroutine(Loadcoroutine(task.Result));

...

IEnumerator Loadcoroutine(Uri uri)
{
    ...
}

Then note that WWW(obsolete) != UnityWebRequest!

What you want to do is probably rather using UnityWebRequestAssetBundle.GetAssetBundle (or in former versions UnityWebRequest.GetAssetBundle)

IEnumerator Loadcoroutine(Uri uri)
{
    using(var www = UnityWebRequestAssetBundle.GetAssetBundle(uri))
    {
        yield return www.SendWebRequest();

        if(www.isNetworkError || www.isHttpError) 
        {
            Debug.Log(www.error);
        }
        else 
        {
            AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);

            // Make sure this path is correct! Afaik it should at least start with "Assets/..."
            var mya1 =  bundle.LoadAssetAsync<GameObject>("Barbarian Variant");
            yield return mya1;

            var obj = Instantiate((GameObject)mya1.asset);
            obj.transform.parent = test.transform;
        }
    }
}

In general I would also avoid spaces in GameObject names.

Upvotes: 1

Related Questions