Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using AssetBundles;
- using System;
- using System.IO;
- using System.Net;
- using System.ComponentModel;
- public class DownLoadAssetBundles : MonoBehaviour
- {
- #if UNITY_IOS
- private string uri;
- //private WWW www;
- private WebClient wct;
- public UILabel progress;
- public UILabel progressAll;
- public UISprite progressBar;
- public UISprite progressBarAll;
- public GameObject panel;
- public string sAssetBundleURL;
- public GameObject m_goAssetBundleLoader;
- bool m_bDownloading = false;
- int m_iIndexFileDownloading = 0;
- bool m_bDownloadCurrentFileCompelted = false;
- string sDownloadingAt;
- bool m_bDownloadCompleted = false;
- string spersistentDataPath = string.Empty;
- bool bUserHasJustClickEnter = false;
- string[] m_arrsFileName;
- void Awake()
- {
- //PlayerPrefs.DeleteAll ();
- // Localization.language = "English";
- // LanguageAtlasList.Instance().isCN = ((int)Application.systemLanguage == 6);
- spersistentDataPath = Application.persistentDataPath;
- sAssetBundleURL = "http://dev.goplay.la/uploads/test/assetBundle/iOS/";
- sDownloadingAt = "DOWNLOADING_AT";
- m_arrsFileName = new string[] {"iOS.assetbundle", "iOS.assetbundle.manifest", "_prfab.assetbundle", "_prfab.assetbundle.manifest", "bombeffect.assetbundle", "bombeffect.assetbundle.manifest",
- "effect_other.assetbundle","effect_other.assetbundle.manifest", "materials.assetbundle", "materials.assetbundle.manifest", "models_bulletprefab.assetbundle", "models_bulletprefab.assetbundle.manifest",
- "models_tank.assetbundle", "models_tank.assetbundle.manifest", "objects.assetbundle", "objects.assetbundle.manifest", "prfinscene.assetbundle", "prfinscene.assetbundle.manifest", "props.assetbundle",
- "props.assetbundle.manifest", "scene_killmode_endlessmode.assetbundle", "scene_killmode_endlessmode.assetbundle.manifest", "scene_killmode.assetbundle", "scene_killmode.assetbundle.manifest",
- "scene_level_endless(rdb).assetbundle", "scene_level_endless(rdb).assetbundle.manifest", "scene_levelmode_changjing.assetbundle", "scene_levelmode_changjing.assetbundle.manifest",
- "scene_levelmode_elitemode.assetbundle", "scene_levelmode_elitemode.assetbundle.manifest", "scene_levelmode.assetbundle", "scene_levelmode.assetbundle.manifest", "scene_materials.assetbundle",
- "scene_materials.assetbundle.manifest", "scene_pvpmode.assetbundle", "scene_pvpmode.assetbundle.manifest",
- "scene.assetbundle", "scene.assetbundle.manifest", "skilleffect.assetbundle", "skilleffect.assetbundle.manifest", "texture.assetbundle", "texture.assetbundle.manifest",
- "textures_materials.assetbundle", "textures_materials.assetbundle.manifest", "textures.assetbundle", "textures.assetbundle.manifest", "tx_fxmaker.assetbundle", "tx_fxmaker.assetbundle.manifest",
- "weiyan.assetbundle", "weiyan.assetbundle.manifest"};
- panel.SetActive(false);
- }
- // Use this for initialization
- void Start ()
- {
- LanguageAtlasList.Instance().isCN = GameLocalization.instance.language == "English" ? false : true;
- //check if all data isn't ready:
- CheckAlreadyDownload();
- if (m_iIndexFileDownloading == m_arrsFileName.Length -1) {
- Debug.Log("---->>> Start Load...");
- m_goAssetBundleLoader.SetActive (true);
- StartCoroutine (AssetBundleLoader.Instance.InitializeLevelAsync ("scene.assetbundle", "logo"));
- }
- else
- {
- wct = new WebClient();
- wct.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
- wct.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadFileCompletedHandler);
- string bundleBaseDownloadingURL = sAssetBundleURL + "iOS.assetbundle";
- string sSaveFilePath = spersistentDataPath + "/assetbundles/iOS/" + "iOS.assetbundle";
- //wct.DownloadFileAsync(new Uri(bundleBaseDownloadingURL), sSaveFilePath);
- // DownLoad(m_arrsFileName[m_iIndexFileDownloading]);
- panel.SetActive(true);
- }
- }
- void CheckAlreadyDownload()
- {
- m_iIndexFileDownloading = PlayerPrefs.GetInt (sDownloadingAt, 0);
- Debug.Log ("m_iIndexFileDownloading from PlayerPrefs : " + m_iIndexFileDownloading);
- }
- // Update is called once per frame
- void Update ()
- {
- if (bUserHasJustClickEnter == true) {
- if (!Directory.Exists(spersistentDataPath + "/assetbundles/iOS/"))
- {
- Directory.CreateDirectory(spersistentDataPath + "/assetbundles/iOS/");
- }
- bUserHasJustClickEnter = false;
- UserClickedEnter ();
- return;
- }
- if (!m_bDownloading) {
- return;
- }
- // progress.text = "[b]" + (www.progress * 100).ToString("f2") + "%";
- // progressBar.width = (int)(www.progress * 800);
- progressAll.text = "[b]" + (((float)m_iIndexFileDownloading / m_arrsFileName.Length ) * 100).ToString("f2") + "%";
- progressBarAll.width = (int)(((float)m_iIndexFileDownloading / m_arrsFileName.Length ) * 800);
- if (m_bDownloadCurrentFileCompelted) {
- //Set player prefs so after we can load and check
- PlayerPrefs.SetInt (sDownloadingAt, m_iIndexFileDownloading);
- PlayerPrefs.Save ();
- m_bDownloadCurrentFileCompelted = false;
- if (m_iIndexFileDownloading == (m_arrsFileName.Length - 1)) {
- Debug.Log ("Total download time : " + (Time.time - fBeginDownloadTime));
- StartCoroutine (AssetBundleLoader.Instance.InitializeLevelAsync ("scene.assetbundle", "logo"));
- } else {
- m_iIndexFileDownloading++;
- //DownLoad (m_arrsFileName [m_iIndexFileDownloading]);
- Debug.Log("Done 1 file ");
- if (m_iIndexFileDownloading == 2) {
- m_goAssetBundleLoader.SetActive (true);
- }
- }
- }
- }
- public void OnClickEnter()
- {
- bUserHasJustClickEnter = true;
- panel.SetActive(false);
- progress.gameObject.SetActive (true);
- progressBar.gameObject.SetActive (true);
- progressAll.gameObject.SetActive (true);
- progressBarAll.gameObject.SetActive (true);
- }
- void UserClickedEnter()
- {
- Debug.Log ("UserClickedEnter");
- m_bDownloading = true;
- fBeginDownloadTime = Time.time;
- // Create the directory if it doesn't already exist
- DownLoad(m_arrsFileName[m_iIndexFileDownloading]);
- //StartCoroutine (AssetBundleLoader.Instance.InitializeLevelAsync ("scene.assetbundle", "logo"));
- }
- public void OnClickExit()
- {
- Application.Quit();
- }
- float fBeginDownloadTime = 0;
- float fBeginDownloadTimeOneFile = 0;
- // public IEnumerator DownLoad(string a_sFileName)
- // {
- // fBeginDownloadTimeOneFile = Time.time;
- // www = null;
- //
- // string bundleBaseDownloadingURL = sAssetBundleURL + a_sFileName;
- // Debug.Log ("bundleBaseDownloadingURL : " + bundleBaseDownloadingURL);
- //
- // www = new WWW(bundleBaseDownloadingURL);
- // yield return www;
- //
- // SaveDownloadedAsset (www, a_sFileName);
- //
- // m_bDownloadCurrentFileCompelted = true;
- //
- // Debug.Log ("Load : " + bundleBaseDownloadingURL + " done, Time take : " + (Time.time - fBeginDownloadTimeOneFile));
- //
- //
- // }
- public void DownLoad(string a_sFileName)
- {
- //fBeginDownloadTimeOneFile = Time.time;
- //wct = null;
- string bundleBaseDownloadingURL = sAssetBundleURL + a_sFileName;
- Debug.Log ("bundleBaseDownloadingURL : " + bundleBaseDownloadingURL);
- string sSaveFilePath = spersistentDataPath + "/assetbundles/iOS/" + a_sFileName;
- wct.DownloadFileAsync(new Uri(bundleBaseDownloadingURL), sSaveFilePath);
- Debug.Log ("EOEOEOEOE");
- }
- void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
- {
- double bytesIn = double.Parse(e.BytesReceived.ToString());
- double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
- double percentage = bytesIn / totalBytes * 100;
- progress.text = "[b]" + (percentage).ToString("f2") + "%";
- progressBar.width = (int)(percentage * 800);
- }
- void DownloadFileCompletedHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
- {
- if (e.Error == null) {
- m_bDownloadCurrentFileCompelted = true;
- Debug.Log ("Load : " + " done, Time take : " + (Time.time - fBeginDownloadTimeOneFile));
- } else {
- Debug.LogError ("Download failed ");
- //DownLoad (m_arrsFileName [m_iIndexFileDownloading]);
- }
- }
- public void SaveDownloadedAsset(WWW objSERVER, string a_sName)
- {
- // Create the directory if it doesn't already exist
- if (!Directory.Exists(Application.persistentDataPath + "/assetbundles/iOS/"))
- {
- Directory.CreateDirectory(Application.persistentDataPath + "/assetbundles/iOS/");
- }
- string sPath = Application.persistentDataPath + "/assetbundles/iOS/" + a_sName;
- // Initialize the byte string
- byte[] bytes = objSERVER.bytes;
- // Creates a new file, writes the specified byte array to the file, and then closes the file.
- // If the target file already exists, it is overwritten.
- File.WriteAllBytes(sPath, bytes);
- }
- #endif //UNITY_IOS
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement