Unity專案程式碼書寫規範
以Google的程式碼規範為主,稍加改動https://google.github.io/styleguide/csharp-style.html
書寫規範
基礎寫法
- Pascal和駝峰混用,引數用駝峰寫法,除引數外,都以Pascal寫法為主。
- 括號建議用換行方式書寫
Code
- 類, 方法, 列舉, public 欄位, public 屬性, 名稱空間的命名規則用:
PascalCase
. - 區域性變數,函式引數命名規則用:
camelCase
. - private, protected, internal and protected internal 欄位和屬性的命名規則用:
_camelCase
. - 命名規則不受const, static, readonly等修飾符影響.
- 對於縮寫,也按
PascalCase
命名,比如MyRpc
而不是
.MyRPC - 介面以
I
,開頭..
Files
- 檔案和資料夾命名規則為
PascalCase
, 例如MyFile.cs
. - 檔名儘量和檔案中主要的類名一直, 例如
MyClass.cs
. - 通常,一個檔案中一個類.
Organization
- 如果出現,修飾符按下列順序書寫:
public protected internal private new abstract virtual override sealed static readonly extern unsafe volatile async
. - 名稱空間在最頂部,using順序按Sytem,Unity,自定義的名稱空間以及字母順序排序,
- 類成員的順序:
- Group按下列順序:
- Nested classes, enums, delegates and events.
- Static, const and readonly fields.
- Fields and properties.
- Constructors and finalizers.
- Methods.
- 每個Group內,按下列順序:
- Public.
- Internal.
- Protected internal.
- Protected.
- Private.
- 介面的實現儘可能安排寫在一起
- Group按下列順序:
註釋規範
程式碼頭部註釋
檔名稱:檔案的名稱。
功能描述:檔案的功能描述與大概流程說明。
作者:建立並編寫的人員。
日期:建立並編寫的日期。
修改記錄:若類有所修改,則需要有修改人員的名字、修改日期及修改理由。
// 檔名稱:UserInput.cs
// 功能描述:玩家輸入按鍵的定義
// 編寫作者:張三
// 編寫日期:2017.7.16
// 修改記錄:
// R1:
// 修改作者:李四
// 修改日期:2017.7.17
// 修改理由:使玩家可以自定義輸入按鍵
Using System;
方法註釋
採用 /// 形式自動產生XML標籤格式的註釋。包括方法功能,引數含義,返回內容
/// <summary>
/// 設定場景的名字.
/// </summary>
/// <returns><c>true</c>, 場景名字設定成功, <c>false</c> 場景名字設定失敗.</returns>
/// <param name="sceneName">場景名字.</param>
public bool SetSceneName(string sceneName)
{
}
類變數註釋
採用 /// 形式自動產生XML標籤格式的註釋變數含義。
/// <summary>
/// 場景的名字
/// </summary>
private string mSceneName;
區域性變數註釋
在變數宣告語句的後面註釋,與前後行變數宣告的註釋左對齊,註釋與程式碼間以Tab隔開。
string firstName; //姓
string lastName; //名
程式碼行註釋
註釋位於程式碼上行,與程式碼開始處左對齊,雙斜線與註釋之間以空格分開
//設定場景的名字。
this.mSceneName = sceneName;
書寫示例:
- using System; //using寫在整個檔案最前,多個using按下面層級以及字母排序
- using System.Collections; //1.system提供的
- using System.Collections.Generic;
- using UnityEngine; //2.unity提供的
- using UnityEngine.UI;
- using GameDataModule; //3.自定義的namespace
-
- namespace MyNamespace // Namespaces 命名規則為 PascalCase.
- {
- public interface IMyInterface // Interfaces 以 'I' 開頭
- {
- public int Calculate(float value, float exp); // 方法函式 命名規則為 PascalCase
- }
-
- public enum MyEnum // Enumerations 命名規則為 PascalCase.
- {
- Yes = 0, // Enumerations 命名規則為 PascalCase,並顯示標註對應值
- No = 1,
- }
-
- public class MyClass // classes 命名規則為 PascalCase.
- {
- public int Foo = 0; // Public 公有成員變數命名規則為 PascalCase.
- public bool NoCounting = false; // 最好對變數初始化.
- private class Results
- {
- public int NumNegativeResults = 0;
- public int NumPositiveResults = 0;
- }
- private Results _results; // Private 私有成員變數命名規則為 _camelCase.
- public static int NumTimesCalled = 0;
- private const int _bar = 100; // const 不影響命名規則.
- private int[] _someTable =
- {
- 2, 3, 4,
- }
- public MyClass() // 建構函式命名規則為 PascalCase.
- {
- _results = new Results // 物件初始化器最好用換行的方式賦值.
- {
- NumNegativeResults = 1, // 操作符前後用個空格分割.
- NumPositiveResults = 1,
- };
- }
-
- public int CalculateValue(int mulNumber)
- {
- var resultValue = Foo * mulNumber; // Local variables 區域性變數命名規則為camelCase.
- NumTimesCalled++;
- Foo += _bar;
- if (!NoCounting) // if後邊和括號用個空格分割.
- {
- if (resultValue < 0)
- {
- _results.NumNegativeResults++
- }
- else if (resultValue > 0)
- {
- _results.NumPositiveResults++;
- }
- }
- return resultValue;
- }
-
- public void ExpressionBodies()
- {
- //對於簡單的lambda,如果一行能寫下,不需要括號
- Func<int, int> increment = x => x + 1;
-
- // 對於複雜一些的lambda,多行書寫.
- Func<int, int, long> difference1 = (x, y) =>
- {
- long diff = (long)x - y;
- return diff >= 0 ? diff : -diff;
- };
- }
-
- void DoNothing() {} // Empty blocks may be concise.
-
-
- void CallingLongFunctionName()
- {
- int veryLongArgumentName = 1234;
- int shortArg = 1;
- // 函式呼叫引數之間用空格分隔
- AnotherLongFunctionNameThatCausesLineWrappingProblems(shortArg, shortArg, veryLongArgumentName);
- // 如果一行寫不下可以另起一行,與第一個引數對齊
- AnotherLongFunctionNameThatCausesLineWrappingProblems(veryLongArgumentName,
- veryLongArgumentName, veryLongArgumentName);
- }
- }
- }