Skip to content

Instantly share code, notes, and snippets.

@neogeek
Last active August 19, 2018 19:09
Show Gist options
  • Save neogeek/b7d0599390c15828605c418ff8ad1643 to your computer and use it in GitHub Desktop.
Save neogeek/b7d0599390c15828605c418ff8ad1643 to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
public class LoadSceneFromAssetBundle : MonoBehaviour
{
[SerializeField]
private string assetBundleUrl;
[SerializeField]
private string scenePath;
private Scene sceneRef;
public void HandleButtonClick()
{
StartCoroutine(LoadAssetBundle());
}
private IEnumerator LoadAssetBundle()
{
if (sceneRef.IsValid())
{
yield return SceneManager.UnloadSceneAsync(sceneRef);
}
using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(assetBundleUrl))
{
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
if (bundle.isStreamedSceneAssetBundle)
{
yield return SceneManager.LoadSceneAsync(scenePath, LoadSceneMode.Additive);
sceneRef = SceneManager.GetSceneByPath(scenePath);
SceneManager.SetActiveScene(sceneRef);
}
bundle.Unload(false);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment