Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.1 kB
2
Indexable
Never
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using Vuforia;

public class AssetBundleAugmenter : MonoBehaviour
{
    public string AssetName;
    private GameObject mBundleInstance = null;
    private ObserverBehaviour mObserverBehaviour;
    private AssetBundle bundle;
    private bool mAttached = false;
    void Start()
    {
        StartCoroutine(DownloadAndCache());
        mObserverBehaviour = GetComponent<ObserverBehaviour>();
        if (mObserverBehaviour)
        {
            mObserverBehaviour.gameObject.AddComponent<DefaultObserverEventHandler>();
        }
    }

    // Update is called once per frame
    IEnumerator DownloadAndCache()
    {
        while (!Caching.ready)
            yield return null;

        // example URL for retrieving bundle from a server
        string bundleURL = "https://drive.google.com/uc?export=download&id=1Ds_4DmQ-CBiAFmTYW2FAVdoh59aIcFVj";
        using (UnityWebRequest mAssetBundle = UnityWebRequestAssetBundle.GetAssetBundle(bundleURL))
        {
            yield return mAssetBundle;
            if (mAssetBundle.error != null)
                throw new UnityException("mAssetBundle download had an error: " + mAssetBundle.error);
            bundle = DownloadHandlerAssetBundle.GetContent(mAssetBundle);
            if (AssetName == "")
            {
                LoadAssetToTarget();
            }
        }
    }

    private void LoadAssetToTarget()
    {
        mBundleInstance = Instantiate(bundle.LoadAsset(AssetName)) as GameObject;
        if (!mAttached && mBundleInstance)
        {
            // If bundle has been loaded, let's attach it to this target
            mBundleInstance.transform.parent = this.transform;
            mBundleInstance.transform.localScale = new Vector3(1f, 1f, 1f);
            mBundleInstance.transform.localPosition = new Vector3(0f,0f,0f);
            mBundleInstance.transform.gameObject.SetActive(true);
            mAttached = true;

        }
    }
}