예제 #1
0
        void GenerateFiles(string[] files, CacheFileRefType refType)
        {
            for (int i = 0; i < files.Length; i++)
            {
                CaheFile file = new CaheFile();
                file.filename = files[i];
                file.guid     = AssetDatabase.AssetPathToGUID(file.filename);
                file.type     = GetType(Path.GetExtension(file.filename));

                if (CacheFileAssetType.IsScriptOrConfig(file.type) && refType == CacheFileRefType.NoUse)
                {
                }
                else
                {
                    file.refType = refType;
                }

                if (dict.ContainsKey(file.guid))
                {
                    Debug.Log(refType + "  " + file.filename);
                    continue;
                }
                dict.Add(file.guid, file);
            }
        }
예제 #2
0
        void GenerateDependencies(CaheFile file, CacheFileRefType refType)
        {
            string[] dependencies = AssetDatabase.GetDependencies(file.filename, false);
            for (int i = 0; i < dependencies.Length; i++)
            {
                string   guid = AssetDatabase.AssetPathToGUID(dependencies[i]);
                bool     has  = dict.ContainsKey(guid);
                CaheFile def;
                if (has)
                {
                    def = dict[guid];
                }
                else
                {
                    def          = new CaheFile();
                    def.filename = dependencies[i];
                    def.guid     = guid;
                    def.type     = GetType(Path.GetExtension(def.filename));
                    if (def.refType == CacheFileRefType.None)
                    {
                        if (CacheFileAssetType.IsScriptOrConfig(def.type) && refType == CacheFileRefType.NoUse)
                        {
                        }
                        else
                        {
                            def.refType = refType;
                        }
                    }

                    dict.Add(def.guid, def);
                }

                file.AddDep(def);
                def.AddRef(file);

                if (!has)
                {
                    GenerateDependencies(def, refType);
                }
            }
        }
예제 #3
0
        public void Generate(bool dir = false)
        {
            string[]      files;
            List <string> unsed = new List <string>();

            if (dir)
            {
                var slec = Selection.activeObject;
                if (slec != null)
                {
                    var path = AssetDatabase.GetAssetPath(slec);
                    if (AssetDatabase.IsValidFolder(path))
                    {
                        files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
                                .Where(s => unityExts.Contains(Path.GetExtension(s).ToLower())).ToArray();

                        GenerateFiles(files, CacheFileRefType.CustomDir);
                    }
                    else
                    {
                        //单独选择文件
                        files = new string[1] {
                            path
                        };
                        GenerateFiles(files, CacheFileRefType.CustomDir);
                    }
                }

                // Ref
                list = new List <CaheFile>(dict.Values);

                for (int i = 0; i < list.Count; i++)
                {
                    GenerateDependencies(list[i], CacheFileRefType.Ref);
                }

                //TODO:针对特效目录
                // NoUse Unity
                //files = Directory.GetFiles(checkerNoUsedPath, "*.*", SearchOption.AllDirectories)
                //   .Where(s => !ignoreExts.Contains(Path.GetExtension(s).ToLower()) && !ignoreFiles.Contains(Path.GetFileName(s).ToLower())).ToArray();

                //for (int i = 0; i < files.Length; i++)
                //{
                //    string guid = AssetDatabase.AssetPathToGUID(files[i]);
                //    if (dict.ContainsKey(guid))
                //    {
                //        continue;
                //    }

                //    CaheFile file = new CaheFile();
                //    file.filename = files[i];
                //    file.guid = guid;
                //    file.type = GetType(Path.GetExtension(file.filename));
                //    if (!CacheFileAssetType.IsScriptOrConfig(file.type))
                //    {
                //        file.refType = CacheFileRefType.NoUse;
                //    }
                //    dict.Add(file.guid, file);
                //    unsed.Add(file.filename);
                //}
            }
            else
            {
                string mresourceRoot = "Assets/Game/MResources";

                // MResources Unity
                if (Directory.Exists(mresourceRoot))
                {
                    files = Directory.GetFiles(mresourceRoot, "*.*", SearchOption.AllDirectories)
                            .Where(s => unityExts.Contains(Path.GetExtension(s).ToLower())).ToArray();

                    GenerateFiles(files, CacheFileRefType.MResource);
                }

                // Resource Unity
                string[] resourcesDirs = Directory.GetDirectories("Assets", "Resources", SearchOption.AllDirectories);
                for (int i = 0; i < resourcesDirs.Length; i++)
                {
                    files = Directory.GetFiles(resourcesDirs[i], "*.*", SearchOption.AllDirectories)
                            .Where(s => unityExts.Contains(Path.GetExtension(s).ToLower())).ToArray();
                    GenerateFiles(files, CacheFileRefType.Resource);
                }

                // Editor
                string[] editorDirs = Directory.GetDirectories("Assets", "Editor", SearchOption.AllDirectories);
                for (int i = 0; i < editorDirs.Length; i++)
                {
                    files = Directory.GetFiles(editorDirs[i], "*.*", SearchOption.AllDirectories)
                            .Where(s => Path.GetExtension(s).ToLower() != ".meta" && !ignoreFiles.Contains(Path.GetFileName(s).ToLower())).ToArray();


                    GenerateFiles(files, CacheFileRefType.Editor);
                }

                // Ref
                list = new List <CaheFile>(dict.Values);

                for (int i = 0; i < list.Count; i++)
                {
                    GenerateDependencies(list[i], CacheFileRefType.Ref);
                }

                // NoUse Unity
                //files = Directory.GetFiles("Assets", "*.*", SearchOption.AllDirectories)
                //    .Where(s => unityExts.Contains(Path.GetExtension(s).ToLower()) && !ignoreFiles.Contains(Path.GetFileName(s).ToLower())).ToArray();

                //for (int i = 0; i < files.Length; i++)
                //{
                //    string guid = AssetDatabase.AssetPathToGUID(files[i]);
                //    if (dict.ContainsKey(guid))
                //    {
                //        continue;
                //    }

                //    CaheFile file = new CaheFile();
                //    file.filename = files[i];
                //    file.guid = guid;
                //    file.type = GetType(Path.GetExtension(file.filename));
                //    if (!CacheFileAssetType.IsScriptOrConfig(file.type))
                //    {
                //        file.refType = CacheFileRefType.NoUse;
                //    }
                //    dict.Add(file.guid, file);

                //    GenerateDependencies(file, CacheFileRefType.NoUse);
                //}

                // Other
                files = Directory.GetFiles("Assets", "*.*", SearchOption.AllDirectories)
                        .Where(s => !ignoreExts.Contains(Path.GetExtension(s).ToLower()) && s.IndexOf("Assets/StreamingAssets") == -1 && !ignoreFiles.Contains(Path.GetFileName(s).ToLower())).ToArray();

                for (int i = 0; i < files.Length; i++)
                {
                    string guid = AssetDatabase.AssetPathToGUID(files[i]);
                    if (dict.ContainsKey(guid))
                    {
                        continue;
                    }

                    CaheFile file = new CaheFile();
                    file.filename = files[i];
                    file.guid     = guid;
                    file.type     = GetType(Path.GetExtension(file.filename));
                    if (file.filename.StartsWith(mresourceRoot))
                    {
                        file.refType = CacheFileRefType.MResource;
                    }
                    else if (file.filename.IndexOf("/Resources/") != -1)
                    {
                        file.refType = CacheFileRefType.Resource;
                    }
                    else
                    {
                        if (!CacheFileAssetType.IsScriptOrConfig(file.type))
                        {
                            file.refType = CacheFileRefType.NoUse;
                        }
                    }
                    dict.Add(file.guid, file);
                }
            }

            unsed.Sort();
            StringBuilder sb = new StringBuilder();

            foreach (var s in unsed)
            {
                sb.AppendLine(s);
            }
            //存储未使用资源
            WirteCfgFile(Application.dataPath + "/JGUnusedCheck.txt", sb.ToString());

            list = new List <CaheFile>(dict.Values);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].ToGUIDList();
            }

            string json = JsonUtility.ToJson(this, true);

            if (!Directory.Exists(outRoot))
            {
                Directory.CreateDirectory(outRoot);
            }

            File.WriteAllText(outFile_data, json);
            File.WriteAllText(outFile_data_js, "var guidData = " + json);

            if (!File.Exists(outFile_index))
            {
                foreach (string filename in copys)
                {
                    File.Copy(guidEditorWebRoot + filename, outRoot + filename.Replace(".javascript", ".js"));
                }
            }

            string url = "file:///" + Path.GetFullPath(outFile_index);

            Debug.Log(url);
            Application.OpenURL(url);
        }