private static void AddRulesForSelection(BuildRules rules, string searchPattern) { var isDir = rules.searchPatternDir.Equals(searchPattern); foreach (var item in Selection.objects) { var path = AssetDatabase.GetAssetPath(item); var rule = new BuildRule { searchPath = path, searchPattern = searchPattern, nameBy = isDir ? NameBy.Directory : NameBy.Path }; ArrayUtility.Add(ref rules.rules, rule); } EditorUtility.SetDirty(rules); AssetDatabase.SaveAssets(); }
private static void AddRulesForSelection(BuildRules rules, string searchPattern) { var isDir = rules.searchPatternDir.Equals(searchPattern); foreach (var item in Selection.objects) { var path = AssetDatabase.GetAssetPath(item); var rule = new BuildRule(); rule.searchPath = path; rule.searchPattern = searchPattern; rule.searchOption = SearchOption.AllDirectories; rule.unshared = false; rule.searchDirOnly = isDir; ArrayUtility.Add <BuildRule>(ref rules.rules, rule); } EditorUtility.SetDirty(rules); AssetDatabase.SaveAssets(); Selection.activeObject = rules; EditorUtility.FocusProjectWindow(); }
public override void OnInspectorGUI() { BuildRules rules = target as BuildRules; EditorGUILayout.HelpBox("【操作提示】" + "\n - 编辑器菜单中 Assets/Apply Rule 可以针对 Project 视图中选中的文件夹快速创建目标规则:" + "\n - Text:" + rules.searchPatternText + "\n - Prefab" + rules.searchPatternPrefab + "\n - PNG" + rules.searchPatternPNG + "\n - Material" + rules.searchPatternMaterial + "\n - Controller" + rules.searchPatternController + "\n - Asset" + rules.searchPatternAsset + "\n - Scene" + rules.searchPatternScene + "\n【注意事项】:" + "\n - 所有shader放到一个包" + "\n - 场景文件不可以和非场景文件放到一个包" + "\n - 预知体通常单个文件一个包" + "\n - 资源冗余可以自行集成 AssetBundle-Browser 查看" + "\n作者:fjy" + "\n邮箱:[email protected]", MessageType.None); using (var h = new EditorGUILayout.HorizontalScope()) { GUILayout.Space(16); if (GUILayout.Button("清理")) { if (EditorUtility.DisplayDialog("提示", "是否确定清理?", "确定")) { rules.rules = new BuildRule[0]; } } if (GUILayout.Button("执行")) { rules.Apply(); } GUILayout.Space(16); } base.OnInspectorGUI(); }
public string[] GetAssets() { var patterns = searchPattern.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (!Directory.Exists(searchPath)) { GameLog.LogWarning("Rule searchPath not exist:" + searchPath); return(new string[0]); } var getFiles = new List <string>(); foreach (string item in patterns) { string[] files = Directory.GetFiles(searchPath, item, SearchOption.AllDirectories); foreach (string file in files) { // 忽略目录?可以省略吧 todo if (Directory.Exists(file)) { continue; } string ext = Path.GetExtension(file).ToLower(); if ((ext == ".fbx" || ext == ".anim") && !item.Contains(ext)) { continue; } if (!BuildRules.ValidateAsset(file)) { continue; } string asset = file.Replace("\\", "/"); getFiles.Add(asset); } } return(getFiles.ToArray()); }
private static void BuildVersions(AssetBundleManifest manifest, BuildRules rules) { var allBundles = manifest.GetAllAssetBundles(); var bundle2Ids = GetBundle2Ids(allBundles); var bundles = GetBundles(manifest, allBundles, bundle2Ids); var ver = rules.AddVersion(); var dirs = new List <string>(); var assets = new List <AssetRef>(); var patches = new List <Patch>(); var asset2Bundles = new Dictionary <string, BundleRef>(); foreach (var item in rules.assets) { var path = item.name; var dir = Path.GetDirectoryName(path); if (!string.IsNullOrEmpty(dir)) { dir = dir.Replace("\\", "/"); } var index = dirs.FindIndex(o => o.Equals(dir)); if (index == -1) { index = dirs.Count; dirs.Add(dir); } var asset = new AssetRef(); if (item.groupBy == GroupBy.None) { var id = AddBundle(path, asset, ref bundles); asset.bundle = id; } else { bundle2Ids.TryGetValue(item.bundle, out asset.bundle); } asset2Bundles[path] = bundles[asset.bundle]; asset.dir = index; asset.name = Path.GetFileName(path); assets.Add(asset); } Func <List <string>, List <int> > getFiles = delegate(List <string> list) { var ret = new List <int>(); foreach (var file in list) { BundleRef bundle; asset2Bundles.TryGetValue(file, out bundle); if (bundle != null) { if (!ret.Contains(bundle.id)) { ret.Add(bundle.id); } foreach (var child in bundle.children) { if (!ret.Contains(child)) { ret.Add(child); } } } else { Debug.LogWarning("bundle == nil, file:" + file); } } return(ret); }; for (var i = 0; i < rules.patches.Count; i++) { var item = rules.patches[i]; patches.Add(new Patch { name = item.name, files = getFiles(item.assets), }); } var versions = new Versions(); versions.activeVariants = manifest.GetAllAssetBundlesWithVariant(); versions.dirs = dirs.ToArray(); versions.assets = assets; versions.bundles = bundles; versions.patches = patches; versions.ver = ver; if (rules.allAssetsToBuild) { bundles.ForEach(obj => obj.location = 1); } else { foreach (var patchName in rules.patchesInBuild) { var patch = versions.patches.Find((Patch item) => { return(item.name.Equals(patchName)); }); if (patch != null) { foreach (var file in patch.files) { if (file >= 0 && file < bundles.Count) { bundles[file].location = 1; } } } } } versions.Save(outputPath + "/" + Assets.Versions); }
private static void BuildManifest(AssetBundleManifest assetBundleManifest, string bundleDir, BuildRules rules) { var manifest = GetManifest(); var allAssetBundles = assetBundleManifest.GetAllAssetBundles(); var bundle2Ids = GetBundle2Ids(allAssetBundles); var bundles = GetBundles(assetBundleManifest, bundleDir, allAssetBundles, bundle2Ids); var dirs = new List <string>(); var assets = new List <AssetRef>(); var patches = new List <VPatch>(); for (var i = 0; i < rules.assets.Length; i++) { var item = rules.assets[i]; var path = item.path; var dir = Path.GetDirectoryName(path).Replace("\\", "/"); var index = dirs.FindIndex(o => o.Equals(dir)); if (index == -1) { index = dirs.Count; dirs.Add(dir); } var asset = new AssetRef(); if (!bundle2Ids.TryGetValue(item.bundle, out asset.bundle)) { // 第三方资源 var bundle = new BundleRef(); bundle.id = bundles.Count; bundle.name = Path.GetFileName(path); using (var stream = File.OpenRead(path)) { bundle.len = stream.Length; bundle.crc = Utility.GetCRC32Hash(stream); } bundles.Add(bundle); asset.bundle = bundle.id; } asset.dir = index; asset.name = Path.GetFileName(path); assets.Add(asset); var patch = patches.Find(pr => pr.@by == item.patch); if (patch == null) { patch = new VPatch() { @by = item.patch }; patches.Add(patch); } if (asset.bundle != -1) { if (!patch.files.Contains(asset.bundle)) { patch.files.Add(asset.bundle); } var bundle = bundles[asset.bundle]; foreach (var child in bundle.children) { if (!patch.files.Contains(child)) { patch.files.Add(child); } } } } manifest.dirs = dirs.ToArray(); manifest.assets = assets.ToArray(); manifest.bundles = bundles.ToArray(); EditorUtility.SetDirty(manifest); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); var manifestBundleName = "manifest.unity3d"; var builds = new[] { new AssetBundleBuild { assetNames = new[] { AssetDatabase.GetAssetPath(manifest), }, assetBundleName = manifestBundleName } }; var targetPlatform = EditorUserBuildSettings.activeBuildTarget; BuildPipeline.BuildAssetBundles(bundleDir, builds, rules.buildBundleOptions, targetPlatform); { var path = bundleDir + "/" + manifestBundleName; var bundle = new BundleRef(); bundle.id = bundles.Count; bundle.name = Path.GetFileName(path); using (var stream = File.OpenRead(path)) { bundle.len = stream.Length; bundle.crc = Utility.GetCRC32Hash(stream); } var patch = patches.Find(pr => pr.@by == PatchBy.Level0); if (patch == null) { patch = new VPatch() { @by = PatchBy.Level0 }; patches.Add(patch); } bundles.Add(bundle); } Versions.BuildVersion(bundleDir, bundles, patches, GetBuildRules().AddVersion()); }