AssetBundleGroup.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. namespace assetBundleUtility
  2. {
  3. #if UNITY_EDITOR
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using UnityEditor;
  9. [Serializable]
  10. public class AssetBundleGroup
  11. {
  12. #region Config
  13. public string MD5FileName = "MD5.txt";
  14. public string MD5DictionaryFileName = "MD5Dictionary.txt";
  15. public string OutputPath;
  16. public List<AssetBundleSet> AssetBundleSets = new List<AssetBundleSet>();
  17. //CustomPropertyDrawer使用
  18. [NonSerialized] public float TotalHeight;
  19. #endregion
  20. public void CreateMD5FileAndMD5DictionaryFile()
  21. {
  22. string md5FilePath = CheckOutputPath(OutputPath, MD5FileName);
  23. string md5DictionaryFilePath = CheckOutputPath(OutputPath, MD5DictionaryFileName);
  24. List<AssetBundleSet> assetBundleSets = new List<AssetBundleSet>();
  25. for (int i = 0; i < AssetBundleSets.Count; i++)
  26. {
  27. AssetBundleSet assetBundleSet = AssetBundleSets[i];
  28. if (assetBundleSet.Object == null) continue;
  29. assetBundleSet.Name = assetBundleSet.Object.name;
  30. assetBundleSet.StreamingPath = AssetDatabase.GetAssetPath(assetBundleSet.Object);
  31. assetBundleSet.MD5 = MD5Utility.GetMD5StringFromPath(assetBundleSet.StreamingPath);
  32. assetBundleSets.Add(assetBundleSet);
  33. }
  34. assetBundleSets.MySort((bundleSet0, bundleSet1) => SortExtension.CompareASCII(bundleSet1.Name, bundleSet0.Name));
  35. string md5Dictionary = AssetBundleUtility.CreateMD5Dictionary(assetBundleSets, false);
  36. byte[] bytes = MD5Utility.GetMD5(md5Dictionary);
  37. string md5 = MD5Utility.BytesToString(bytes);
  38. File.WriteAllText(md5FilePath, md5);
  39. md5Dictionary = AssetBundleUtility.CreateMD5Dictionary(assetBundleSets, true);
  40. File.WriteAllText(md5DictionaryFilePath, md5Dictionary);
  41. AssetDatabase.Refresh();
  42. }
  43. private string CheckOutputPath(string path, string name)
  44. {
  45. string directory = path.TrimEnd('/', '\\') + Path.DirectorySeparatorChar;
  46. if (directory == Path.DirectorySeparatorChar.ToString() || !Directory.Exists(directory))
  47. {
  48. throw new Exception("文件夹不存在");
  49. }
  50. if (string.IsNullOrEmpty(name) || name.Any(Path.GetInvalidFileNameChars().Contains))
  51. {
  52. throw new Exception("文件名包含无效字符");
  53. }
  54. return $"{path}{Path.DirectorySeparatorChar}{name}";
  55. }
  56. }
  57. #endif
  58. }