Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Created March 6, 2017 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuantumFractal/97053ab9d4ab70ef973696878436ff7f to your computer and use it in GitHub Desktop.
Save QuantumFractal/97053ab9d4ab70ef973696878436ff7f to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using AssetBundles;
public class LoadAssets : MonoBehaviour
{
public const string AssetBundlesOutputPath = "/AssetBundles/";
public string assetBundleName;
public string assetName;
public string assetRepositoryURL;
IEnumerator Start ()
{
yield return StartCoroutine(Initialize() );
// Below is the code to load a game object from the server
//yield return StartCoroutine(InstantiateGameObjectAsync (assetBundleName, assetName) );
}
protected IEnumerator Initialize()
{
DontDestroyOnLoad(gameObject);
// Set up the repository url
AssetBundleManager.SetSourceAssetBundleURL(assetRepositoryURL);
var request = AssetBundleManager.Initialize();
if (request != null)
yield return StartCoroutine(request);
}
protected IEnumerator InstantiateGameObjectAsync (string assetBundleName, string assetName)
{
float startTime = Time.realtimeSinceStartup;
// Load asset from assetBundle.
AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetAsync(assetBundleName, assetName, typeof(GameObject) );
if (request == null)
yield break;
yield return StartCoroutine(request);
GameObject prefab = request.GetAsset<GameObject> ();
if (prefab != null)
{
prefab.transform.localScale = prefab.transform.localScale * 50;
GameObject.Instantiate(prefab);
}
float elapsedTime = Time.realtimeSinceStartup - startTime;
Debug.Log(assetName + (prefab == null ? " was not" : " was")+ " loaded successfully in " + elapsedTime + " seconds" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment