版本:2022.3
语言英语
  • C#

AssemblyBuilder.Build

建议更改

成功!

感谢您帮助我们提高Unity文档的质量。虽然我们无法接受所有提交内容,但我们确实会阅读每位用户的建议,并在适用的情况下进行更新。

关闭

提交失败

由于某些原因,您的建议无法提交。请稍后几分钟再尝试一次。感谢您花时间帮助我们提高Unity文档的质量。

关闭

取消

声明

public bool Build();

返回

bool 如果构建已启动,则返回true。如果在构建过程中,编辑器正在编译Assets文件夹中的脚本,则返回false。

描述

开始汇编的构建。

在构建过程中,Unity主窗口右下角的小进度图标会旋转,并且EditorApplication.isCompiling将返回true。

using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Compilation;
using UnityEngine;

public class AssembyBuilderExample { [MenuItem("AssemblyBuilder Example/Build Assembly Async")] public static void BuildAssemblyAsync() { BuildAssembly(false); }

[MenuItem("AssemblyBuilder Example/Build Assembly Sync")] public static void BuildAssemblySync() { BuildAssembly(true); }

static void BuildAssembly(bool wait) { var scripts = new[] { "Temp/MyAssembly/MyScript1.cs", "Temp/MyAssembly/MyScript2.cs" }; var outputAssembly = "Temp/MyAssembly/MyAssembly.dll"; var assemblyProjectPath = "Assets/MyAssembly.dll";

Directory.CreateDirectory("Temp/MyAssembly");

// Create scripts foreach (var scriptPath in scripts) { var scriptName = Path.GetFileNameWithoutExtension(scriptPath); File.WriteAllText(scriptPath, string.Format("using UnityEngine; public class {0} : MonoBehaviour {{ void Start() {{ Debug.Log(\"{0}\"); }} }}", scriptName)); }

var assemblyBuilder = new AssemblyBuilder(outputAssembly, scripts);

// Exclude a reference to the copy of the assembly in the Assets folder, if any. assemblyBuilder.excludeReferences = new string[] { assemblyProjectPath };

// Called on main thread assemblyBuilder.buildStarted += delegate(string assemblyPath) { Debug.LogFormat("Assembly build started for {0}", assemblyPath); };

// Called on main thread 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);

Debug.LogFormat("Assembly build finished for {0}", assemblyPath); Debug.LogFormat("Warnings: {0} - Errors: {0}", errorCount, warningCount);

if(errorCount == 0) { File.Copy(outputAssembly, assemblyProjectPath, true); AssetDatabase.ImportAsset(assemblyProjectPath); } };

// Start build of assembly if(!assemblyBuilder.Build()) { Debug.LogErrorFormat("Failed to start build of assembly {0}!", assemblyBuilder.assemblyPath); return; }

if(wait) { while(assemblyBuilder.status != AssemblyBuilderStatus.Finished) System.Threading.Thread.Sleep(10); } } }