Skip to main content

Namespace Standards

Written by: Akram Taghavi-Burris | © Copyright 2024

Status: In Development

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.

Groups of Code

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.

Assets/Scripts/MyScript.cs
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.

Assets/Scripts/MyCompany/MyGame/MyScript.cs
    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MyCompany.MyGame
{
public class MyScript : MonoBehaviour
{
void Start()
{
// Initialization code
}

void Update()
{
// Update code
}
}
}
Exclude Using

When encapsulating your classes in their own namespace, do not include using directives inside the namespace. This ensures that there are no duplications of imports between classes.

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

CompanyProjectFeatureClassExample Name
MyCompanyGeneralUtilities, Configuration, LoggingLogger, ConfigurationManager, MathUtils, ExtensionMethodsMyCompany.General.Logger, MyCompany.General.ConfigurationManager, MyCompany.General.MathUtils, MyCompany.General.ExtensionMethods
MyCompanyMyGameUI, Audio, DataManagementMainMenu, AudioManager, PlayerDataMyCompany.MyGame.UI.MainMenu, MyCompany.MyGame.Audio.AudioManager, MyCompany.MyGame.DataManagement.PlayerData
YourCompanyYourProjectNetworking, Gameplay, UtilitiesNetworkManager, GameplayController, FileHelperYourCompany.YourProject.Networking.NetworkManager, YourCompany.YourProject.Gameplay.GameplayController, YourCompany.YourProject.Utilities.FileHelper
AnotherCoAnotherAppGraphics, AI, InputRenderer, AIController, InputHandlerAnotherCo.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 Unity, custom namespaces should align with your Assets folder to ensure they are easily accessible and maintainable. Create a folder structure that mirrors your namespaces for better organization and easier navigation. This method also helps Unity's script compilation process recognize and compile your namespaces correctly.

For example, if you have a custom namespace like MyCompany.MyGame.UI, create the folder path Assets/Scripts/MyCompany/MyGame/UI. Place all related scripts within this folder.

Custom Namespaces

Just as Unity's namespaces are imported with the using keyword, you can also use the using keyword to include your custom namespaces:

Assets/Scripts/GameController.cs
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
}
}