GameZip.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using ThirdParty.DownloadSystem;
  5. using UnityEngine;
  6. public class GameZip
  7. {
  8. private UnZipThreadPool _unZipThreadPool;
  9. private string md5Value;
  10. public GameStart GameStart;
  11. private System.Action callBack;
  12. private string streamingPath;
  13. private GameStartUIPanel _gameStartUIPanel;
  14. public void Start(GameStartUIPanel gameStartUIPanel,GameStart gameStart, System.Action callBack)
  15. {
  16. this.GameStart = gameStart;
  17. this.callBack = callBack;
  18. _gameStartUIPanel = gameStartUIPanel;
  19. streamingPath = Application.streamingAssetsPath;
  20. #if UNITY_IPHONE
  21. streamingPath = "file://"+streamingPath;
  22. #endif
  23. LoadMd5();
  24. }
  25. private void LoadMd5()
  26. {
  27. string md5 = UnityEngine.PlayerPrefs.GetString("assetMD5", "");
  28. Debug.Log(" Application.version" + Application.version);
  29. if (!md5.Equals(Application.version)) //需要解压文件
  30. {
  31. Debug.Log(Application.persistentDataPath);
  32. GameStart.StartCoroutine(DowFile());
  33. }
  34. else
  35. {
  36. Finish();
  37. }
  38. }
  39. private IEnumerator DowFile()
  40. {
  41. _gameStartUIPanel.SetMassge("转移文件");
  42. string url = streamingPath + "/assetBundle.zip";
  43. string dowFinle = Application.persistentDataPath + "/assetBundle.zip";
  44. AddLoad("转移解压文件");
  45. DownloadFileData downloadFileData = new DownloadFileData();
  46. downloadFileData.remoteUrl = url;
  47. downloadFileData.localPath = dowFinle;
  48. DownloadHander downloadHander = FileDownloadSystem.Instance.DownloadFile(downloadFileData);
  49. while (!downloadHander.isFinish)
  50. {
  51. yield return new WaitForSeconds(1);
  52. }
  53. if (!File.Exists(dowFinle))
  54. {
  55. Finish();
  56. yield break;
  57. }
  58. string unzipPath = Application.persistentDataPath + "/Bundle";
  59. _unZipThreadPool = new UnZipThreadPool();
  60. AddLoad("开始解压" + unzipPath);
  61. _gameStartUIPanel.SetMassge("开始解压,解压过程中不消耗流量");
  62. _unZipThreadPool.InitUnZip(dowFinle, unzipPath);
  63. SliderData sliderData = new SliderData();
  64. sliderData.maxValue = _unZipThreadPool.maxSize;
  65. sliderData.CcurrValue = delegate { return sliderData.maxValue - _unZipThreadPool.CurrCount(); };
  66. _gameStartUIPanel.SetSlider(sliderData);
  67. while (!_unZipThreadPool.isEnd)
  68. {
  69. yield return new WaitForSeconds(1);
  70. }
  71. _gameStartUIPanel.ClearSlider();
  72. _gameStartUIPanel.SetMassge("解压完成");
  73. AddLoad("解压完成");
  74. _unZipThreadPool.Cloas();
  75. UnityEngine.PlayerPrefs.SetString("assetMD5", Application.version);
  76. Finish();
  77. }
  78. private void Finish()
  79. {
  80. // LoadDll.StartDllLoad();
  81. callBack?.Invoke();
  82. }
  83. private void Update()
  84. {
  85. FileDownloadSystem.Instance.Update();
  86. }
  87. public void AddLoad(string message)
  88. {
  89. }
  90. }