Last active
August 19, 2018 19:09
-
-
Save neogeek/b7d0599390c15828605c418ff8ad1643 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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