Skip to content

Instantly share code, notes, and snippets.

@habedi
Last active December 13, 2024 22:28
Show Gist options
  • Save habedi/daa01be223c940cd3e59e197143275c1 to your computer and use it in GitHub Desktop.
Save habedi/daa01be223c940cd3e59e197143275c1 to your computer and use it in GitHub Desktop.
Naming conventions for #Unity projects

Unity Naming Conventions

Using consistent naming conventions in Unity projects helps keep everything organized and easy to work with. Below is a list of recommended conventions for naming components, scripts, variables, methods, folders, and game objects in a Unity project.


General Rules

  1. PascalCase for Classes:

    • Use PascalCase for class names (e.g., PlayerController, GameManager).
    • Example:
      public class PlayerController : MonoBehaviour { }
  2. camelCase for Variables:

    • Use camelCase for variables and private fields.
    • Prefix private fields with _ (e.g., _playerHealth, _isJumping).
    • Example:
      private int _playerHealth;
      public float movementSpeed;
  3. CONSTANT_CASE for Constants:

    • Use all-uppercase with underscores for constants.
    • Example:
      private const int MAX_HEALTH = 100;
  4. k_Prefix for Static Variables:

    • Prefix static fields with k_.
    • Example:
      private static int k_MaxEnemies = 10;
  5. Descriptive Names:

    • Use clear and meaningful names to describe the purpose of a component or variable.
    • Avoid generic names like temp, value, or object.

Scripts and Components

Category Convention Example
Manager Classes Suffix with Manager GameManager, UIManager
Player-Specific Classes Prefix with Player PlayerController, PlayerStats
Singleton Classes Suffix with Instance AudioManagerInstance
Physics-Related Classes Suffix with Physics RigidbodyPhysics, ProjectilePhysics
UI Components Prefix with UI UIButton, UIHealthBar
System Helpers Suffix with Helper or Utility MathHelper, InputUtility

Variables

Type Convention Example
Private Fields camelCase with _ prefix _playerScore, _isPaused
Public Fields camelCase movementSpeed, health
Properties PascalCase PlayerName, IsAlive
Constants CONSTANT_CASE MAX_HEALTH, GRAVITY_FORCE

Folders

Organise folders with a naming structure that reflects their purpose:

Folder Convention Example
Scripts Scripts or [Feature]/Scripts Player/Scripts, UI/Scripts
Prefabs Prefabs Player/Prefabs, Environment/Prefabs
Materials Materials Player/Materials, UI/Materials
Animations Animations Player/Animations
Audio Audio Audio/Music, Audio/SFX

GameObjects

Category Convention Example
Root Objects Descriptive PascalCase PlayerCharacter, MainCamera
UI Elements Prefix with UI UICanvas, UIHealthBar
Lights Suffix with Light DirectionalLight, Spotlight
Environment Objects Descriptive PascalCase Tree, RockLarge, HouseSmall
Enemy Objects Prefix with Enemy EnemyGoblin, EnemyBoss
Spawners Suffix with Spawner EnemySpawner, ItemSpawner

Additional Rules for GameObjects:

  1. Consistent Naming:

    • Name objects based on their role and type.
    • Avoid names like GameObject1 or Untitled.
  2. Hierarchy Organisation:

    • Group related GameObjects under empty parent GameObjects with descriptive names (e.g., Environment, Enemies, UI).
  3. Instance-Specific Names:

    • Append instance identifiers when duplicating objects (e.g., Tree_01, Tree_02).

Public Methods

Action Convention Example
Actions Use verbs in PascalCase MovePlayer, AttackEnemy
Getters/Setters Prefix with Get or Set GetHealth, SetName
Events Use On Prefix OnPlayerDeath, OnButtonClicked

Example Component

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    // Private fields
    private float _speed = 5f;
    private int _health = 100;

    // Public properties
    public int Health
    {
        get { return _health; }
        set { _health = Mathf.Clamp(value, 0, 100); }
    }

    // Constants
    private const int MAX_ENEMIES = 10;

    // Methods
    private void MovePlayer()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector3 direction = new Vector3(horizontal, 0, vertical);
        transform.Translate(direction * _speed * Time.deltaTime);
    }

    private void OnPlayerDeath()
    {
        Debug.Log("Player has died!");
    }
}

Additional Tips

  • Abbreviations: Avoid unclear abbreviations. Use full words (e.g., HealthBar instead of HB).
  • Hierarchy Naming: Name GameObjects with descriptive names to match their role (e.g., PlayerCharacter, EnemySpawner).
  • Grouping: Use folders in the project and GameObject hierarchy to keep related components together.

Additional Resources

Changelog

  • 2024-12-13: The first version of this document was created.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment