Ejemplo n.º 1
0
        static public void Build()
        {
            string outputPath = Path.Combine(AssetBundlesOutputPath, AssetBundleUtility.GetPlatformName());

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

            //List<KeyValuePair<string, string>> assetInfos = new List<KeyValuePair<string, string>>();
            //assetInfos.Add(new KeyValuePair<string, string>(AssetBundleUtility.GetPlatformName(), AssetBundleUtility.GetPlatformName() + AssetBundleUtility.AssetBundleExtension));

            List <string> paths = new List <string>();

            paths.Add(AssetBundleUtility.GetPlatformName() + AssetBundleUtility.AssetBundleExtension);

            List <AssetBundleBuild> builds = new List <AssetBundleBuild>();

            //string[] lookFor = new string[] { AssetBundleUtility.AssetBundleResourcesPath };
            //string[] guids = AssetDatabase.FindAssets("", lookFor);
            //foreach (var guid in guids)
            //{
            //    string assetPath = AssetDatabase.GUIDToAssetPath(guid);
            //    if (!AssetDatabase.IsValidFolder(assetPath))//排除文件夹
            //    {
            //        string assetBundleName = AssetPathToAssetBundleName(assetPath).ToLower();
            //        paths.Add(assetBundleName);

            //        AssetBundleBuild build = new AssetBundleBuild();
            //        build.assetBundleName = assetBundleName;
            //        build.assetNames = new string[] { assetPath };
            //        builds.Add(build);
            //        //Debug.Log(build.assetBundleName);
            //        //Debug.Log(assetPath);
            //    }
            //}

            DirectoryInfo dirInfo = new DirectoryInfo(AssetBundleUtility.AssetBundleResourcesPath);

            FileInfo[] fileInfos = dirInfo.GetFiles("*", SearchOption.AllDirectories);
            foreach (var item in fileInfos)
            {
                if (Path.GetExtension(item.Name) != ".meta")
                {
                    //AssetImporter ai = AssetImporter.GetAtPath(item.FullName);
                    //if (string.IsNullOrEmpty(ai.assetBundleName))
                    //{
                    //    ai.assetBundleName = FilePathToAssetBundleName(item.FullName).ToLower();
                    //}
                    //else
                    //{
                    //}
                    string assetBundleName = FilePathToAssetBundleName(item.FullName).ToLower() + AssetBundleUtility.AssetBundleExtension;
                    paths.Add(assetBundleName);

                    AssetBundleBuild build = new AssetBundleBuild();
                    build.assetBundleName = assetBundleName;
                    build.assetNames      = new string[] { FilePathToAssetName(item.FullName) };
                    builds.Add(build);
                }
            }
            BuildPipeline.BuildAssetBundles(outputPath, builds.ToArray(), BuildAssetBundleOptions.DisableWriteTypeTree | BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);

            string manifestFilePath       = Path.Combine(outputPath, AssetBundleUtility.GetPlatformName());
            string manifestFilePathRename = manifestFilePath + AssetBundleUtility.AssetBundleExtension;

            if (File.Exists(manifestFilePathRename))
            {
                File.Delete(manifestFilePathRename);
            }
            File.Move(manifestFilePath, manifestFilePathRename);

            StringBuilder sb = new StringBuilder(DateTime.Now.ToString("yyyyMMddHHmmss"));

            for (int i = 0; i < paths.Count; i++)
            {
                string     path = paths[i];
                FileStream fs   = new FileStream(Path.Combine(outputPath, path), FileMode.Open);
                sb.AppendFormat("\n{0}\t{1}\t{2}", path, AssetBundleUtility.GetMD5HashFromFileStream(fs), fs.Length);
                fs.Close();
                EditorUtility.DisplayProgressBar("Compute MD5", string.Format("{0}/{1}  {2}", i + 1, paths.Count, path), (i + 1) / (float)paths.Count);
            }
            File.WriteAllBytes(Path.Combine(outputPath, AssetBundleUtility.VersionFileName), Encoding.UTF8.GetBytes(sb.ToString()));
            EditorUtility.ClearProgressBar();

            if (!EditorUtility.DisplayDialog("Build AssetBundles", "Build Success!", "OK", "Open Contain Folder"))
            {
                System.Diagnostics.Process.Start("explorer.exe", "/select," + Path.GetFullPath(outputPath));
            }
        }