0

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

Community
  • 1
  • 1

1 Answers1

1

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.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • tnx for the reply but i get an error saying i cant convert task.result from system.uri to string – malith priyashan Nov 18 '19 at 17:17
  • Yeah my bad .. I notes later that it is a `Task` updated the answer. – derHugo Nov 18 '19 at 17:19
  • i get an error in transform saying object does not contain a defenition for transform and no accesible extension method transform accepting a first argument of type object could be found but even if that line of code is removed it should download my model from firebase right but its not getting downloaded – malith priyashan Nov 18 '19 at 18:00
  • Sorry I'm typing only on the phone ;) You have to cast the returned `Object` to `GameObject`. Either like `var obj = Instantiate((GameObject)mya1.asset);` or `var obj = (GameObject) Instantiate (mya1.asset);` should be the same afaik. How exactly do you track the download? Do you get any errors? Did you try debugging the code step by step? – derHugo Nov 18 '19 at 19:43
  • well this is the output i get on the console-Download URL: https://firebasestorage.googleapis.com/v0/b/fit-union-221609.appspot.com/o/assettest1%2Fmyasset?alt=media&token=2e486742-add0-40e9-ada0-83e6e5cc7195 UnityEngine.Debug:Log(Object) Loadmodel2:b__1_0(Task`1) (at Assets/Loadmodel2.cs:25) System.Threading._ThreadPoolWaitCallback:PerformWaitCallback() – malith priyashan Nov 20 '19 at 03:18
  • i get the download url in the debug but nothing gets instantiated or downloaded – malith priyashan Nov 25 '19 at 13:07
  • can someone please help me on this this is my final year project – malith priyashan Nov 25 '19 at 13:08
  • i debugged each line of code but couldnt find any errors – malith priyashan Nov 25 '19 at 14:05
  • Can you make sure your path for `LoadAssetAsync` is correct? Usually afaik it should at least begin with `"Assets/..."` – derHugo Nov 25 '19 at 16:32
  • so bro i posted more information if that helps u ,and should my path for the load asset async be "myasset/BarbarianVariant" i tried it but that didnt work.i also used tried using your code to download from a local file system(path in my pc)and it worked fine. – malith priyashan Nov 26 '19 at 02:03
  • The path has to match your prefab names and folder structure within the Asset bundle -> the one it had in Unity when building the Asset bundle. – derHugo Nov 26 '19 at 05:43