PackageImportWindow.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEditor;
  4. namespace YooAsset.Editor
  5. {
  6. public class PackageImportWindow : EditorWindow
  7. {
  8. static PackageImportWindow _thisInstance;
  9. [MenuItem("YooAsset/补丁包导入工具", false, 301)]
  10. static void ShowWindow()
  11. {
  12. if (_thisInstance == null)
  13. {
  14. _thisInstance = EditorWindow.GetWindow(typeof(PackageImportWindow), false, "补丁包导入工具", true) as PackageImportWindow;
  15. _thisInstance.minSize = new Vector2(800, 600);
  16. }
  17. _thisInstance.Show();
  18. }
  19. private string _manifestPath = string.Empty;
  20. private string _packageName = "DefaultPackage";
  21. private void OnGUI()
  22. {
  23. GUILayout.Space(10);
  24. EditorGUILayout.BeginHorizontal();
  25. if (GUILayout.Button("选择补丁包", GUILayout.MaxWidth(150)))
  26. {
  27. string resultPath = EditorUtility.OpenFilePanel("Find", "Assets/", "bytes");
  28. if (string.IsNullOrEmpty(resultPath))
  29. return;
  30. _manifestPath = resultPath;
  31. }
  32. EditorGUILayout.LabelField(_manifestPath);
  33. EditorGUILayout.EndHorizontal();
  34. if (string.IsNullOrEmpty(_manifestPath) == false)
  35. {
  36. if (GUILayout.Button("导入补丁包(全部文件)", GUILayout.MaxWidth(150)))
  37. {
  38. string streamingAssetsRoot = AssetBundleBuilderHelper.GetStreamingAssetsRoot();
  39. EditorTools.ClearFolder(streamingAssetsRoot);
  40. CopyPackageFiles(_manifestPath);
  41. }
  42. }
  43. }
  44. private void CopyPackageFiles(string manifestFilePath)
  45. {
  46. string manifestFileName = Path.GetFileNameWithoutExtension(manifestFilePath);
  47. string outputDirectory = Path.GetDirectoryName(manifestFilePath);
  48. // 加载补丁清单
  49. byte[] bytesData = FileUtility.ReadAllBytes(manifestFilePath);
  50. PackageManifest manifest = ManifestTools.DeserializeFromBinary(bytesData);
  51. // 拷贝核心文件
  52. {
  53. string sourcePath = $"{outputDirectory}/{manifestFileName}.bytes";
  54. string destPath = $"{AssetBundleBuilderHelper.GetStreamingAssetsRoot()}/{_packageName}/{manifestFileName}.bytes";
  55. EditorTools.CopyFile(sourcePath, destPath, true);
  56. }
  57. {
  58. string sourcePath = $"{outputDirectory}/{manifestFileName}.hash";
  59. string destPath = $"{AssetBundleBuilderHelper.GetStreamingAssetsRoot()}/{_packageName}/{manifestFileName}.hash";
  60. EditorTools.CopyFile(sourcePath, destPath, true);
  61. }
  62. {
  63. string fileName = YooAssetSettingsData.GetPackageVersionFileName(manifest.PackageName);
  64. string sourcePath = $"{outputDirectory}/{fileName}";
  65. string destPath = $"{AssetBundleBuilderHelper.GetStreamingAssetsRoot()}/{_packageName}/{fileName}";
  66. EditorTools.CopyFile(sourcePath, destPath, true);
  67. }
  68. // 拷贝文件列表
  69. int fileCount = 0;
  70. foreach (var packageBundle in manifest.BundleList)
  71. {
  72. fileCount++;
  73. string sourcePath = $"{outputDirectory}/{packageBundle.FileName}";
  74. string destPath = $"{AssetBundleBuilderHelper.GetStreamingAssetsRoot()}/{_packageName}/{packageBundle.FileName}";
  75. EditorTools.CopyFile(sourcePath, destPath, true);
  76. }
  77. Debug.Log($"补丁包拷贝完成,一共拷贝了{fileCount}个资源文件");
  78. AssetDatabase.Refresh();
  79. }
  80. }
  81. }