Esempio n. 1
0
    public static void CheckResourceReference()
    {
        var filePaths = EditorFileUtility.FilterDirectoryIgnoreExt(Application.dataPath + "/Resources",
                                                                   IgnoreFileExtensionList);

        var rootPath = Path.GetDirectoryName(Application.dataPath);

        rootPath = rootPath.Replace("\\", "/") + "/";

        if (filePaths != null)
        {
            Queue <string> assetQueue = new Queue <string>();
            foreach (var filePath in filePaths)
            {
                var path = filePath.Replace("\\", "/");
                path = path.Replace(rootPath, "");
                assetQueue.Enqueue(path);
            }

            while (assetQueue.Count > 0)
            {
                var path = assetQueue.Dequeue();
                if (GameAssets.ContainsKey(path))
                {
                    continue;
                }

                GetAssetDependences(assetQueue, path);
            }

            Dictionary <string, List <string> > sameAssets = new Dictionary <string, List <string> >();
            foreach (var gameAsset in GameAssets)
            {
                var md5 =
                    EditorFileUtility.GetMD5HashFromFile(Path.GetDirectoryName(Application.dataPath) + "/" +
                                                         gameAsset.Value.Path);
                if (!sameAssets.ContainsKey(md5))
                {
                    sameAssets.Add(md5, new List <string>());
                    sameAssets[md5].Add(gameAsset.Value.Path);
                }
                else
                {
                    sameAssets[md5].Add(gameAsset.Value.Path);
                }
            }

            StringBuilder sb = new StringBuilder();
            foreach (var asset in sameAssets)
            {
                if (asset.Value.Count > 1)
                {
                    sb.AppendLine("MD5: " + asset.Key);
                    foreach (var p in asset.Value)
                    {
                        sb.AppendLine("\t资源路径:" + p);
                        foreach (var gameAsset in GameAssets)
                        {
                            foreach (var cd in gameAsset.Value.ChildDependences)
                            {
                                if (cd == p)
                                {
                                    sb.AppendLine("\t\t引用到它的资源:" + gameAsset.Key);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            sb.AppendLine();

            sb.AppendLine("Resources目录下未被直接引用的资源(可能是代码中引用)");
            foreach (var gameAsset in GameAssets)
            {
                var hasRefer = false;
                foreach (var gameAsset1 in GameAssets)
                {
                    var isRefer = false;
                    foreach (var cd in gameAsset1.Value.ChildDependences)
                    {
                        if (cd == gameAsset.Value.Path)
                        {
                            isRefer = true;
                            break;
                        }
                    }
                    if (isRefer)
                    {
                        hasRefer = true;
                        break;
                    }
                }

                if (!hasRefer)
                {
                    sb.AppendLine("\t资源路径:" + gameAsset.Value.Path);
                }
            }

            File.WriteAllText(Path.GetDirectoryName(Application.dataPath) + "/CheckResourceReference.txt", sb.ToString());
            WindowsOSUtility.ExploreFile(Path.GetDirectoryName(Application.dataPath));
        }
    }
Esempio n. 2
0
    public static void LoadAllAssets()
    {
        GameAssets.Clear();
        AssetWithoutExtMap.Clear();
        HasBuildAssetBundleMap.Clear();

        var filePaths = EditorFileUtility.FilterDirectoryIgnoreExt(Application.dataPath + "/Resources", IgnoreFileExtensionList);

        var rootPath = Path.GetDirectoryName(Application.dataPath);

        rootPath = rootPath.Replace("\\", "/") + "/";

        if (filePaths != null)
        {
            Queue <string> assetQueue = new Queue <string>();
            foreach (var filePath in filePaths)
            {
                var path = filePath.Replace("\\", "/");
                path = path.Replace(rootPath, "");

                bool canAdd = true;
                foreach (var ignoreFile in IgnoreFileList)
                {
                    if (path.StartsWith(ignoreFile))
                    {
                        canAdd = false;
                    }
                }

                if (canAdd)
                {
                    assetQueue.Enqueue(path);
                }
            }

            while (assetQueue.Count > 0)
            {
                var path = assetQueue.Dequeue();
                if (GameAssets.ContainsKey(path))
                {
                    continue;
                }

                GetAssetDependences(assetQueue, path);
            }

            ReadBuildConfig();
            ClearExportDir();
            BuildAssetBundles();
            ExportAssetDependences();
            PackScripts();

            string assetName = "";
            try
            {
                foreach (var gameAsset in GameAssets)
                {
                    assetName = gameAsset.Key;
                    CheckAssetsDenpendences(assetName);
                }
            }
            catch (Exception e)
            {
                if (e.ToString().StartsWith("System.StackOverflowException"))
                {
                    Debug.LogError("检查资源依赖关系出现栈溢出问题,可能是资源存在环形依赖问题,资源名称是:" + assetName + ",请修复问题并重新打包:" + e);
                }
                else
                {
                    Debug.LogError("检查资源依赖关系出现错误,请修复问题并重新打包:" + e);
                }
            }
        }

        var exportRootPath = Path.GetDirectoryName(Application.dataPath);

        EditorFileUtility.DeleteDirectory(exportRootPath + ExportDir + "/Temp");
    }