1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- namespace assetBundleUtility
- {
- #if UNITY_EDITOR
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using UnityEditor;
-
- [Serializable]
- public class AssetBundleGroup
- {
- #region Config
- public string MD5FileName = "MD5.txt";
- public string MD5DictionaryFileName = "MD5Dictionary.txt";
- public string OutputPath;
- public List<AssetBundleSet> AssetBundleSets = new List<AssetBundleSet>();
- //CustomPropertyDrawer使用
- [NonSerialized] public float TotalHeight;
- #endregion
-
- public void CreateMD5FileAndMD5DictionaryFile()
- {
- string md5FilePath = CheckOutputPath(OutputPath, MD5FileName);
- string md5DictionaryFilePath = CheckOutputPath(OutputPath, MD5DictionaryFileName);
-
- List<AssetBundleSet> assetBundleSets = new List<AssetBundleSet>();
- for (int i = 0; i < AssetBundleSets.Count; i++)
- {
- AssetBundleSet assetBundleSet = AssetBundleSets[i];
- if (assetBundleSet.Object == null) continue;
- assetBundleSet.Name = assetBundleSet.Object.name;
- assetBundleSet.StreamingPath = AssetDatabase.GetAssetPath(assetBundleSet.Object);
- assetBundleSet.MD5 = MD5Utility.GetMD5StringFromPath(assetBundleSet.StreamingPath);
- assetBundleSets.Add(assetBundleSet);
- }
- assetBundleSets.MySort((bundleSet0, bundleSet1) => SortExtension.CompareASCII(bundleSet1.Name, bundleSet0.Name));
- string md5Dictionary = AssetBundleUtility.CreateMD5Dictionary(assetBundleSets, false);
- byte[] bytes = MD5Utility.GetMD5(md5Dictionary);
- string md5 = MD5Utility.BytesToString(bytes);
- File.WriteAllText(md5FilePath, md5);
- md5Dictionary = AssetBundleUtility.CreateMD5Dictionary(assetBundleSets, true);
- File.WriteAllText(md5DictionaryFilePath, md5Dictionary);
- AssetDatabase.Refresh();
- }
-
- private string CheckOutputPath(string path, string name)
- {
- string directory = path.TrimEnd('/', '\\') + Path.DirectorySeparatorChar;
-
- if (directory == Path.DirectorySeparatorChar.ToString() || !Directory.Exists(directory))
- {
- throw new Exception("文件夹不存在");
- }
-
- if (string.IsNullOrEmpty(name) || name.Any(Path.GetInvalidFileNameChars().Contains))
- {
- throw new Exception("文件名包含无效字符");
- }
-
- return $"{path}{Path.DirectorySeparatorChar}{name}";
- }
- }
- #endif
- }
|