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. }

 

 

 

 

 

 

 

posted @ 2021-06-17 17:40  yassine  阅读(323)  评论(0编辑  收藏  举报