Beispiel #1
0
 public static void BuildAssetBundle()
 {
     if (File.Exists("Assets/Editor/Util/AssetBundle/AssetBundleSetting.asset"))
     {
         AssetBundleSetting assetBundleSetting = AssetDatabase.LoadAssetAtPath <AssetBundleSetting>("Assets/Editor/Util/AssetBundle/AssetBundleSetting.asset");
         assetBundleSetting.buildId++;
         if (string.IsNullOrEmpty(assetBundleSetting.outputPath))
         {
             assetBundleSetting.outputPath = "Assets/AssetBundles";
         }
         AssetBundleUtil.BuildAssetBundle(assetBundleSetting);
     }
 }
Beispiel #2
0
 public static void TagDirectoryRule()
 {
     if (File.Exists("Assets/Editor/Util/AssetBundle/AssetBundleSetting.asset"))
     {
         AssetBundleSetting assetBundleSetting = AssetDatabase.LoadAssetAtPath <AssetBundleSetting>("Assets/Editor/Util/AssetBundle/AssetBundleSetting.asset");
         if (assetBundleSetting.assetBundleRuleList == null)
         {
             assetBundleSetting.assetBundleRuleList = new List <AssetBundleRule>();
         }
         assetBundleSetting.assetBundleRuleList.Add(AssetBundleUtil.TagDirectoryRule());
         assetBundleSetting.assetBundleDataList = AssetBundleUtil.BuildAssetBundleData(assetBundleSetting.assetBundleRuleList);
     }
     else
     {
         AssetBundleSetting assetBundleSetting = ScriptableObject.CreateInstance <AssetBundleSetting>();
         assetBundleSetting.assetBundleRuleList = new List <AssetBundleRule>();
         assetBundleSetting.assetBundleRuleList.Add(AssetBundleUtil.TagDirectoryRule());
         assetBundleSetting.assetBundleDataList = AssetBundleUtil.BuildAssetBundleData(assetBundleSetting.assetBundleRuleList);
         AssetDatabase.CreateAsset(assetBundleSetting, "Assets/Editor/Util/AssetBundle/AssetBundleSetting.asset");
         AssetDatabase.Refresh();
     }
 }
Beispiel #3
0
        public static void BuildAssetBundle(AssetBundleSetting assetBundleSetting)
        {
            Dictionary <string, AssetBundleData>     assetBundleDataDict     = new Dictionary <string, AssetBundleData>();
            Dictionary <string, AssetBundleRuleType> assetBundleRuleTypeDict = new Dictionary <string, AssetBundleRuleType>();
            string path = DirectoryUtil.GetDirectoryPath($"{assetBundleSetting.outputPath}/{GetPlatformForAssetBundle(EditorUserBuildSettings.activeBuildTarget)}");

            foreach (DirectoryInfo item in DirectoryUtil.GetDirectorys(new DirectoryInfo(path), new List <DirectoryInfo>()))
            {
                item.Delete();
            }
            foreach (FileInfo item in FileUtil.GetFiles(new DirectoryInfo(path), new List <FileInfo>()))
            {
                item.Delete();
            }
            List <AssetBundleBuild> assetBundleBuildList = new List <AssetBundleBuild>();

            foreach (AssetBundleRule item in assetBundleSetting.assetBundleRuleList)
            {
                if (item.assetBundleRuleType == AssetBundleRuleType.File)
                {
                    FileInfo[] fileInfos = FileUtil.GetFiles(new DirectoryInfo(item.path), new List <FileInfo>());
                    if (fileInfos.Length == 0)
                    {
                        continue;
                    }
                    List <FileInfo> fileInfoList = (from fileInfo in fileInfos where !string.IsNullOrEmpty(Path.GetExtension(fileInfo.Name)) && Path.GetExtension(fileInfo.Name) != ".meta" select fileInfo).ToList();
                    foreach (FileInfo fileInfo in fileInfoList)
                    {
                        assetBundleRuleTypeDict.Add(fileInfo.FullName.Substring(fileInfo.FullName.IndexOf("Assets")).Replace("\\", "/"), AssetBundleRuleType.File);
                    }
                }
                if (item.assetBundleRuleType == AssetBundleRuleType.Directory)
                {
                    DirectoryInfo[] directoryInfos = DirectoryUtil.GetDirectorys(new DirectoryInfo(item.path), new List <DirectoryInfo>()
                    {
                        new DirectoryInfo(item.path)
                    });
                    if (directoryInfos.Length == 0)
                    {
                        continue;
                    }
                    foreach (DirectoryInfo directoryInfo in directoryInfos)
                    {
                        foreach (FileInfo fileInfo in directoryInfo.GetFiles())
                        {
                            assetBundleRuleTypeDict.Add(fileInfo.FullName.Substring(fileInfo.FullName.IndexOf("Assets")).Replace("\\", "/"), AssetBundleRuleType.Directory);
                        }
                    }
                }
            }
            foreach (AssetBundleData item in assetBundleSetting.assetBundleDataList)
            {
                assetBundleBuildList.Add(new AssetBundleBuild()
                {
                    assetBundleName = item.assetBundleName,
                    assetNames      = item.assetNames,
                });
            }
            AssetBundleManifest assetBundleManifest = BuildPipeline.BuildAssetBundles(path, assetBundleBuildList.ToArray(), BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);

            foreach (AssetBundleData item in assetBundleSetting.assetBundleDataList)
            {
                item.assetBundleHash = assetBundleManifest.GetAssetBundleHash(item.assetBundleName).ToString();
                BuildPipeline.GetCRCForAssetBundle($"{path}/{item.assetBundleName}", out item.assetBundleCRC);
                item.fileSize = FileUtil.GetFileSize($"{path}/{item.assetBundleName}");
                assetBundleDataDict.Add(Path.GetFileNameWithoutExtension(item.assetBundleName), item);
            }
            AssetBundleConfig assetBundleConfig = new AssetBundleConfig(assetBundleSetting.buildId, assetBundleDataDict, assetBundleRuleTypeDict);

            FileUtil.SaveAsset(path, "AssetBundleConfig.json", JsonUtil.ToJson(assetBundleConfig));
            if (assetBundleSetting.isCopyStreamingAssets)
            {
                string copyPath = DirectoryUtil.GetDirectoryPath(PathUtil.GetPath(PathType.StreamingAssetsPath, "Res", GetPlatformForAssetBundle(EditorUserBuildSettings.activeBuildTarget)));
                foreach (DirectoryInfo item in DirectoryUtil.GetDirectorys(new DirectoryInfo(copyPath), new List <DirectoryInfo>()))
                {
                    item.Delete();
                }
                foreach (FileInfo item in FileUtil.GetFiles(new DirectoryInfo(copyPath), new List <FileInfo>()))
                {
                    item.Delete();
                }
                foreach (FileInfo item in FileUtil.GetFiles(new DirectoryInfo(path), new List <FileInfo>()))
                {
                    if (Path.GetExtension(item.Name) == ".meta")
                    {
                        continue;
                    }
                    File.Copy(item.FullName, $"{PathUtil.GetPath(PathType.StreamingAssetsPath, "Res", GetPlatformForAssetBundle(EditorUserBuildSettings.activeBuildTarget))}/{item.Name}");
                }
            }
            AssetDatabase.Refresh();
        }