プロジェクトが大きくなり、スクリプト数が増えるにつれてスクリプトクラス名が衝突する可能性が増大します。これはゲームの異なるパーツを別々に作業し、最終的に同じプロジェクトに統合する場合に当てはまります。例えば一人のプログラマがメインプレイヤーの制御コードを作成し、もう一人が敵キャラクターの制御コードを作成したとします。二人ともメインのスクリプトクラスを Controller と命名してプロジェクト統合したときに名前が衝突します。
ある一定の範囲までは、ネーミングルールを導入したり、衝突する都度に名前変更することで問題は避けられます(例.先の例ならばクラスは PlayerController および EnemyController という名前をつける、等)。しかし衝突するクラスが複数あったり、変数がその名前をつけて宣言されている場合は面倒です。各々のクラス名の使用箇所をひとつひとつ変更しないとコンパイルができません。
C# 言語には Namespace (名前空間) と呼ばれる機能を提供していて、堅牢な方法でこの問題が解決できます。名前空間はクラス名にプレフィックスをつけて使用されるクラスの集合です。詳細は、Microsoft の 名前空間 に関するドキュメントを参照してください。
以下の例では、クラス Controller1 と Controller2 は Enemy という名前空間のメンバーです。
namespace Enemy {
public class Controller1 : MonoBehaviour {
...
}
public class Controller2 : MonoBehaviour {
...
}
}
コードの中で、これらのクラスは各々 Enemy.Controller1
と Enemy.Controller2
との名前で使用されてます。これは名前空間の宣言は既存クラスの宣言の周りにブラケットをつけることができるため、クラスの名前変更をするよりよい方法です(すなわち、個別のクラスの名前をすべて変更する必要がないため)。さらに異なるソースファイルにあったとしても複数のブラケットの付いた名前空間をいつでもつけることができます。
名前空間のプレフィックスを繰り返し入力することを避けるにはファイルの先頭で using を追加します。
using Enemy;
この行により Controller1 と Controller2 が見つかったところでは、各々 Enemy.Controller1 と Enemy.Controller2 にと解釈されます。もしスクリプトが別の名前空間から同じ名前のクラスを参照する必要がある場合(例えば Player )もプレフィックスを使い続けることができます。ただし、衝突するクラス名を含む二つの名前空間が using ディレクティブを使用して同時にインポートされた場合はコンパイラでエラーが発生します。
Note: Unity has a specific limitation relating to namespaces and MonoBehaviour or ScriptableObject classes. If your file contains a definition for a MonoBehaviour or ScriptableObject class, you cannot use multiple namespaces within that file.
Unity gives the following warning in the console:
Class MyClass can not exist in multiple namespaces in the same file, even if one is excluded with preprocessor directives. Please move these to separate files if this is the case.
This means if you have a file which defines a MonoBehaviour in one namespace, and other classes in a different namespace within the same file, Unity will not recognize the MonoBehaviour class and you will not be able to use it on GameObjects. This limitation was introduced in Unity 2020.1 to improve import and compilation speed, and therefore some older asset store packages written before this limitation was introduced may function incorrectly as a result. To fix problems relating to this issue, separate out the code for the classes in each namespace to separate files.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.