GameZip.cs 3.2 KB

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