Question by
Ediir · Nov 07, 2017 at 07:01 PM ·
cachebundlebundlesloadfromcacheordownload
LoadFromCacheOrDownload.
So, i ran into a problem. I have tried and failed a couple of times with LoadFromCacheOrDownload.
I use unityweb request, to download the files, and then another script to load the bundle scene async into the exicting scene. This works perfectly, but it downloads the bundle everytime i run the build.
So i want LoadFromCacheOrDownload in a third script. I want this script to run first, then launch the IEnumerator function that either loads existing bundle, or download the new version. Hopefully someone can help me with this. Posting the scripts i use underneath.
Thanks in advance.
Downloadscript
public class DownloadBundleFromCloud : MonoBehaviour {
public LoadSceneFromBundle LoadSceneFromBundleScript;
[Header("Model update info")]
public string DownloadURL;
[Header("Progress bar")]
public Slider Loading;
public GameObject Canvas;
public Text ProgressText;
public Text WhatsGoingOnText;
// Private
private AssetBundleCreateRequest bundleRequest;
private UnityWebRequest request;
void Start()
{
StartCoroutine(LoadAsync());
}
IEnumerator LoadAsync()
{
var request = UnityWebRequest.GetAssetBundle(DownloadURL);
request.SendWebRequest();
Canvas.SetActive(true);
while (!request.isDone && !request.isHttpError)
{
float thisDownloadProgress = Mathf.Clamp01(request.downloadProgress / .9f);
Loading.value = thisDownloadProgress;
thisDownloadProgress = Mathf.Round(thisDownloadProgress * 100f);
ProgressText.text = thisDownloadProgress + "%";
WhatsGoingOnText.text = "Downloading..";
yield return null;
}
if (request.isHttpError)
{
Debug.Log("HTTP Error");
WhatsGoingOnText.text = "HTTP Error";
}
if (request.isDone)
{
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
StartCoroutine(LoadSceneFromBundleScript.LoadSceneAsync());
}
}
}
Load Scene Async
public class LoadSceneFromBundle : MonoBehaviour {
public DownloadBundleFromCloud DownloadBundleFromCloudScript;
[Header("Scene to load")]
public string SceneName;
[Space]
[HideInInspector]
public bool SceneLoaded = false;
// Private
private Slider Loading;
private GameObject Canvas;
private Text ProgressText;
private Text WhatsGoingOnText;
void Start()
{
Loading = DownloadBundleFromCloudScript.Loading;
Canvas = DownloadBundleFromCloudScript.Canvas;
ProgressText = DownloadBundleFromCloudScript.ProgressText;
WhatsGoingOnText = DownloadBundleFromCloudScript.WhatsGoingOnText;
}
public IEnumerator LoadSceneAsync()
{
AsyncOperation async = SceneManager.LoadSceneAsync(SceneName, LoadSceneMode.Additive);
while (!async.isDone)
{
float progress = Mathf.Clamp01(async.progress / .9f);
Loading.value = progress;
progress = Mathf.Round(progress * 100f);
ProgressText.text = progress + "%";
WhatsGoingOnText.text = "Loading Scene..";
yield return null;
}
if (async.isDone)
{
Canvas.SetActive(false);
SceneLoaded = true;
}
}
}
Comment