Created
March 6, 2017 01:44
-
-
Save QuantumFractal/97053ab9d4ab70ef973696878436ff7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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