Afternoon folks,
I have recently decided I needed to learn how to use Assetbundles and have been getting my share of bumps and bruises. However, it has been fun and there is a ton of documentation to help out someone who has no idea how they work. I have watched hours of tutorials and read the recent documentation for Webrequest and its benefits. The problem I am having is that I want to load the file from the location I saved the asset bundle to. Also, I chose the request and save method used because of the size of our bundles. I want to be able to download it, and load the scene on Click of a button(not auto). I also want to be able to delete or uninstall the assetbundle.
Maybe if I can get help figuring out how this whole thing works then I could figure out the rest.
Anyhow, thanks for the help in advance. The below script is my attempt at putting the things together I have learned over the past 5 days. Current issue is that I get an error
private AssetBundleCreateRequest bundleRequest;
private UnityWebRequest request;
private DownloadHandler downloadHandler;
public string sceneURL;
public string sceneName;
private bool sceneDownloaded = false;
private bool sceneDownloading = false;
public Slider downloadBar;
public static string dlcPath;
public void PlatformSaveLocationCheck()
{
print("Detected Device Type" + Application.platform);
if (Application.platform == RuntimePlatform.Android)
{
dlcPath = Application.persistentDataPath + "/DLC/";
if (!Directory.Exists(dlcPath))
{
Debug.Log("Directory Created at" + dlcPath);
Directory.CreateDirectory(dlcPath);
}
}
else
{
dlcPath = Application.dataPath + "/DLC/";
if (!Directory.Exists(dlcPath))
{
Debug.Log("Directory Created at" + dlcPath);
Directory.CreateDirectory(dlcPath);
}
}
}
IEnumerator Start()
{
PlatformSaveLocationCheck();
string path = Path.Combine(dlcPath, sceneName + ".unity3d");
if (File.Exists(path))
{
StartCoroutine(LoadBundleScene());
Debug.Log("Already Downloaded");
}
else
{
//get the bundle
var request = new UnityWebRequest(sceneURL, UnityWebRequest.kHttpVerbGET);
request.downloadHandler = new DownloadHandlerFile(path);
sceneDownloading = true;
//set actual wait time for server communication
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
Debug.LogError(request.error);
else
Debug.Log("File successfully downloaded and saved to " + path);
}
}
IEnumerator LoadBundleScene()
{
bundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(dlcPath, sceneName));
Debug.Log("Asset Already Downloaded. Loading Scene");
if (bundleRequest == null)
{
Debug.Log("Failed to load AssetBundle!");
}
else
{
yield return null;
if (bundleRequest.isDone)
{
Debug.Log("Found Asset Bundle");
LoadScene();
}
}
}
public void LoadScene()
{
if (sceneDownloaded)
{
SceneManager.LoadScene(sceneName);
}
else
{
return;
}
}
private void Update()
{
/*if (sceneDownloading)
{
if (request.downloadProgress > 0 && request.downloadProgress < 0.99f)
{
downloadBar.value = request.downloadProgress;
}
}*/
}