private static void ClearPreferences(AssetBundleWindow2 thisWindow) { thisWindow.assetBundleFolderLocation = "/BundleCreator/Data/AssetBundles/"; thisWindow.exportLocation = "/../AssetBundles/"; thisWindow.bundleFileExtension = ".unity3d"; thisWindow.optionalSettings = false; thisWindow.buildTarget = BuildTarget.WebPlayer; //BuildAssetBundleOptions thisWindow.buildAssetBundleOptions = true; thisWindow.collectDependencies = true; thisWindow.completeAssets = true; thisWindow.disableWriteTypeTree = false; thisWindow.deterministicAssetBundle = false; thisWindow.uncompressedAssetBundle = false; thisWindow.bundleVersions.Clear(); thisWindow.bundleContents.Clear(); thisWindow.bundleFileSizes.Clear(); }
private static void WriteEditorPrefs(AssetBundleWindow2 thisWindow) { //save editor prefs //cws is for "cry wolf studios" EditorPrefs.SetString("cws_assetFolder", thisWindow.assetBundleFolderLocation); EditorPrefs.SetString("cws_exportFolder", thisWindow.exportLocation); EditorPrefs.SetString("cws_bundleExtension", thisWindow.bundleFileExtension); EditorPrefs.SetBool("cws_optionalSettings", thisWindow.optionalSettings); EditorPrefs.SetInt("cws_buildTarget", (int)thisWindow.buildTarget); EditorPrefs.SetBool("cws_buildAssetBundleOptions", thisWindow.buildAssetBundleOptions); EditorPrefs.SetBool("cws_collectDependencies", thisWindow.collectDependencies); EditorPrefs.SetBool("cws_completeAssets", thisWindow.completeAssets); EditorPrefs.SetBool("cws_disableWriteTypeTree", thisWindow.disableWriteTypeTree); EditorPrefs.SetBool("cws_deterministicAssetBundle", thisWindow.deterministicAssetBundle); EditorPrefs.SetBool("cws_uncompressedAssetBundle", thisWindow.uncompressedAssetBundle); EditorPrefs.SetBool("cws_setLowerCaseName", thisWindow.setLowerCaseName); //If you want the export folder at runtime (for asset bundle loading in editor mode) PlayerPrefs.SetString("cws_exportFolder", thisWindow.exportLocation); }
/// <summary> /// Helper function to build an asset bundle /// This will iterate through all the settings of the AssetBundleWindow and set them accordingly /// </summary> /// <param name="thisWindow"></param> /// <param name="toInclude"></param> /// <param name="bundlePath"></param> public static bool BuildAssetBundle(AssetBundleWindow2 thisWindow, List<Object> toInclude, string bundlePath) { BuildAssetBundleOptions buildAssetOptions = 0; if(thisWindow.buildAssetBundleOptions) { if(thisWindow.collectDependencies) { if(buildAssetOptions == 0) { buildAssetOptions = BuildAssetBundleOptions.CollectDependencies; } else { buildAssetOptions |= BuildAssetBundleOptions.CollectDependencies; } } if(thisWindow.completeAssets) { if(buildAssetOptions == 0) { buildAssetOptions = BuildAssetBundleOptions.CompleteAssets; } else { buildAssetOptions |= BuildAssetBundleOptions.CompleteAssets; } } if(thisWindow.disableWriteTypeTree) { if(buildAssetOptions == 0) { buildAssetOptions = BuildAssetBundleOptions.DisableWriteTypeTree; } else { buildAssetOptions |= BuildAssetBundleOptions.DisableWriteTypeTree; } } if(thisWindow.deterministicAssetBundle) { if(buildAssetOptions == 0) { buildAssetOptions = BuildAssetBundleOptions.DeterministicAssetBundle; } else { buildAssetOptions |= BuildAssetBundleOptions.DeterministicAssetBundle; } } if(thisWindow.uncompressedAssetBundle) { if(buildAssetOptions == 0) { buildAssetOptions = BuildAssetBundleOptions.UncompressedAssetBundle; } else { buildAssetOptions |= BuildAssetBundleOptions.UncompressedAssetBundle; } } } //If none of "BuildAssetBundleOptions" or "Optional Settings" are set, then create without dependency tracking if (!thisWindow.buildAssetBundleOptions && !thisWindow.optionalSettings) { if (!BuildPipeline.BuildAssetBundle(null, toInclude.ToArray(), bundlePath)) { return false; } } else { if (buildAssetOptions == 0) //If it's still zero, set default values { Debug.LogWarning("No BuildAssetBundleOptions are set, reverting back to dependency tracking. If you want no dependency tracking uncheck the 'BuildAssetBundleOptions' && 'Optional Settings' toggles all together"); buildAssetOptions = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets; thisWindow.buildAssetBundleOptions = true; thisWindow.collectDependencies = true; thisWindow.completeAssets = true; } if (thisWindow.optionalSettings) //Support for different build targets { if (!BuildPipeline.BuildAssetBundle(null, toInclude.ToArray(), bundlePath, buildAssetOptions, thisWindow.buildTarget)) { return false; } } else { if (!BuildPipeline.BuildAssetBundle(null, toInclude.ToArray(), bundlePath, buildAssetOptions)) { return false; } } } return true; }
private static void ReadEditorPrefs(AssetBundleWindow2 thisWindow) { //load editor prefs //cws is for "cry wolf studios" if (EditorPrefs.HasKey("cws_assetFolder")) { thisWindow.assetBundleFolderLocation = EditorPrefs.GetString("cws_assetFolder"); } if (EditorPrefs.HasKey("cws_exportFolder")) { thisWindow.exportLocation = EditorPrefs.GetString("cws_exportFolder"); } if (EditorPrefs.HasKey("cws_bundleExtension")) { thisWindow.bundleFileExtension = EditorPrefs.GetString("cws_bundleExtension"); } if (EditorPrefs.HasKey("cws_optionalSettings")) { thisWindow.optionalSettings = EditorPrefs.GetBool("cws_optionalSettings"); } if (EditorPrefs.HasKey("cws_buildTarget")) { thisWindow.buildTarget = (BuildTarget)EditorPrefs.GetInt("cws_buildTarget"); } if (EditorPrefs.HasKey("cws_buildAssetBundleOptions")) { thisWindow.buildAssetBundleOptions = EditorPrefs.GetBool("cws_buildAssetBundleOptions"); } if (EditorPrefs.HasKey("cws_collectDependencies")) { thisWindow.collectDependencies = EditorPrefs.GetBool("cws_collectDependencies"); } if (EditorPrefs.HasKey("cws_completeAssets")) { thisWindow.completeAssets = EditorPrefs.GetBool("cws_completeAssets"); } if (EditorPrefs.HasKey("cws_disableWriteTypeTree")) { thisWindow.disableWriteTypeTree = EditorPrefs.GetBool("cws_disableWriteTypeTree"); } if (EditorPrefs.HasKey("cws_deterministicAssetBundle")) { thisWindow.deterministicAssetBundle = EditorPrefs.GetBool("cws_deterministicAssetBundle"); } if (EditorPrefs.HasKey("cws_uncompressedAssetBundle")) { thisWindow.uncompressedAssetBundle = EditorPrefs.GetBool("cws_uncompressedAssetBundle"); } if (EditorPrefs.HasKey("cws_setLowerCaseName")) { thisWindow.setLowerCaseName = EditorPrefs.GetBool("cws_setLowerCaseName"); } }
public static bool ExportAssetBundleFolders(AssetBundleWindow2 thisWindow) { //To keep track of the versions with the control file Dictionary<string, int> bundleVersions = new Dictionary<string, int>(); //A list of all the assets in each bundle //This will be saved into the file bundleContents.txt; Dictionary<string, List<string>> bundleContents = new Dictionary<string, List<string>>(); //The AssetBundle folder string path = Application.dataPath + thisWindow.exportLocation; //The folder location in the editor string assetBundleFolderLocation = thisWindow.assetBundleFolderLocation; //Create directory if it does not exist if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //Read and parse the control file ReadBundleControlFile(path + bundleControlFileName, bundleVersions); //Read and parse the contents file ReadBundleContentsFile(path + bundleContentsFileName, bundleContents); //Check if the directory exist if (!Directory.Exists(Application.dataPath + assetBundleFolderLocation)) { Debug.LogError("Specified 'AssetBundles folder' does not exist! Open Assets->Bundle Creator->Asset Bundle Creator to correct it"); return false; } int createdBundles = 0; string[] directoryNames = Directory.GetDirectories(Application.dataPath + assetBundleFolderLocation); foreach (string folderPath in directoryNames) { //Generate the name of this asset bundle DirectoryInfo dirInfo = new DirectoryInfo(folderPath); //if the user has specified a build target, add a prefix to the bundle name string bundleName = ""; string directoryName = dirInfo.Name; if(thisWindow.setLowerCaseName) { directoryName = directoryName.ToLower(); } if (!thisWindow.optionalSettings) { bundleName = directoryName + thisWindow.bundleFileExtension; } else { bundleName = thisWindow.buildTarget.ToString().ToLower() + "_" + directoryName + thisWindow.bundleFileExtension; } List<Object> toInclude = new List<Object>(); string[] assetsInFolder = Directory.GetFiles(folderPath); //To save in the contents file(information) List<string> assetListInFolder = new List<string>(); foreach (string asset in assetsInFolder) { string thisFileName = Path.GetFileName(asset); if (asset.EndsWith(".prefab")) { string internalFilePath = "Assets" + assetBundleFolderLocation + dirInfo.Name + "/" + thisFileName; GameObject prefab = (GameObject)Resources.LoadAssetAtPath(internalFilePath, typeof(GameObject)); toInclude.Add((Object)prefab); assetListInFolder.Add(thisFileName); } else if (!asset.EndsWith(".meta")) { //toInclude.AddRange(AssetDatabase.LoadAllAssetsAtPath(assetBundleFolderLocation + dirInfo.Name + "/" + thisFileName)); string internalFilePath = "Assets" + assetBundleFolderLocation + dirInfo.Name + "/" + thisFileName; toInclude.Add((Object)Resources.LoadAssetAtPath(internalFilePath, typeof(Object))); assetListInFolder.Add(thisFileName); } } //Build only if there are any files in this folder if (toInclude.Count > 0) { //Check if the bundle already have been created if (bundleContents.ContainsKey(bundleName)) { bundleContents.Remove(bundleName); } //Add to the contents text file bundleContents.Add(bundleName, assetListInFolder); Debug.Log("Building bundle:" + bundleName); if (!BuildAssetBundle(thisWindow, toInclude, path + bundleName)) { return false; //It failed, abort everything } createdBundles++; //Checks to save the version numbers int versionNumber = -1; bundleVersions.TryGetValue(bundleName, out versionNumber); if (versionNumber == -1) { versionNumber = 1; } else { versionNumber++; bundleVersions.Remove(bundleName); } bundleVersions.Add(bundleName, versionNumber); } toInclude.Clear(); } WriteBundleControlFile(path + bundleControlFileName, bundleVersions, path); WriteBundleContentsFile(path + bundleContentsFileName, bundleContents, path); bundleVersions.Clear(); foreach (KeyValuePair<string, List<string>> pair in bundleContents) { pair.Value.Clear(); } bundleContents.Clear(); Debug.Log("***Successfully Created " + createdBundles + " AssetBundles!***"); return true; }