IEnumerator _Download (BundleDownloadHandle handle) { if (cachePath == null) cachePath = System.IO.Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), "bundlecache"); var uri = handle.uri; var dir = Path.Combine (Path.Combine (cachePath, uri.Host), Path.GetDirectoryName (uri.AbsolutePath).Substring (1)); var file = Path.GetFileName (uri.AbsolutePath); var path = Path.Combine (dir, file); if (File.Exists (path)) { var www = new WWW ("file://" + path); yield return www; if (HandleDownload (handle, www)) { handle.state = BundleDownloadState.Success; handle.fromCache = true; } } if (handle.state != BundleDownloadState.Success) { var www = new WWW (uri.ToString ()); yield return www; if (HandleDownload (handle, www)) { Directory.CreateDirectory (dir); File.WriteAllBytes (path, www.bytes); handle.error = null; handle.state = BundleDownloadState.Success; } else { handle.state = BundleDownloadState.Failure; } } }
bool HandleDownload (BundleDownloadHandle handle, WWW www) { if (www.error == null) { handle.asset = www.assetBundle; if (www.error != null) { handle.error = www.error; return false; } else { if (handle.asset == null) { handle.error = "Failed to load asset bundle"; return false; } else { return true; } } } else { handle.error = www.error; return false; } }
public BundleDownloadHandle Download (string url) { var handle = new BundleDownloadHandle (url); StartCoroutine (_Download (handle)); return handle; }