static void BuildSensors() { var outputFolder = Path.Combine(Application.dataPath, "..", "AssetBundles", "Sensors"); Directory.CreateDirectory(outputFolder); var externalSensors = Path.Combine(Application.dataPath, "External", "Sensors"); var directories = new DirectoryInfo(externalSensors).GetDirectories(); var sensorsAsmDefPath = Path.Combine(externalSensors, "Simulator.Sensors.asmdef"); var sensorsAsmDef = JsonUtility.FromJson <AsmdefBody>(File.ReadAllText(sensorsAsmDefPath)); foreach (var directoryInfo in directories) { string bundleGuid = Guid.NewGuid().ToString(); string filename = directoryInfo.Name; try { var prefab = Path.Combine("Assets", "External", "Sensors", filename, $"{filename}.prefab"); if (!File.Exists(Path.Combine(Application.dataPath, "..", prefab))) { Debug.LogError($"Building of {filename} failed: {prefab} not found"); break; } AsmdefBody asmdefContents = new AsmdefBody(); asmdefContents.name = filename; asmdefContents.references = sensorsAsmDef.references; File.WriteAllText(Path.Combine(externalSensors, filename, $"{filename}.asmdef"), JsonUtility.ToJson(asmdefContents)); Manifest manifest = new Manifest { assetName = filename, bundleGuid = bundleGuid, bundleFormat = BundleConfig.SensorBundleFormatVersion, description = "", licenseName = "", authorName = "", authorUrl = "", }; var windowsBuild = new AssetBundleBuild() { assetBundleName = $"{bundleGuid}_sensor_main_windows", assetNames = new[] { prefab }, }; BuildPipeline.BuildAssetBundles( outputFolder, new[] { windowsBuild }, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.StrictMode, UnityEditor.BuildTarget.StandaloneWindows64); if (!File.Exists(Path.Combine(outputFolder, windowsBuild.assetBundleName))) { Debug.LogError($"Failed to find Windows asset bundle of {filename}. Please correct other errors and try again."); return; } var linuxBuild = new AssetBundleBuild() { assetBundleName = $"{bundleGuid}_sensor_main_linux", assetNames = new[] { prefab }, }; BuildPipeline.BuildAssetBundles( outputFolder, new[] { linuxBuild }, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.StrictMode, UnityEditor.BuildTarget.StandaloneLinux64); if (!File.Exists(Path.Combine(outputFolder, linuxBuild.assetBundleName))) { Debug.LogError($"Failed to find Linux asset bundle of {filename}. Please correct other errors and try again."); return; } DirectoryInfo prefabDir = new DirectoryInfo(Path.Combine(externalSensors, filename)); var scripts = prefabDir.GetFiles("*.cs", SearchOption.AllDirectories).Select(script => script.FullName).ToArray(); var outputAssembly = Path.Combine(outputFolder, $"{filename}.dll"); var assemblyBuilder = new AssemblyBuilder(outputAssembly, scripts); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var modules = assemblies.Where(asm => asm.GetName().Name == "UnityEngine" || asm.GetName().Name == "UnityEngine.JSONSerializeModule" || asm.GetName().Name == "UnityEngine.CoreModule" || asm.GetName().Name == "UnityEngine.PhysicsModule").ToArray(); assemblyBuilder.additionalReferences = modules.Select(a => a.Location).ToArray(); assemblyBuilder.buildFinished += delegate(string assemblyPath, CompilerMessage[] compilerMessages) { var errorCount = compilerMessages.Count(m => m.type == CompilerMessageType.Error); var warningCount = compilerMessages.Count(m => m.type == CompilerMessageType.Warning); try { Debug.Log($"Assembly build finished for {assemblyPath}"); if (errorCount != 0) { Debug.Log($"Found {errorCount} errors"); foreach (CompilerMessage message in compilerMessages) { if (message.type == CompilerMessageType.Error) { Debug.LogError(message.message); return; } } } else { var manifestOutput = Path.Combine(outputFolder, "manifest"); File.WriteAllText(manifestOutput, new Serializer().Serialize(manifest)); using (ZipFile archive = ZipFile.Create(Path.Combine(outputFolder, $"sensor_{filename}"))) { archive.BeginUpdate(); archive.Add(new StaticDiskDataSource(Path.Combine(outputFolder, linuxBuild.assetBundleName)), linuxBuild.assetBundleName, CompressionMethod.Stored, true); archive.Add(new StaticDiskDataSource(Path.Combine(outputFolder, windowsBuild.assetBundleName)), windowsBuild.assetBundleName, CompressionMethod.Stored, true); archive.Add(new StaticDiskDataSource(outputAssembly), $"{filename}.dll", CompressionMethod.Stored, true); archive.Add(new StaticDiskDataSource(manifestOutput), "manifest", CompressionMethod.Stored, true); archive.CommitUpdate(); archive.Close(); } } } finally { var di = new DirectoryInfo(outputFolder); SilentDelete(Path.Combine(outputFolder, $"{filename}.dll")); SilentDelete(Path.Combine(outputFolder, $"{filename}.pdb")); SilentDelete(Path.Combine(outputFolder, "manifest")); } }; // Start build of assembly if (!assemblyBuilder.Build()) { Debug.LogErrorFormat("Failed to start build of assembly {0}!", assemblyBuilder.assemblyPath); return; } while (assemblyBuilder.status != AssemblyBuilderStatus.Finished) { } } finally { var di = new DirectoryInfo(outputFolder); var files = di.GetFiles($"{bundleGuid}*"); Array.ForEach(files, f => SilentDelete(f.FullName)); SilentDelete(Path.Combine(outputFolder, "Sensors")); SilentDelete(Path.Combine(outputFolder, "Sensors.manifest")); SilentDelete(Path.Combine(externalSensors, filename, $"{filename}.asmdef")); SilentDelete(Path.Combine(externalSensors, filename, $"{filename}.asmdef.meta")); } } }
public void RunBuild(string outputFolder) { string Thing = Enum.GetName(typeof(BundleConfig.BundleTypes), bundleType); string Things = BundleConfig.pluralOf(bundleType); string thing = Thing.ToLower(); outputFolder = Path.Combine(outputFolder, bundlePath); Directory.CreateDirectory(outputFolder); var currentScenes = new HashSet <Scene>(); var selected = entries.Values.Where(e => e.selected && e.available).ToList(); if (selected.Count == 0) { return; } if (bundleType == BundleConfig.BundleTypes.Environment) { if (!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) { Debug.LogWarning("Cancelling the build."); return; } for (int i = 0; i < EditorSceneManager.loadedSceneCount; i++) { currentScenes.Add(EditorSceneManager.GetSceneAt(i)); } } foreach (var entry in selected) { Manifest manifest; if (bundleType == BundleConfig.BundleTypes.Environment) { manifest = PrepareSceneManifest(entry, currentScenes); } else { manifest = PreparePrefabManifest(entry); } var asmDefPath = Path.Combine(BundleConfig.ExternalBase, Things, $"Simulator.{Things}.asmdef"); AsmdefBody asmDef = null; if (File.Exists(asmDefPath)) { asmDef = JsonUtility.FromJson <AsmdefBody>(File.ReadAllText(asmDefPath)); } var buildArtifacts = new List <(string source, string archiveName)>(); bool mainAssetIsScript = entry.mainAssetFile.EndsWith("." + ScriptExtension); try { Debug.Log($"Building asset: {entry.mainAssetFile} -> " + Path.Combine(outputFolder, $"{thing}_{entry.name}")); if (!File.Exists(Path.Combine(Application.dataPath, "..", entry.mainAssetFile))) { Debug.LogError($"Building of {entry.name} failed: {entry.mainAssetFile} not found"); break; } if (asmDef != null) { AsmdefBody asmdefContents = new AsmdefBody(); asmdefContents.name = entry.name; asmdefContents.references = asmDef.references; var asmDefOut = Path.Combine(sourcePath, entry.name, $"{entry.name}.asmdef"); File.WriteAllText(asmDefOut, JsonUtility.ToJson(asmdefContents)); buildArtifacts.Add((asmDefOut, null)); } AssetDatabase.Refresh(); if (!mainAssetIsScript) { var textureBuild = new AssetBundleBuild() { assetBundleName = $"{manifest.assetGuid}_{thing}_textures", assetNames = AssetDatabase.GetDependencies(entry.mainAssetFile).Where(a => a.EndsWith(".png") || a.EndsWith(".jpg")).ToArray() }; bool buildTextureBundle = textureBuild.assetNames.Length > 0; var windowsBuild = new AssetBundleBuild() { assetBundleName = $"{manifest.assetGuid}_{thing}_main_windows", assetNames = new[] { entry.mainAssetFile }, }; var linuxBuild = new AssetBundleBuild() { assetBundleName = $"{manifest.assetGuid}_{thing}_main_linux", assetNames = new[] { entry.mainAssetFile }, }; var builds = new[]