コード例 #1
0
        private static List <Material> GetAllMaterials()
        {
            int           progressValue = 0;
            List <string> allAssets     = new List <string>(1000);

            // 获取所有打包的资源
            List <AssetCollectInfo> allCollectInfos = AssetBundleCollectorSettingData.GetAllCollectAssets();
            List <string>           collectAssets   = allCollectInfos.Select(t => t.AssetPath).ToList();

            foreach (var assetPath in collectAssets)
            {
                string[] depends = AssetDatabase.GetDependencies(assetPath, true);
                foreach (var depend in depends)
                {
                    if (allAssets.Contains(depend) == false)
                    {
                        allAssets.Add(depend);
                    }
                }
                EditorTools.DisplayProgressBar("获取所有打包资源", ++progressValue, collectAssets.Count);
            }
            EditorTools.ClearProgressBar();

            // 搜集所有材质球
            progressValue = 0;
            var shaderDic = new Dictionary <Shader, List <Material> >(100);

            foreach (var assetPath in allAssets)
            {
                System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
                if (assetType == typeof(UnityEngine.Material))
                {
                    var material = AssetDatabase.LoadAssetAtPath <Material>(assetPath);
                    var shader   = material.shader;
                    if (shader == null)
                    {
                        continue;
                    }

                    if (shaderDic.ContainsKey(shader) == false)
                    {
                        shaderDic.Add(shader, new List <Material>());
                    }
                    if (shaderDic[shader].Contains(material) == false)
                    {
                        shaderDic[shader].Add(material);
                    }
                }
                EditorTools.DisplayProgressBar("搜集所有材质球", ++progressValue, allAssets.Count);
            }
            EditorTools.ClearProgressBar();

            // 返回结果
            var materials = new List <Material>(1000);

            foreach (var valuePair in shaderDic)
            {
                materials.AddRange(valuePair.Value);
            }
            return(materials);
        }
コード例 #2
0
        /// <summary>
        /// 获取构建的资源列表
        /// </summary>
        private List <AssetInfo> GetBuildAssets()
        {
            Dictionary <string, AssetInfo> buildAssets = new Dictionary <string, AssetInfo>();
            Dictionary <string, string>    references  = new Dictionary <string, string>();

            // 1. 获取主动收集的资源
            List <AssetCollectInfo> allCollectAssets = AssetBundleCollectorSettingData.GetAllCollectAssets();

            // 2. 对收集的资源进行依赖分析
            int progressValue = 0;

            foreach (AssetCollectInfo collectInfo in allCollectAssets)
            {
                string           mainAssetPath = collectInfo.AssetPath;
                List <AssetInfo> depends       = GetDependencies(mainAssetPath);
                for (int i = 0; i < depends.Count; i++)
                {
                    AssetInfo assetInfo = depends[i];
                    string    assetPath = assetInfo.AssetPath;

                    // 如果已经存在,则增加该资源的依赖计数
                    if (buildAssets.ContainsKey(assetPath))
                    {
                        buildAssets[assetPath].DependCount++;
                    }
                    else
                    {
                        buildAssets.Add(assetPath, assetInfo);
                        references.Add(assetPath, mainAssetPath);
                    }

                    // 添加资源标记
                    buildAssets[assetPath].AddAssetTags(collectInfo.AssetTags);

                    // 注意:检测是否为主动收集资源
                    if (assetPath == mainAssetPath)
                    {
                        buildAssets[assetPath].IsCollectAsset     = true;
                        buildAssets[assetPath].DontWriteAssetPath = collectInfo.DontWriteAssetPath;
                    }
                }
                EditorTools.DisplayProgressBar("依赖文件分析", ++progressValue, allCollectAssets.Count);
            }
            EditorTools.ClearProgressBar();

            // 3. 移除零依赖的资源
            List <AssetInfo> undependentAssets = new List <AssetInfo>();

            foreach (KeyValuePair <string, AssetInfo> pair in buildAssets)
            {
                if (pair.Value.IsCollectAsset)
                {
                    continue;
                }
                if (pair.Value.DependCount == 0)
                {
                    undependentAssets.Add(pair.Value);
                }
            }
            foreach (var assetInfo in undependentAssets)
            {
                buildAssets.Remove(assetInfo.AssetPath);
            }

            // 4. 设置资源标签和变种
            progressValue = 0;
            foreach (KeyValuePair <string, AssetInfo> pair in buildAssets)
            {
                var assetInfo             = pair.Value;
                var bundleLabelAndVariant = AssetBundleCollectorSettingData.GetBundleLabelAndVariant(assetInfo.AssetPath);
                assetInfo.SetBundleLabelAndVariant(bundleLabelAndVariant.BundleLabel, bundleLabelAndVariant.BundleVariant);
                EditorTools.DisplayProgressBar("设置资源标签", ++progressValue, buildAssets.Count);
            }
            EditorTools.ClearProgressBar();

            // 5. 补充零依赖的资源
            foreach (var assetInfo in undependentAssets)
            {
                var referenceAssetPath = references[assetInfo.AssetPath];
                var referenceAssetInfo = buildAssets[referenceAssetPath];
                assetInfo.SetBundleLabelAndVariant(referenceAssetInfo.AssetBundleLabel, referenceAssetInfo.AssetBundleVariant);
                buildAssets.Add(assetInfo.AssetPath, assetInfo);
            }

            // 6. 返回结果
            return(buildAssets.Values.ToList());
        }
コード例 #3
0
        /// <summary>
        /// 准备工作
        /// </summary>
        private List <AssetInfo> GetBuildMap()
        {
            int progressBarCount = 0;
            Dictionary <string, AssetInfo> buildMap = new Dictionary <string, AssetInfo>();

            // 获取要收集的资源
            List <string> allCollectAssets = AssetBundleCollectorSettingData.GetAllCollectAssets();

            // 进行依赖分析
            foreach (string mainAssetPath in allCollectAssets)
            {
                List <AssetInfo> depends = GetDependencies(mainAssetPath);
                for (int i = 0; i < depends.Count; i++)
                {
                    AssetInfo assetInfo = depends[i];
                    if (buildMap.ContainsKey(assetInfo.AssetPath))
                    {
                        buildMap[assetInfo.AssetPath].DependCount++;
                    }
                    else
                    {
                        buildMap.Add(assetInfo.AssetPath, assetInfo);
                    }
                }
                progressBarCount++;
                EditorUtility.DisplayProgressBar("进度", $"依赖文件分析:{progressBarCount}/{allCollectAssets.Count}", (float)progressBarCount / allCollectAssets.Count);
            }
            EditorUtility.ClearProgressBar();
            progressBarCount = 0;

            // 移除零依赖的资源
            List <string> removeList = new List <string>();

            foreach (KeyValuePair <string, AssetInfo> pair in buildMap)
            {
                if (pair.Value.IsCollectAsset)
                {
                    continue;
                }
                if (pair.Value.DependCount == 0)
                {
                    removeList.Add(pair.Value.AssetPath);
                }
            }
            for (int i = 0; i < removeList.Count; i++)
            {
                buildMap.Remove(removeList[i]);
            }

            // 设置资源标签
            foreach (KeyValuePair <string, AssetInfo> pair in buildMap)
            {
                var assetInfo       = pair.Value;
                var labelAndVariant = AssetBundleCollectorSettingData.GetBundleLabelAndVariant(assetInfo.AssetPath);
                assetInfo.AssetBundleLabel   = labelAndVariant.BundleLabel;
                assetInfo.AssetBundleVariant = labelAndVariant.BundleVariant;
                progressBarCount++;
                EditorUtility.DisplayProgressBar("进度", $"设置资源标签:{progressBarCount}/{buildMap.Count}", (float)progressBarCount / buildMap.Count);
            }
            EditorUtility.ClearProgressBar();

            // 返回结果
            return(buildMap.Values.ToList());
        }
コード例 #4
0
        /// <summary>
        /// 获取构建的资源列表
        /// </summary>
        private List <AssetInfo> GetBuildAssets()
        {
            int progressBarCount = 0;
            Dictionary <string, AssetInfo> buildAssets = new Dictionary <string, AssetInfo>();

            // 1. 获取主动收集的资源
            List <string> allCollectAssets = AssetBundleCollectorSettingData.GetAllCollectAssets();

            // 2. 对收集的资源进行依赖分析
            foreach (string collectAssetPath in allCollectAssets)
            {
                List <AssetInfo> depends = GetDependencies(collectAssetPath);
                for (int i = 0; i < depends.Count; i++)
                {
                    AssetInfo assetInfo = depends[i];
                    if (buildAssets.ContainsKey(assetInfo.AssetPath))
                    {
                        buildAssets[assetInfo.AssetPath].DependCount++;
                    }
                    else
                    {
                        buildAssets.Add(assetInfo.AssetPath, assetInfo);
                    }

                    // 注意:检测是否为主动收集资源
                    if (assetInfo.AssetPath == collectAssetPath)
                    {
                        buildAssets[collectAssetPath].IsCollectAsset = true;
                    }
                }
                progressBarCount++;
                EditorUtility.DisplayProgressBar("进度", $"依赖文件分析:{progressBarCount}/{allCollectAssets.Count}", (float)progressBarCount / allCollectAssets.Count);
            }
            progressBarCount = 0;
            EditorUtility.ClearProgressBar();

            // 3. 移除零依赖的资源
            List <string> removeList = new List <string>();

            foreach (KeyValuePair <string, AssetInfo> pair in buildAssets)
            {
                if (pair.Value.IsCollectAsset)
                {
                    continue;
                }
                if (pair.Value.DependCount == 0)
                {
                    removeList.Add(pair.Value.AssetPath);
                }
            }
            for (int i = 0; i < removeList.Count; i++)
            {
                buildAssets.Remove(removeList[i]);
            }

            // 4. 设置资源标签和变种
            foreach (KeyValuePair <string, AssetInfo> pair in buildAssets)
            {
                var assetInfo             = pair.Value;
                var bundleLabelAndVariant = AssetBundleCollectorSettingData.GetBundleLabelAndVariant(assetInfo.AssetPath, assetInfo.AssetType);
                assetInfo.SetBundleLabelAndVariant(bundleLabelAndVariant.BundleLabel, bundleLabelAndVariant.BundleVariant);
                progressBarCount++;
                EditorUtility.DisplayProgressBar("进度", $"设置资源标签:{progressBarCount}/{buildAssets.Count}", (float)progressBarCount / buildAssets.Count);
            }
            EditorUtility.ClearProgressBar();

            // 5. 返回结果
            return(buildAssets.Values.ToList());
        }