示例#1
0
 /// <summary>
 /// 根据路径创建新的资源;
 /// </summary>
 /// <param name="path">资源路径</param>
 /// <returns>资源节点</returns>
 public AssetNode CreateNewAssetNode(string path)
 {
     return(new AssetNode()
     {
         type = BuildDefine.GetAssetType(path),
         assetName = Path.GetFileNameWithoutExtension(path),
         assetPath = path,
         parentDependentAssets = new HashSet <string>(),
         sonDependentAssets = new HashSet <string>()
     });
 }
示例#2
0
    /// <summary>
    /// 分析全部资源依赖关系;
    /// </summary>
    public void AnalysisAllAsset()
    {
        Stopwatch watch = Stopwatch.StartNew();//开启计时;

        string[] allPath = Directory.GetFiles(FilePathUtil.resPath, "*.*", SearchOption.AllDirectories);

        //剔除.meta文件;
        List <string> allAssetPath = new List <string>();

        foreach (string tempPath in allPath)
        {
            string path = tempPath.Replace("\\", "/");
            if (Path.GetExtension(path) == ".meta")
            {
                continue;
            }
            allAssetPath.Add(path);
        }

        //开始分析资源依赖关系;
        for (int i = 0; i < allAssetPath.Count; i++)
        {
            EditorUtility.DisplayProgressBar("Start Analysis All Asset", "Analysis Progress", (i / allAssetPath.Count));

            //还未遍历到该资源;
            if (!allAsset.ContainsKey(allAssetPath[i]))
            {
                allAsset[allAssetPath[i]] = CreateNewAssetNode(allAssetPath[i]);
            }
            //获取依赖关系;
            string[] allDirectDependencies = AssetDatabase.GetDependencies(allAssetPath[i], false);
            foreach (string tempPath in allDirectDependencies)
            {
                //依赖脚本直接添加到脚本队列;
                if (BuildDefine.GetAssetType(tempPath) == AssetType.Scripts)
                {
                    continue;
                }
                //依赖Shader直接添加到Shader队列;
                if (BuildDefine.GetAssetType(tempPath) == AssetType.Shader)
                {
                    allShaderAsset.Add(tempPath);
                    continue;
                }
                //Lua文件直接添加到Lua队列;
                if (BuildDefine.GetAssetType(tempPath) == AssetType.Lua)
                {
                    allLuaAsset.Add(tempPath);
                    continue;
                }
                if (tempPath.Contains(FilePathUtil.resPath))
                {
                    //添加依赖的资源信息;
                    allAsset[allAssetPath[i]].sonDependentAssets.Add(tempPath);
                    //添加被依赖的资源信息;
                    if (!allAsset.ContainsKey(tempPath))
                    {
                        allAsset[tempPath] = CreateNewAssetNode(tempPath);
                    }
                    allAsset[tempPath].parentDependentAssets.Add(allAssetPath[i]);
                }
                else
                {
                    //需要打包AssetBundle的资源目录下的资源,引用非该目录下的资源!!!
                    Debug.LogError("[error Reference] path:" + allAssetPath[i] + " Reference--->: " + tempPath);
                }
            }
        }
        EditorUtility.ClearProgressBar();

        //找出需要打包的资源;
        for (int i = 0; i < allAssetPath.Count; i++)
        {
            EditorUtility.DisplayProgressBar("Start Search Independence Asset", "Search Progress", (i / allAssetPath.Count));

            if (allAssetPath[i].Contains("Atlas") && Path.GetExtension(allAssetPath[i]) == ".prefab")  //图集特殊处理;
            {
                independenceAsset[allAssetPath[i]] = allAsset[allAssetPath[i]];
                continue;
            }
            if (allAssetPath[i].Contains("Shaders") && Path.GetExtension(allAssetPath[i]) == ".shader")
            {
                allShaderAsset.Add(allAssetPath[i]);
                continue;
            }
            if (allAssetPath[i].Contains("Lua") && Path.GetExtension(allAssetPath[i]) == ".bytes")
            {
                allLuaAsset.Add(allAssetPath[i]);
                continue;
            }
            if (allAsset[allAssetPath[i]].parentDependentAssets.Count == 0 || //没有被依赖的资源;
                allAsset[allAssetPath[i]].parentDependentAssets.Count > 1 ||  //被超过一个资源依赖的资源;
                allAssetPath[i].Contains(FilePathUtil.singleResPath))         //指定要求单独打包的资源;
            {
                independenceAsset[allAssetPath[i]] = allAsset[allAssetPath[i]];
            }
        }
        EditorUtility.ClearProgressBar();

        //设置资源AssetBundle Name;
        for (int i = 0; i < allAssetPath.Count; i++)
        {
            EditorUtility.DisplayProgressBar("Set Asset AssetBundle Name", "AssetBundle Name Setting Progress", (i / allAssetPath.Count));

            AssetImporter importer = AssetImporter.GetAtPath(allAssetPath[i]);
            if (importer != null)
            {
                if (independenceAsset.ContainsKey(allAssetPath[i]))
                {
                    importer.assetBundleName = FilePathUtil.GetAssetBundleFileName(independenceAsset[allAssetPath[i]].type, independenceAsset[allAssetPath[i]].assetName);
                }
                else
                {
                    importer.assetBundleName = null;
                }
                AssetDatabase.ImportAsset(allAssetPath[i]);
            }
        }
        EditorUtility.ClearProgressBar();

        int index = 0;

        //设置Shader AssetBundle Name;
        foreach (string tempPath in allShaderAsset)
        {
            index++;
            EditorUtility.DisplayProgressBar("Set Asset AssetBundle Name", "AssetBundle Name Setting Progress", (index / allShaderAsset.Count));
            AssetImporter importer = AssetImporter.GetAtPath(tempPath);
            if (importer != null)
            {
                importer.assetBundleName = FilePathUtil.GetAssetBundleFileName(AssetType.Shader, "Shader");
                AssetDatabase.ImportAsset(tempPath);
            }
        }
        index = 0;
        //设置Lua文件 AssetBundle Name;
        foreach (string tempPath in allLuaAsset)
        {
            index++;
            EditorUtility.DisplayProgressBar("Set Asset AssetBundle Name", "AssetBundle Name Setting Progress", (index / allShaderAsset.Count));
            AssetImporter importer = AssetImporter.GetAtPath(tempPath);
            if (importer != null)
            {
                importer.assetBundleName = FilePathUtil.GetAssetBundleFileName(AssetType.Lua, "Lua");
                AssetDatabase.ImportAsset(tempPath);
            }
        }
        EditorUtility.ClearProgressBar();

        watch.Stop();
        Debug.LogWarning(string.Format("[AssetDependenciesAnalysis]Asset Dependencies Analysis Spend Time:{0}s", watch.Elapsed.TotalSeconds));

        AssetDatabase.Refresh();
    }