Namespaces Standards
Unity Namespace
A namespace is a grouping of related classes, interfaces, and other types, used to organize code and prevent naming conflicts. It allows for the code to be categorized and managed more effectively.
In C# classes can be accessed within a namespace by specifying the namespace when declaring or using the class. This is typically done with the using
keyword at the top of a script, which simplifies referencing classes without needing to use their fully qualified names. For example, to use Unity's MonoBehaviour
class, you include using UnityEngine
; at the top of your script.
Namespaces should be thought of as groups related code within a library. A library, on the other hand, is a collection of code that may include multiple namespaces. For example, .NET framework is a collection of libraries that includes many namespaces, like System.Collections
, which groups related types for handling collections of objects.
Unity Default C# Template
The Unity C# template does not specify a namespace, and is instead placed in the global namespace by default. This is generally acceptable for small projects or for scripts that are tightly coupled with Unity's MonoBehaviour system. The template does however include the common namespaces System.Collections
and System.Collections.Generic
, which are part of standard C# libraries for data structures, and UnityEngine
, which contains Unity-specific classes and functions.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyScript : MonoBehaviour
{
void Start()
{
// Initialization code
}
void Update()
{
// Update code
}
}
Creating Custom Namespaces
While it's not required, using namespaces can be very beneficial, especially in larger projects or when working in teams. Namespaces help to:
- Organize Code: Group related classes and functions together logically.
- Avoid Naming Conflicts: Prevent naming collisions between classes, especially when integrating third-party libraries or assets.
- Improve Readability: Make it clear where a class or function belongs within the project structure.
- Modularity: Namespacse offers the ability to maintain a modular codebase that is easier to manage and update.
A namespace can be added manually, encapsulating the class as shown in the example below.
namespace MyCompany.MyGame
{
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyScript : MonoBehaviour
{
void Start()
{
// Initialization code
}
void Update()
{
// Update code
}
}
}
Naming Convention for Namespaces
When naming namespaces in C#, following a clear and consistent convention is important for maintaining code readability and manageability. Here�s a description of a commonly used naming convention for namespaces:
General Guidelines
-
Hierarchical Structure: Use a hierarchical structure to group related classes and functionality. Each level in the hierarchy represents a more specific category within a broader context.
-
Company and Project name: Start with your company or organization name, followed by project name. Use General for grouping generic classes that will be used by various projects.
-
Descriptive and Clear: Choose names that clearly describe the purpose or functionality of the classes within the namespace. This makes it easier for developers to understand the role of the code.
-
Pascal Case: Use PascalCase (where each word starts with a capital letter) for namespace names. This is consistent with C# naming conventions and improves readability.
Example Naming Convention
Company | Project | Feature | Class | Example Name |
---|---|---|---|---|
MyCompany | General | Utilities, Configuration, Logging | Logger, ConfigurationManager, MathUtils, ExtensionMethods | MyCompany.General.Logger , MyCompany.General.ConfigurationManager , MyCompany.General.MathUtils , MyCompany.General.ExtensionMethods |
MyCompany | MyGame | UI, Audio, DataManagement | MainMenu, AudioManager, PlayerData | MyCompany.MyGame.UI.MainMenu , MyCompany.MyGame.Audio.AudioManager , MyCompany.MyGame.DataManagement.PlayerData |
YourCompany | YourProject | Networking, Gameplay, Utilities | NetworkManager, GameplayController, FileHelper | YourCompany.YourProject.Networking.NetworkManager , YourCompany.YourProject.Gameplay.GameplayController , YourCompany.YourProject.Utilities.FileHelper |
AnotherCo | AnotherApp | Graphics, AI, Input | Renderer, AIController, InputHandler | AnotherCo.AnotherApp.Graphics.Renderer , AnotherCo.AnotherApp.AI.AIController , AnotherCo.AnotherApp.Input.InputHandler |
Example Code
namespace MyCompany.MyGame.UI
{
public class MainMenu
{
// UI code for the main menu
}
}
namespace MyCompany.MyGame.DataManagement
{
public class PlayerData
{
// Code for managing player data
}
}
Organizing Namespaces
In a Unity project, custom namespaces should be organized within the Assets folder to ensure that they are easily accessible and maintainable. To effectively manage custom namespaces, create a folder structure that mirrors the namespace hierarchy. For example, if you have a custom namespace like MyCompany.General
, you should create a corresponding folder path within the Assets directory, such as Assets/Scripts/MyCompany/General
.
Place all related scripts into this folder to maintain a clear organization. This approach not only helps in keeping your project tidy but also ensures that Unity's script compilation process correctly recognizes and compiles your custom namespaces. By following this structured organization, you make it easier to manage, reference, and reuse code across different parts of your project or even in future projects.
Using Custom Namespaces
Just as the using
keyword is used to import the UnityEngine
classes, the same method is used for custom namespaces. This method is illustrated below.
using UnityEngine;
using MyCompany.MyGame.UI; // Include the namespace containing MainMenu
public class GameController : MonoBehaviour
{
void Start()
{
MainMenu mainMenu = new MainMenu(); // Instantiate and use MainMenu
mainMenu.Show(); // Call the Show method
}
}