# Unity のアセンブリの概要

> Understand the fundamentals of how assemblies work in Unity and why using them to organize your scripts is beneficial.

Unity ではスクリプトをアセンブリに整理するために Assembly Definition と Assembly Reference という 2 つの特別なアセットタイプを使用します。

アセンブリとは C# のコードライブラリで、スクリプトによって定義されたコンパイル済みのクラスや構造体を含み、他のアセンブリへの参照も定義します。C# のアセンブリの詳細は、[.NET のアセンブリ](https://learn.microsoft.com/en-us/dotnet/standard/assembly/) を参照してください。

Unity はデフォルトで、ほとんどすべてのゲームスクリプトを `Assembly-CSharp.dll` と呼ばれる事前定義されたアセンブリにコンパイルします。また、いくつかの [より小さく用途に特化した定義済みアセンブリ](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/script-compile-order-folders.md) も作成します。

この方法は小規模なプロジェクトには適していますが、プロジェクトにさらに多くのコードを追加すると、いくつかの欠点があります。

* 1 つのスクリプトを変更するたびに、Unity は他のすべてのスクリプトを再コンパイルする必要があり、反復的なコード変更を行うと全体のコンパイル時間が長くなります。
* どのスクリプトも他のスクリプトで定義された型に直接アクセスできるため、コードのリファクタリングや改善が難しくなる可能性があります。
* すべてのスクリプトは、すべてのプラットフォーム用にコンパイルされます。

アセンブリを定義することで、コードを整理してモジュール性や再利用性を高めることができます。定義したアセンブリ内のスクリプトはデフォルトのアセンブリには追加されず、他のカスタムアセンブリのスクリプトへのアクセスのみを行います。


**Frame:**
![](/api/media?file=/engine/6000.0/media/images/ScriptCompilation.png)

上の図は、プロジェクトのコードを複数のアセンブリに分割する方法を示しています。*Main* は *Stuff* を参照しており、その逆の参照はないため、*Main* のコードを変更しても *Stuff* のコードには影響しません。同様に、*Library* は他のアセンブリに依存していないので、*Library* のコードは別のプロジェクトでより簡単に再利用することができます。

## アセンブリの定義##defining-assemblies

プロジェクトのコードをアセンブリに整理するには、必要なアセンブリごとにフォルダーを作成し、各アセンブリに属するスクリプトを該当するフォルダーに移動します。その後、[Assembly Definition アセットを作成](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/assembly-definitions-creating.md) してアセンブリのプロパティを指定します。

Unity は、Assembly Definition アセットで定義された名前やその他の設定を使用し、Assembly Definition を含むフォルダーのすべてのスクリプトを 1 つのアセンブリにコンパイルします。サブフォルダーに独自の Assembly Definition または Assembly Reference アセットがない限り、アセンブリにはサブフォルダー内のスクリプトが加えられます。

既存のアセンブリに子ではないフォルダーのスクリプトを加えるには、子ではないフォルダーに Assembly Reference アセットを作成し、ターゲットのアセンブリを定義する Assembly Definition アセットを参照するように設定します。例えば、Editor フォルダーがどこにあるかにかかわらず、プロジェクト内のすべての Editor フォルダーのスクリプトを独自のアセンブリにまとめることができます。

Unity はアセンブリを、依存関係に基づいて決められた順序でコンパイルします。コンパイルの実行順序を指定することはできません。

## スクリプトがどのアセンブリに属しているかを調べる##finding-which-assembly-a-script-belongs-to

スクリプトがどのアセンブリに属しているかを調べるには、**Project** ウィンドウでスクリプトファイルを選択し、**Inspector** ウィンドウでプロパティを確認します。アセンブリのファイル名と Assembly Definition は、**Inspector** の **Assembly Information** セクションに表示されます。

## Editor フォルダー##editor-folder

Unity では通常、`Editor` という名前のフォルダーにあるスクリプトは、どこにあっても定義済みの `Assembly-CSharp-Editor` アセンブリにコンパイルされます。ただし、Editor フォルダーの上のフォルダーに Assembly Definition アセットを作成すると、Unity はそれらの Editor スクリプトを定義済みの Editor アセンブリに加えることはなくなります。代わりに Assembly Definition で作成された新しいアセンブリに加えられますが、これは本来属す場所ではない可能性があります。`Editor` フォルダーを管理するには、各 `Editor` フォルダーに Assembly Definition または Assembly Reference アセットを作成して、それらのスクリプトを 1 つまたは複数の Editor アセンブリに配置します。詳細については、[エディターコード用アセンブリの作成](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/assembly-definitions-creating.md#create-editor-assembly) を参照してください。

## 追加リソース##additional-resources

* [アセンブリ定義の作成](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/assembly-definitions-creating.md)
* [アセンブリの参照](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/assembly-definitions-referencing.md)
* [アセンブリのメタデータとコンパイルの詳細](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/metadata.md)
* [Assembly Definition のプロパティ](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/class-assembly-definition-importer.md)
* [Assembly Definition Reference のプロパティ](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/class-assembly-definition-reference-importer.md)
* [Assembly Definition ファイル形式](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/assembly-definition-files/file-format.md)
