Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using AssetBundles;
  4. using System;
  5. using System.IO;
  6. using System.Net;
  7. using System.ComponentModel;
  8.  
  9.  
  10. public class DownLoadAssetBundles : MonoBehaviour
  11. {
  12. #if UNITY_IOS
  13.  
  14. private string uri;
  15. //private WWW www;
  16. private WebClient wct;
  17.  
  18. public UILabel progress;
  19. public UILabel progressAll;
  20.  
  21. public UISprite progressBar;
  22. public UISprite progressBarAll;
  23. public GameObject panel;
  24. public string sAssetBundleURL;
  25.  
  26. public GameObject m_goAssetBundleLoader;
  27. bool m_bDownloading = false;
  28. int m_iIndexFileDownloading = 0;
  29. bool m_bDownloadCurrentFileCompelted = false;
  30. string sDownloadingAt;
  31. bool m_bDownloadCompleted = false;
  32.  
  33. string spersistentDataPath = string.Empty;
  34. bool bUserHasJustClickEnter = false;
  35.  
  36. string[] m_arrsFileName;
  37.  
  38.  
  39. void Awake()
  40. {
  41. //PlayerPrefs.DeleteAll ();
  42. // Localization.language = "English";
  43. // LanguageAtlasList.Instance().isCN = ((int)Application.systemLanguage == 6);
  44. spersistentDataPath = Application.persistentDataPath;
  45. sAssetBundleURL = "http://dev.goplay.la/uploads/test/assetBundle/iOS/";
  46. sDownloadingAt = "DOWNLOADING_AT";
  47.  
  48. m_arrsFileName = new string[] {"iOS.assetbundle", "iOS.assetbundle.manifest", "_prfab.assetbundle", "_prfab.assetbundle.manifest", "bombeffect.assetbundle", "bombeffect.assetbundle.manifest",
  49. "effect_other.assetbundle","effect_other.assetbundle.manifest", "materials.assetbundle", "materials.assetbundle.manifest", "models_bulletprefab.assetbundle", "models_bulletprefab.assetbundle.manifest",
  50. "models_tank.assetbundle", "models_tank.assetbundle.manifest", "objects.assetbundle", "objects.assetbundle.manifest", "prfinscene.assetbundle", "prfinscene.assetbundle.manifest", "props.assetbundle",
  51. "props.assetbundle.manifest", "scene_killmode_endlessmode.assetbundle", "scene_killmode_endlessmode.assetbundle.manifest", "scene_killmode.assetbundle", "scene_killmode.assetbundle.manifest",
  52. "scene_level_endless(rdb).assetbundle", "scene_level_endless(rdb).assetbundle.manifest", "scene_levelmode_changjing.assetbundle", "scene_levelmode_changjing.assetbundle.manifest",
  53. "scene_levelmode_elitemode.assetbundle", "scene_levelmode_elitemode.assetbundle.manifest", "scene_levelmode.assetbundle", "scene_levelmode.assetbundle.manifest", "scene_materials.assetbundle",
  54. "scene_materials.assetbundle.manifest", "scene_pvpmode.assetbundle", "scene_pvpmode.assetbundle.manifest",
  55. "scene.assetbundle", "scene.assetbundle.manifest", "skilleffect.assetbundle", "skilleffect.assetbundle.manifest", "texture.assetbundle", "texture.assetbundle.manifest",
  56. "textures_materials.assetbundle", "textures_materials.assetbundle.manifest", "textures.assetbundle", "textures.assetbundle.manifest", "tx_fxmaker.assetbundle", "tx_fxmaker.assetbundle.manifest",
  57. "weiyan.assetbundle", "weiyan.assetbundle.manifest"};
  58.  
  59. panel.SetActive(false);
  60. }
  61.  
  62. // Use this for initialization
  63. void Start ()
  64. {
  65. LanguageAtlasList.Instance().isCN = GameLocalization.instance.language == "English" ? false : true;
  66.  
  67.  
  68. //check if all data isn't ready:
  69. CheckAlreadyDownload();
  70.  
  71. if (m_iIndexFileDownloading == m_arrsFileName.Length -1) {
  72. Debug.Log("---->>> Start Load...");
  73. m_goAssetBundleLoader.SetActive (true);
  74. StartCoroutine (AssetBundleLoader.Instance.InitializeLevelAsync ("scene.assetbundle", "logo"));
  75. }
  76. else
  77. {
  78. wct = new WebClient();
  79. wct.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
  80. wct.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadFileCompletedHandler);
  81.  
  82. string bundleBaseDownloadingURL = sAssetBundleURL + "iOS.assetbundle";
  83. string sSaveFilePath = spersistentDataPath + "/assetbundles/iOS/" + "iOS.assetbundle";
  84.  
  85. //wct.DownloadFileAsync(new Uri(bundleBaseDownloadingURL), sSaveFilePath);
  86.  
  87. // DownLoad(m_arrsFileName[m_iIndexFileDownloading]);
  88. panel.SetActive(true);
  89. }
  90. }
  91.  
  92.  
  93. void CheckAlreadyDownload()
  94. {
  95. m_iIndexFileDownloading = PlayerPrefs.GetInt (sDownloadingAt, 0);
  96. Debug.Log ("m_iIndexFileDownloading from PlayerPrefs : " + m_iIndexFileDownloading);
  97. }
  98.  
  99. // Update is called once per frame
  100. void Update ()
  101. {
  102. if (bUserHasJustClickEnter == true) {
  103.  
  104. if (!Directory.Exists(spersistentDataPath + "/assetbundles/iOS/"))
  105. {
  106. Directory.CreateDirectory(spersistentDataPath + "/assetbundles/iOS/");
  107. }
  108.  
  109. bUserHasJustClickEnter = false;
  110. UserClickedEnter ();
  111.  
  112. return;
  113. }
  114.  
  115. if (!m_bDownloading) {
  116. return;
  117. }
  118.  
  119. // progress.text = "[b]" + (www.progress * 100).ToString("f2") + "%";
  120. // progressBar.width = (int)(www.progress * 800);
  121. progressAll.text = "[b]" + (((float)m_iIndexFileDownloading / m_arrsFileName.Length ) * 100).ToString("f2") + "%";
  122. progressBarAll.width = (int)(((float)m_iIndexFileDownloading / m_arrsFileName.Length ) * 800);
  123.  
  124. if (m_bDownloadCurrentFileCompelted) {
  125.  
  126. //Set player prefs so after we can load and check
  127. PlayerPrefs.SetInt (sDownloadingAt, m_iIndexFileDownloading);
  128. PlayerPrefs.Save ();
  129.  
  130. m_bDownloadCurrentFileCompelted = false;
  131. if (m_iIndexFileDownloading == (m_arrsFileName.Length - 1)) {
  132.  
  133. Debug.Log ("Total download time : " + (Time.time - fBeginDownloadTime));
  134. StartCoroutine (AssetBundleLoader.Instance.InitializeLevelAsync ("scene.assetbundle", "logo"));
  135. } else {
  136.  
  137. m_iIndexFileDownloading++;
  138.  
  139. //DownLoad (m_arrsFileName [m_iIndexFileDownloading]);
  140. Debug.Log("Done 1 file ");
  141. if (m_iIndexFileDownloading == 2) {
  142. m_goAssetBundleLoader.SetActive (true);
  143. }
  144. }
  145. }
  146. }
  147.  
  148. public void OnClickEnter()
  149. {
  150. bUserHasJustClickEnter = true;
  151.  
  152. panel.SetActive(false);
  153. progress.gameObject.SetActive (true);
  154. progressBar.gameObject.SetActive (true);
  155.  
  156. progressAll.gameObject.SetActive (true);
  157. progressBarAll.gameObject.SetActive (true);
  158.  
  159.  
  160. }
  161.  
  162. void UserClickedEnter()
  163. {
  164. Debug.Log ("UserClickedEnter");
  165. m_bDownloading = true;
  166. fBeginDownloadTime = Time.time;
  167.  
  168. // Create the directory if it doesn't already exist
  169.  
  170.  
  171. DownLoad(m_arrsFileName[m_iIndexFileDownloading]);
  172. //StartCoroutine (AssetBundleLoader.Instance.InitializeLevelAsync ("scene.assetbundle", "logo"));
  173. }
  174.  
  175. public void OnClickExit()
  176. {
  177. Application.Quit();
  178. }
  179.  
  180.  
  181. float fBeginDownloadTime = 0;
  182. float fBeginDownloadTimeOneFile = 0;
  183. // public IEnumerator DownLoad(string a_sFileName)
  184. // {
  185. // fBeginDownloadTimeOneFile = Time.time;
  186. // www = null;
  187. //
  188. // string bundleBaseDownloadingURL = sAssetBundleURL + a_sFileName;
  189. // Debug.Log ("bundleBaseDownloadingURL : " + bundleBaseDownloadingURL);
  190. //
  191. // www = new WWW(bundleBaseDownloadingURL);
  192. // yield return www;
  193. //
  194. // SaveDownloadedAsset (www, a_sFileName);
  195. //
  196. // m_bDownloadCurrentFileCompelted = true;
  197. //
  198. // Debug.Log ("Load : " + bundleBaseDownloadingURL + " done, Time take : " + (Time.time - fBeginDownloadTimeOneFile));
  199. //
  200. //
  201. // }
  202.  
  203.  
  204. public void DownLoad(string a_sFileName)
  205. {
  206. //fBeginDownloadTimeOneFile = Time.time;
  207. //wct = null;
  208.  
  209. string bundleBaseDownloadingURL = sAssetBundleURL + a_sFileName;
  210. Debug.Log ("bundleBaseDownloadingURL : " + bundleBaseDownloadingURL);
  211.  
  212.  
  213.  
  214.  
  215. string sSaveFilePath = spersistentDataPath + "/assetbundles/iOS/" + a_sFileName;
  216.  
  217.  
  218. wct.DownloadFileAsync(new Uri(bundleBaseDownloadingURL), sSaveFilePath);
  219.  
  220. Debug.Log ("EOEOEOEOE");
  221. }
  222.  
  223. void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  224. {
  225. double bytesIn = double.Parse(e.BytesReceived.ToString());
  226. double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
  227. double percentage = bytesIn / totalBytes * 100;
  228.  
  229. progress.text = "[b]" + (percentage).ToString("f2") + "%";
  230. progressBar.width = (int)(percentage * 800);
  231. }
  232.  
  233. void DownloadFileCompletedHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  234. {
  235. if (e.Error == null) {
  236. m_bDownloadCurrentFileCompelted = true;
  237.  
  238. Debug.Log ("Load : " + " done, Time take : " + (Time.time - fBeginDownloadTimeOneFile));
  239. } else {
  240. Debug.LogError ("Download failed ");
  241. //DownLoad (m_arrsFileName [m_iIndexFileDownloading]);
  242. }
  243. }
  244.  
  245. public void SaveDownloadedAsset(WWW objSERVER, string a_sName)
  246. {
  247. // Create the directory if it doesn't already exist
  248. if (!Directory.Exists(Application.persistentDataPath + "/assetbundles/iOS/"))
  249. {
  250. Directory.CreateDirectory(Application.persistentDataPath + "/assetbundles/iOS/");
  251. }
  252.  
  253. string sPath = Application.persistentDataPath + "/assetbundles/iOS/" + a_sName;
  254.  
  255. // Initialize the byte string
  256. byte[] bytes = objSERVER.bytes;
  257.  
  258. // Creates a new file, writes the specified byte array to the file, and then closes the file.
  259. // If the target file already exists, it is overwritten.
  260. File.WriteAllBytes(sPath, bytes);
  261. }
  262.  
  263.  
  264. #endif //UNITY_IOS
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement