As a programmer, it is very important to follow a coding standard and make sure others can read and easily understand his code. While there’s not majority guideline of coding standard in Unity and C#, you can/should code whatever you like, but in this article I’m going to show you our style and format guideline within the company. We have tried to adopt different styles throughout these years and came out the one we feel most comfortable.
Our 3 golden rules to forms our style standard:
1. Follow Unity’s style in Scripting Reference
2. The rest follow Microsoft’s C# Coding Conventions
3. Minor tweak where we believe that can make the code clearer
Folder Structure
All similar files should be grouped in the root folders:
Assets
Animations Animation clips
Editor Editor specified scripts, prefabs, etc
Fonts Fonts used in game
Materials Texture materials
Models Models, fbx
Prefabs In-game prefabs
Resources Unity resources assets
Scenes Scenes
Scripts Code scripts, grouped in sub-folders
Sounds Musics, sounds
SweatyChair Company folder that share across projects
(ModuleName1) Module name, e.g. DailyLogin
(Scripts) Scripts within the module
(Prefabs) Prefabs within the module
... Other assets grouped in folders
... Other modules
Textures Image textures
UI Texture used for UI
Icons App icons
We have a
SweatyChair
folder, which containes a number of well-structure modules and sync across projects using Git submodules. We we are always reuse the commond code and able to perform rapid prototyping.
File Naming
File naming is simple, always use Pascal Case except for images:
- Folders — PascalCase
FolderName
/ - Images — hyphen-
image-name-64x64.jpg
- The rest — PascalCase
ScriptName.cs
,PrefabName.prefab
,SceneName.unity
The reason images use hyphen naming is that most images are also used in our website / press-kit. Hyphen naming is the majority naming convention for web images and it’s also search engine friendly. Using the same names can save us time renaming and easily find the same images when switching between Unity and web.
Script Naming
While each script has its unique purpose and use cases, we follow these naming rules and so we can still roughly know what the script is for by simply reading its filename:
XxxPanel
,XxxSlot
,XxxButton
, etc for UIMenuPanel
,AchievementSlot
,CoinShopButton
XxxManager
, for master scripts that control specific workflow (only ONE instance in the scene)DailyMissionManager
,AchievementManager
XxxController
, for scripts controlling a game object (one or many in the scene)PlayerController
,BossController
,BackgroundControler
XxxDatabase
, for a database (e.g CSV) which contains a list of data rowsWeaponDatabase
,CardDatabase
XxxData
, for data row in a CSV databaseWeaponData
,CardData
XxxItem
, for in-game item instanceCardItem
,CharacterItem
XxxGenerator
, for scripts instantiate GameObjectsObjectGenerator
,LandGenerator
XxxSettings
, for settings scripts inherent Unity’sScriptableObject
classAchievementSettings
,DailyLoginSettings
XxxEditor
, for editor-only scripts inherent Unity’sEditor
classTutorialTaskEditor
,AchievementSettingsEditor
Difference between
Manager
andController
isManager
should be singleton or static, and it controls a specific game logic that may involve multiple objects and assets, while Controller controls an object and may have multiple instances. For example, there are multiple EnemyController in the scene and each control one enemy. We will discuss more on this in a singleton article.Difference between
Data
andItem
is thatItem
is an instance in-game, and in most casesItem
contains aData
. For exampleCardData
has all preset attributes of a card, whileCardItem
has itsCardData
plus attributes that vary for different players, such as card level. We will talk about more this in another data structure article.
Variable Naming
Similar to file naming, variable naming allow us to know what the variable is for without reading though all code:
- Use camcelCase, always as of a noun or in a form of (is/has)Abjective
hasRewarded
,currentPosition
,isInitialized
- Prefix with an underscore for private and protected variables
_itemCount
,_controller
,_titleText
- All capital letters for const
SHOW_SUB_MENU_LEVEL
,MAX_HP_COUNT
,BASE_DAMAGE
- All capital letters and PREFS_XXXX for PlayerPref keys
private const string PREFS_LAST_FREE_DRAW = “LastFreeDraw”;
PlayerPrefs.GetInt(PREFS_LAST_FREE_DRAW)
- Use
xxxGO
for scene GameObject variablesoptionButtonGO
,backgroundMaskGO
- Use
xxxPrefab
for scene GameObject variablesweaponSlotPrefab
,explosionPrefab
- Use
xxxTF
for Transform variablesweaponTF
,armTF
- Use
xxxComponent
for all other components, abbreviate if neededeyesSpriteRenderer
/eyesSR
,runAnimation
/runAnim
,attckAnimationClip
/attackAC
,victoryAudioClip
/victoryAC
- Use xxxxs for arrays
slotPrefabs = new Prefabs[0]
achievementIds = new int [0]
- Use xxxxList for List and xxxxDict for Dictionary
weaponTransformList = new List()
achievementProgressDict = new Dictionary()
- Use nounVerbed for callback event
public static event UnityAction gameStarted
public static UnityAction characterDied
Unity use prefix
m_
for private/protected variables, ands_
for static variables (see their BitBucket). We found that this makes the code very hard to read, so we leave one underscore for private/protected variables and keep it the same for both static and non-static variables.Callback event follow Unity’s event naming convention, e.g.
SceneManager.activeSceneChanged
.
Function Naming
- Use PascalCase, start with a verb and followed by a noun if needed
Reward()
,StartGame()
,MoveToCenter()
- Use
OnXxxxClick
, for UI button clicksOnStartClick()
,OnCancelClick()
- Use
OnNounVerbed
, for callbacksOnChestOpened()
,OnBuyWeaponConfirmed()
Member Properties
If a function has no input, use member property (aka get/set property) instead, in PacelCase as well:bool IsRewarded() { }
tobool IsRewarded {get { return …} }
Or even in a shortened form asbool IsRewarded => ...;
Also, use member properties where a variable can only set internally but can be accessed publicly:public int Id { get; private set; }
public Sprite IconSprite { get; private set; }
Variables / Functions Order
Having a consistent order can greatly save our time on looking for a specific variable or function:
MyCalss: Monobehavior {
// Constant variables
public const int CONST_1;
private const string CONST_2;
// Static variables
public static int static1;
private static string _static2;
// Editor-assigned variables
[SerializeField] public Image maskImage;
[SerializeField] private MyClass _anotherComponent;
// Other varaibles
public int primitiveVariables1;
private string _primitiveVariable2;
// Properties
public int Property1 {
get;
set;
}
private string _roperty2 {
get;
set;
}
// Unity functions
Awake()
OnEnable()
Start()
Update()
FixedUpdate()
// Other custom functions
Init()
...
Reset()
// Unity functions
OnDisable()
OnDestroy()
#region UNITY_EDITOR
// Eebug functions that only runs in Editor
[ContextMenu("...")]
private void DebugFunction1()[MenuItem("Debug/...")]
private static void DebugFunction2()
#endregion
}
You can also group valuables and functions in a region in large scripts (but not too large enough to be split into another script):
#region Timer
private
const int RESET_SECONDS = 180;
private float _secondsLeft;
public float canResetTime => _secondsLeft < 0;
public void ResetTime() {
_secondsLeft = RESET_SECONDS;
}
#endregion Timer
... // Other regions
Code Formatting
We use K&R style which is already preset in Mac Visual Studio:
While there’s not K&R in Windows (Why, Microsoft?), just a little bit effort to manually set few ticks:
Brackets and Newlines
For single-line conditional statements we do not need any brackets:
if (1 == 1)
DoSomethingFancy();
if (1 == 1) return true;
if (1 == 1) {
FirstThing();
LastThing();
}
Many developers, includes Unity, favourite putting newlines after open bracket (
{
). It may make the code looks more tidy and symmetric, but also creates a lot of unuseful lines and making script scrolling much more often and longer. Every bit count. If you spend 1 more second on scrolling a day, you probably wasted 5 minutes of valuable time a year.
Comments
Single line comments should include a space after the slashes:// Notice the space after the two slashes
Temporarily commenting out code should not include a space after the slashes, to be differentiated from the normal comment://noSpace.Code();
TODO
comments should include the space after the slashes and then 2 spaces after the colon following the TODO// TODO: Notice the space after the slashes and the 2 spaces after the colon of todo
That’s all. This may go bigger when you adopt more latest .Net syntax. There are also a few other good guidelines if you want to read more. Free feel to follow this style if you think it’s reasonable and helpful, so we may create a better game dev world where code is clean and please to read. ?
Yes! Finally something about Cialis.