1. 程式人生 > 其它 >Unity專案程式碼書寫規範

Unity專案程式碼書寫規範

以Google的程式碼規範為主,稍加改動https://google.github.io/styleguide/csharp-style.html

書寫規範

基礎寫法

  1. Pascal和駝峰混用,引數用駝峰寫法,除引數外,都以Pascal寫法為主。
  2. 括號建議用換行方式書寫

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.
    • 介面的實現儘可能安排寫在一起

註釋規範

程式碼頭部註釋
檔名稱:檔案的名稱。
功能描述:檔案的功能描述與大概流程說明。
作者:建立並編寫的人員。


日期:建立並編寫的日期。
修改記錄:若類有所修改,則需要有修改人員的名字、修改日期及修改理由。

// 檔名稱: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;

書寫示例

  1. using System; //using寫在整個檔案最前,多個using按下面層級以及字母排序
  2. using System.Collections; //1.system提供的
  3. using System.Collections.Generic;
  4. using UnityEngine; //2.unity提供的
  5. using UnityEngine.UI;
  6. using GameDataModule; //3.自定義的namespace
  7. namespace MyNamespace // Namespaces 命名規則為 PascalCase.
  8. {
  9. public interface IMyInterface // Interfaces 以 'I' 開頭
  10. {
  11. public int Calculate(float value, float exp); // 方法函式 命名規則為 PascalCase
  12. }
  13. public enum MyEnum // Enumerations 命名規則為 PascalCase.
  14. {
  15. Yes = 0, // Enumerations 命名規則為 PascalCase,並顯示標註對應值
  16. No = 1,
  17. }
  18. public class MyClass // classes 命名規則為 PascalCase.
  19. {
  20. public int Foo = 0; // Public 公有成員變數命名規則為 PascalCase.
  21. public bool NoCounting = false; // 最好對變數初始化.
  22. private class Results
  23. {
  24. public int NumNegativeResults = 0;
  25. public int NumPositiveResults = 0;
  26. }
  27. private Results _results; // Private 私有成員變數命名規則為 _camelCase.
  28. public static int NumTimesCalled = 0;
  29. private const int _bar = 100; // const 不影響命名規則.
  30. private int[] _someTable =
  31. {
  32. 2, 3, 4,
  33. }
  34. public MyClass() // 建構函式命名規則為 PascalCase.
  35. {
  36. _results = new Results // 物件初始化器最好用換行的方式賦值.
  37. {
  38. NumNegativeResults = 1, // 操作符前後用個空格分割.
  39. NumPositiveResults = 1,
  40. };
  41. }
  42. public int CalculateValue(int mulNumber)
  43. {
  44. var resultValue = Foo * mulNumber; // Local variables 區域性變數命名規則為camelCase.
  45. NumTimesCalled++;
  46. Foo += _bar;
  47. if (!NoCounting) // if後邊和括號用個空格分割.
  48. {
  49. if (resultValue < 0)
  50. {
  51. _results.NumNegativeResults++
  52. }
  53. else if (resultValue > 0)
  54. {
  55. _results.NumPositiveResults++;
  56. }
  57. }
  58. return resultValue;
  59. }
  60. public void ExpressionBodies()
  61. {
  62. //對於簡單的lambda,如果一行能寫下,不需要括號
  63. Func<int, int> increment = x => x + 1;
  64. // 對於複雜一些的lambda,多行書寫.
  65. Func<int, int, long> difference1 = (x, y) =>
  66. {
  67. long diff = (long)x - y;
  68. return diff >= 0 ? diff : -diff;
  69. };
  70. }
  71. void DoNothing() {} // Empty blocks may be concise.
  72. void CallingLongFunctionName()
  73. {
  74. int veryLongArgumentName = 1234;
  75. int shortArg = 1;
  76. // 函式呼叫引數之間用空格分隔
  77. AnotherLongFunctionNameThatCausesLineWrappingProblems(shortArg, shortArg, veryLongArgumentName);
  78. // 如果一行寫不下可以另起一行,與第一個引數對齊
  79. AnotherLongFunctionNameThatCausesLineWrappingProblems(veryLongArgumentName,
  80. veryLongArgumentName, veryLongArgumentName);
  81. }
  82. }
  83. }