Beispiel #1
0
        private void ApplyRule(BuildRule rule)
        {
            var path            = rule.searchPath;
            var patterns        = rule.searchPattern.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var searchOption    = rule.searchOption;
            var searchDir       = rule.searchDirOnly;
            var assetBundleName = rule.assetBundleName;
            int startIndex      = path.LastIndexOf('/') + 1;
            var suffix          = searchDir ? "_x" : "";

            foreach (var item in patterns)
            {
                string[] files = searchDir ? Directory.GetDirectories(path, item, searchOption) : Directory.GetFiles(path, item, searchOption);
                foreach (string file in files)
                {
                    if (!searchDir && Directory.Exists(file))
                    {
                        continue;
                    }

                    var asset = file.Replace("\\", "/");
                    if (string.IsNullOrEmpty(assetBundleName))
                    {
                        string assetName     = Path.GetFileNameWithoutExtension(asset);
                        string directoryName = Path.GetDirectoryName(asset);
                        string str           = directoryName.Substring(startIndex);
                        asset2bundles[asset] = (str + "/" + assetName).Replace("\\", "/").ToLower() + suffix; // 防止目录名称和 ab 名称冲突
                    }
                    else
                    {
                        asset2bundles[asset] = assetBundleName;
                    }
                }
            }
        }
Beispiel #2
0
        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();
        }
Beispiel #3
0
        public Dictionary <string, string> Apply()
        {
            tracker.Clear();
            duplicated.Clear();
            conflicted.Clear();
            asset2bundles.Clear();

            for (int i = 0, max = rules.Length; i < max; i++)
            {
                BuildRule rule = rules[i];
                if (EditorUtility.DisplayCancelableProgressBar(string.Format("执行规则{0}/{1}", i, max), rule.searchPath, i / (float)max))
                {
                    break;
                }
                if (rule.unshared)
                {
                    if (!unshared.Contains(rule.searchPath))
                    {
                        unshared.Add(rule.searchPath);
                    }
                }
                ApplyRule(rule);
            }

            {
                int i = 0, max = asset2bundles.Count;
                foreach (var item in asset2bundles)
                {
                    string bundle = item.Value;
                    if (EditorUtility.DisplayCancelableProgressBar(string.Format("设置包名{0}/{1}", i, max), bundle, i / (float)max))
                    {
                        break;
                    }
                    var ai = AssetImporter.GetAtPath(item.Key);
                    if (ai != null && !bundle.Equals(ai.assetBundleName))
                    {
                        ai.assetBundleName = bundle;
                    }
                    i++;
                }
            }

            EditorUtility.ClearProgressBar();
            AssetDatabase.Refresh();
            AssetDatabase.RemoveUnusedAssetBundleNames();

            string[] bundles = AssetDatabase.GetAllAssetBundleNames();
            for (int i = 0, max = bundles.Length; i < max; i++)
            {
                string bundle = bundles[i];
                if (EditorUtility.DisplayCancelableProgressBar(string.Format("分析依赖{0}/{1}", i, max), bundle, i / (float)max))
                {
                    break;
                }

                string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(bundle);
                if (Array.Exists(assetPaths, IsScene) && !Array.TrueForAll(assetPaths, IsScene))
                {
                    conflicted.Add(bundle, assetPaths);
                }

                string[] dependencies = AssetDatabase.GetDependencies(assetPaths, true);
                foreach (string asset in dependencies)
                {
                    if (ValidateAsset(asset) && !unshared.Exists((s) => asset.Contains(s)))
                    {
                        Track(asset, bundle);
                    }
                }
            }

            {
                int i = 0, max = conflicted.Count;
                foreach (var item in conflicted)
                {
                    if (EditorUtility.DisplayCancelableProgressBar(string.Format("解决冲突{0}/{1}", i, max), item.Key, i / (float)max))
                    {
                        break;
                    }
                    string[] assets = item.Value;
                    foreach (string asset in assets)
                    {
                        if (!IsScene(asset))
                        {
                            duplicated.Add(asset);
                        }
                    }
                    i++;
                }
            }

            for (int i = 0, max = duplicated.Count; i < max; i++)
            {
                string item = duplicated[i];
                if (EditorUtility.DisplayCancelableProgressBar(string.Format("解决冗余{0}/{1}", i, max), item, i / (float)max))
                {
                    break;
                }
                OptimazieAsset(item);
            }
            duplicated.Clear();
            EditorUtility.ClearProgressBar();
            return(asset2bundles);
        }