Unity项目代码书写规范
以Google的代碼規范為主,稍加改動?https://google.github.io/styleguide/csharp-style.html
書寫規范
基礎寫法
?
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.自定義的namespacenamespace 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);}} }?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的Unity项目代码书写规范的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 发现你的美《坦克世界》重制版新地图彩蛋合
- 下一篇: Unity游戏中的一些规范和优化建议