1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine;
public class AssetBundlePackager { private AssetBundleConfig config; private AssetBundleManifestData manifestData; private string outputPath;
public AssetBundlePackager(AssetBundleConfig config) { this.config = config; this.outputPath = Path.Combine("AssetBundles", GetPlatformName()); this.manifestData = new AssetBundleManifestData(); }
public void Build() { Debug.Log("开始打包 AssetBundle...");
EnsureOutputDirectory();
List<AssetBundleBuild> builds = CollectBuilds();
if (builds.Count == 0) { Debug.LogWarning("没有找到需要打包的资源"); return; }
var manifest = BuildPipeline.BuildAssetBundles( outputPath, builds.ToArray(), BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DisableWriteTypeTree, EditorUserBuildSettings.activeBuildTarget );
if (manifest == null) { Debug.LogError("AssetBundle 打包失败"); return; }
GenerateManifestData(manifest);
string manifestPath = Path.Combine(outputPath, config.versionFile); manifestData.version = GetNextVersion(); manifestData.Save(manifestPath);
CopyToStreamingAssets();
Debug.Log($"AssetBundle 打包完成! 共 {builds.Count} 个包"); }
private void EnsureOutputDirectory() { if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); }
foreach (var file in Directory.GetFiles(outputPath)) { if (!file.EndsWith(".meta")) { File.Delete(file); } } }
private List<AssetBundleBuild> CollectBuilds() { List<AssetBundleBuild> builds = new List<AssetBundleBuild>();
foreach (var rule in config.rules) { foreach (var assetPath in CollectAssetsByRule(rule)) { string bundleName = AssetBundleNaming.GetBundleName( assetPath, rule.assetType, config.bundleNamePrefix );
builds.Add(new AssetBundleBuild { assetBundleName = bundleName, assetNames = new[] { assetPath } }); } }
return builds; }
private IEnumerable<string> CollectAssetsByRule(AssetBundleConfig.BundleRule rule) { foreach (var searchPath in rule.searchPaths) { string fullPath = Path.Combine("Assets", searchPath); if (!Directory.Exists(fullPath)) continue;
foreach (var ext in rule.extensions) { foreach (var file in Directory.GetFiles(fullPath, "*" + ext, SearchOption.AllDirectories)) { string assetPath = "Assets" + file.Substring(Application.dataPath.Length); yield return assetPath; } } } }
private void GenerateManifestData(AssetBundleManifest unityManifest) { manifestData.bundles.Clear();
string[] allBundles = unityManifest.GetAllAssetBundles();
foreach (string bundle in allBundles) { var bundleInfo = new AssetBundleManifestData.BundleInfo { bundleName = bundle, hash = GetBundleHash(bundle, unityManifest), dependencies = unityManifest.GetAllDependencies(bundle), assets = unityManifest.GetAllAssetBundles() };
manifestData.bundles.Add(bundleInfo); } }
private string GetBundleHash(string bundleName, AssetBundleManifest manifest) { return manifest.GetAssetBundleHash(bundleName).ToString(); }
private int GetNextVersion() { return DateTime.Now.ToString("yyyyMMddHHmmss").ToInt(); }
private void CopyToStreamingAssets() { string streamingPath = Path.Combine(Application.streamingAssetsPath, "AssetBundles");
if (Directory.Exists(streamingPath)) { Directory.Delete(streamingPath, true); }
DirectoryCopy(outputPath, streamingPath, true); AssetDatabase.Refresh(); }
private static void DirectoryCopy(string sourceDir, string targetDir, bool recursive) { DirectoryInfo dir = new DirectoryInfo(sourceDir); DirectoryInfo[] dirs = dir.GetDirectories();
if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); }
FileInfo[] files = dir.GetFiles(); foreach (FileInfo file in files) { string tempPath = Path.Combine(targetDir, file.Name); file.CopyTo(tempPath, false); }
if (recursive) { foreach (DirectoryInfo subdir in dirs) { string tempPath = Path.Combine(targetDir, subdir.Name); DirectoryCopy(subdir.FullName, tempPath, recursive); } } }
private static string GetPlatformName() { return EditorUserBuildSettings.activeBuildTarget switch { BuildTarget.StandaloneWindows => "Windows", BuildTarget.StandaloneOSX => "macOS", BuildTarget.StandaloneLinux64 => "Linux", BuildTarget.iOS => "iOS", BuildTarget.Android => "Android", BuildTarget.WebGL => "WebGL", _ => "Unknown" }; } }
|