unity-coding-conventions
Skill1 starsChanged 5 months agoMITView on GitHub
C# coding conventions for Unity projects - naming, style, and organization. ALWAYS Auto-loaded when writing code for Unity Projects.
In this repository9 other files it gives its agents.
- unity-architecture-conventionsSkill
- unity-folder-conventionsSkill
- unity-mcp-toolsSkill
- unity-performanceSkill
- unity-testingSkill
- unity-ugui-developmentSkill
- unity-ugui-listSkill
- writing-testsSkill
- zenject-conventionsSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
26 rules. About 0 are common, 15 are distinctive, and 11 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈15
Distinctive · estimate
This author's own choices. Worth a look.
11
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 7
Follow these conventions when writing C# code for Unity projects.
- Very specific: line 24
All lowercase → _gpsManager, m_uiManager
- Very specific: line 52
Every .cs file must have a namespace.
- Distinctive: line 54
Avoid sub-namespaces within an assembly for categorization — use folders instead
- Distinctive: line 73
Never use plain public fields (except ScriptableObjects — see below)
- Distinctive: line 102
Use public fields for data (exception to the properties preference)
- Very specific: line 107
Use auto-properties { get.
- Distinctive: line 117
Always handle exceptions properly - never fire-and-forget without error handling
- Distinctive: line 118
Use try-catch in async methods, log errors before re-throwing if needed
- Very specific: line 129
If an Update() body begins with if (!m_isRunning) return.
- Distinctive: line 130
Prefer coroutines or UniTask for state-driven loops, timed sequences, or logic that activates/deactivates during gameplay.
- Distinctive: line 141
Place at the end of the containing method
- Very specific: line 142
If void method, add explicit return.
- Distinctive: line 158
If enum is only used inside one class: declare inside that class as private
- Distinctive: line 159
If rarely used outside: declare inside as public
- Distinctive: line 179
Unity lifecycle methods: Place initialization methods first (Awake, OnEnable, Start), disposal methods last (OnDisable, OnDestroy).
- Distinctive: line 181
Closely related fields: When fields have a tight relationship, place them adjacent to each other within their section.
- Very specific: line 193
Prefer ?. and ?? operators
- Very specific: line 194
Warning: Do NOT use ?. or ?? for UnityEngine.Object - Unity overloads == for destroyed object detection, so use explicit null checks instead
- Very specific: line 199
Use method syntax only (.Where().Select())
- Very specific: line 200
Never use query syntax (from x in y)
- Very specific: line 206
Use descriptive names (TEntity, TResult) when meaning isn't obvious
- Very specific: line 210
Always name elements: (int x, int y) not (int, int)
- Distinctive: line 214
Avoid boolean parameters - use enums or separate methods instead
- Distinctive: line 224
Only for convoluted sections of code
- Distinctive: line 226
If you need many comments, consider refactoring the code
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Unity Coding Conventions
Follow these conventions when writing C# code for Unity projects.
Repository & File Naming
- Repositories: kebab-case ending with "-unity" (e.g.,
my-game-unity) - Files and directories: PascalCase
- In case of script files, its name will be the same as the most important definition inside that file (class/struct/enum, etc.)
- Special ordering prefix: _PascalCase (to appear first alphabetically, should be used sparingly, mostly for folders)
Abbreviation Rules
In PascalCase:
- 3+ letters: First capitalized, rest lowercase →
GpsManager - 2 letters: Both capitalized →
UIManager - Exception: "id" is always
Idin PascalCase →UserId,GetId()
In camelCase (including _camelCase, m_camelCase):
- All lowercase →
_gpsManager,m_uiManager
Class & Method Naming
- Classes and structs: PascalCase
- Methods: PascalCase
- Method arguments: camelCase
- Local variables: camelCase
- Method names must accurately describe what the method does — a method that retrieves an available item should be
GetAvailable()orRent(), notActivate()if it doesn't perform activation
Namespace Conventions
One namespace per assembly. The namespace matches the assembly name, prefixed with the company/username:
| Assembly (asmdef name) | Namespace | Purpose |
Core | [CompanyName].Core | Shared/foundational runtime code (_Core/) |
Core.Editor | [CompanyName].Core.Editor | Core editor tools & automation (_Core/Editor/) |
[ProjectName] | [CompanyName].[ProjectName] | Runtime code |
[ProjectName].Editor | [CompanyName].[ProjectName].Editor | Editor tools |
[ProjectName].Tests.Editor | [CompanyName].[ProjectName].Tests.Editor | Edit mode tests |
[ProjectName].Tests.Runtime | [CompanyName].[ProjectName].Tests.Runtime | Play mode tests |
Example for COMPANY_NAME = acme, project MyGame:
acme.Core,acme.Core.Editoracme.MyGame,acme.MyGame.Editoracme.MyGame.Tests.Editor,acme.MyGame.Tests.Runtime
Every .cs file must have a namespace. The asmdef rootNamespace field is set to the assembly's namespace so Unity auto-fills it.
- Avoid sub-namespaces within an assembly for categorization — use folders instead
- Rationale: Too many coupled namespaces add boilerplate without improving readability
- File-scoped namespaces: Use
namespace Foo;if C# 10 is enabled, otherwise traditional block style - Using directives: Place outside namespace, no specific ordering required
Field Naming
| Field Type | Convention | Example |
| Public/Protected fields | PascalCase | public int Health |
| Properties | PascalCase | public int Health { get; set; } |
| Private immutable | _camelCase | [SerializeField] private Transform _target |
| Private mutable | m_camelCase | private float m_currentSpeed |
| Coroutine refs | m_camelCase + Coroutine suffix | private Coroutine m_movementCoroutine |
| Static mutable | s_camelCase | private static int s_instanceCount |
| Static readonly | _camelCase | private static readonly float _defaultSpeed |
| Constants | ALL_CAPS | private const float MAX_SPEED |
Properties vs Public Fields:
- Never use plain public fields (except ScriptableObjects — see below)
- Auto-property
{ get; private set; }— use when no custom logic is needed. This is the default choice. - Backing field
private int m_health; public int Health => m_health;— use only when:- The setter contains validation or side-effect logic
- The backing field serves double duty (e.g.,
[SerializeField]for inspector exposure alongside a public getter) - The field needs internal mutation with complex logic that a simple setter can't express
- For Unity inspector exposure:
[field: SerializeField] public int Health { get; private set; }
Why "_camelCase" for "immutable"?
In common Unity workflows, many fields cannot be marked readonly even though we would want them to be. These fields are set once during initialization and never changed - they are "ideally" immutable. Use _camelCase for these fields to signal intent:
[SerializeField]injections (set by inspector, never reassigned)[Inject]injections (set by Zenject, never reassigned)- Fields resolved in constructors, Awake, or equivalent initialization methods
Events
- Events: PascalCase with "On" prefix and past tense →
OnButtonClicked,OnDamageReceived - Handler methods: Use "Handle" prefix →
HandleButtonClicked,HandleDamageReceived
Other Naming
- Interfaces: Start with "I" →
ICopyable - Enums: Plural name →
States - Enum values: Always explicitly numbered starting from 0 →
None = 0, Running = 1, Stopped = 2 - Enum instances: Singular →
private States m_state
ScriptableObjects
- Naming: End with "SO" →
PlayerConfigSO,WeaponDataSO - Use public fields for data (exception to the properties preference)
Data Models
- Naming: End with "Model" →
PlayerModel,EnemyModel - Use auto-properties
{ get; set; }for data fields - Data models are plain C# classes/structs for data transfer (DTOs, API responses, serialization)
Async
- Async methods end with "Async" →
ConnectToServerAsync - CancellationToken parameter:
cancellationToken(full name), always last parameter, with= default - CancellationTokenSource fields:
m_operationCts(mutable) or_lifetimeCts(immutable)
Exception handling in async:
- Always handle exceptions properly - never fire-and-forget without error handling
- Use try-catch in async methods, log errors before re-throwing if needed
- For fire-and-forget calls, wrap in a method that catches and logs exceptions
Coroutines
- Coroutine methods (returning
IEnumerator): End with "Coroutine" →MovementCoroutine() - Coroutine reference fields: End with "Coroutine", follow mutable field convention →
private Coroutine m_movementCoroutine
Update vs Coroutines/UniTask
- Reserve
Update()for continuous per-frame logic that truly runs every frame (e.g., input polling, smooth animation). - If an
Update()body begins withif (!m_isRunning) return;or similar guard — that is a signal it should be a coroutine or UniTask instead. - Prefer coroutines or UniTask for state-driven loops, timed sequences, or logic that activates/deactivates during gameplay.
Code Style
- Curly braces on separate lines (Allman style)
var: Use for complex types, explicit for primitives (int,string,bool,float)- Expression bodies (
=>): Always use when body is a single expression - Modifier order:
public static readonly(access → static → readonly) - Never use
#region
Local methods:
- Place at the end of the containing method
- If void method, add explicit
return;before local methods:
private void DoWork()
{
var data = GetData();
ProcessData(data);
return;
int GetData() => 42;
void ProcessData(int d) => Debug.Log(d);
}Enum Scoping
- If enum is only used inside one class: declare inside that class as private
- If rarely used outside: declare inside as public
- Benefit: Easier naming without project-wide uniqueness concerns
Member Ordering
Order members within a class from top to bottom:
- Nested types (classes, structs, enums)
- Constants (
const) - Readonly fields (
readonly) - Immutable fields (
_camelCase) - Zenject injections (
[Inject]) - Inspector injections (
[SerializeField],[SerializeReference],[field: SerializeField]properties) - Mutable fields (
m_camelCase) - Delegate definitions
- Events / delegate instances
- Public fields
- Properties
- Initializers (constructor, Awake)
- Methods - ordered logically (caller above callee, Clean Code style)
Unity lifecycle methods: Place initialization methods first (Awake, OnEnable, Start), disposal methods last (OnDisable, OnDestroy). No strict ordering for other lifecycle methods.
Closely related fields: When fields have a tight relationship, place them adjacent to each other within their section. Specifically, a backing field immediately precedes its property:
// Properties section
private int m_health;
public int Health => m_health;
private string m_playerName;
public string PlayerName { get => m_playerName; set => m_playerName = value; }Null Handling
- Prefer
?.and??operators - Warning: Do NOT use
?.or??forUnityEngine.Object- Unity overloads==for destroyed object detection, so use explicit null checks instead - No silent null returns: Early returns on unexpected null must include
Debug.LogWarningso failures are visible during development. Silentif (x == null) return;hides bugs.
LINQ
- Use method syntax only (
.Where().Select()) - Never use query syntax (
from x in y) - For performance implications of LINQ in hot paths, see
unity-performanceconventions
Generics & Tuples
Generic type parameters:
- Use descriptive names (
TEntity,TResult) when meaning isn't obvious - Single
Tis acceptable if clear from class name
Tuples:
- Always name elements:
(int x, int y)not(int, int)
Method Parameters
- Avoid boolean parameters - use enums or separate methods instead
Constants Location
- Class-specific constants: Keep in the class that uses them
- Shared/project-wide constants: Place in a dedicated constants file (e.g.,
GameConstants.cs) constis for true invariants only — math constants, fixed protocol values. Gameplay-tuning values (speeds, thresholds, durations) should be[SerializeField]or ScriptableObject config so they're adjustable without recompilation.
Comments
- Only for convoluted sections of code
- Code should be self-explanatory without comments
- If you need many comments, consider refactoring the code
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

