Last active
April 22, 2018 10:13
-
-
Save fightingplane/bf1b5efa70fea23efe6cac58e575caac to your computer and use it in GitHub Desktop.
Unity3D Game Develop Tips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unity3D 开发命名规则 | |
/*1. 用Pascal规则来命名属性、方法、事件和类名 | |
Pascal规则是指名称中单词的首字母大写 ,如EmployeeSalary、 ConfimationDialog、PlainTextEncoding。 | |
*/ | |
public class HelloWorld | |
{ | |
public void SayHello(string name) | |
{ | |
} | |
} | |
/*2. 用Camel规则来命名成员变量、局部变量和方法的参数 | |
Camel规则类似于Pascal规则 ,但名称中第一个单词的首字母不大写 ,如employeeSalary、 confimationDialog、plainTextEncoding。 | |
给成员变量命名的时候,适当使用匈牙利命名法(给成员变量加任何前缀(如、m、s_等等)) | |
*/ | |
public class Product | |
{ | |
private string mProductId; | |
private string mProductName; | |
//UI界面上对象前面加下划线("_") | |
public GameObject _productIcon; | |
public void AddProduct(string productId,string productName) | |
{ | |
int productCount = 0; | |
} | |
} | |
/*3. 常量或者只读变量的变量名全部大写或者使用Pascal规则来命名*/ | |
// Recommanded | |
public static const string ShippingType = "DropShip"; | |
public static const string SHIPPING_TYPE = "DropShip"; | |
public static Dictionary<Relationship, string> S_RELATIONSHIP_STR = new Dictionary<Relationship, string>(){}; | |
// Avoid | |
public static const string SHIPPINGTYPE = "DropShip"; | |
/*4. 接口的名称一般以大写I作前缀 */ | |
public interface IConvertible | |
{ | |
byte ToByte(); | |
} | |
/*5. 自定义的属性以Attribute结尾 */ | |
public class TableAttribute:Attribute | |
{ | |
} | |
/*6. 自定义的异常以Exception结尾*/ | |
public class NullEmptyException:Exception | |
{ | |
} | |
/*7. 类的命名。用名词或名词短语来命名类名*/ | |
public class Employee | |
{ | |
} | |
public class BusinessLocation | |
{ | |
} | |
public class DocumentCollection | |
{ | |
} | |
/*8. 方法的命名。一般将其命名为动宾短语*/ | |
public class File | |
{ | |
public void CreateFile(string filePath) | |
{ | |
} | |
public void GetPath(string path) | |
{ | |
} | |
} | |
/*9.局部变量的名称要有意义, 不要直接用用i,j,k,l,m,n,x,y,z等做变量名,但for循环除外*/ | |
/*10. 代码分块 | |
所有的成员变量声明在类的顶端,用一个换行把它和方法分开。同时可以使用成对的#region...#endregion标记,方便折叠。*/ | |
/*11. 布尔型变量或者方法一般可以用is、can、has或者should做前缀。如,isFinished, canWork等。*/ | |
/*12. 一般C#的编码风格要求花括号{另起一行,不要直接跟在类名和方法后面。*/ | |
public Sample() | |
{ | |
// TODO: 在此处添加构造函数逻辑 | |
} | |
/*13. 可以用缩写作为UI元素的前缀 | |
常见UI组件的一般缩写形式: | |
Label --> lbl、Text --> txt、Button --> btn | |
Image --> img、 Widget --> Wgt、 List --> lst、CheckBox --> chk | |
Hyperlink --> lnk、Panel --> pnl、Table --> tab | |
ImageButton --> imb | |
*/ | |
public Button _exitBtn; | |
/*14. 判断条件是一个布尔变量时不要使用==进行条件判断*/ | |
// 不友好的写法 | |
private bool isFinished = true; | |
if(isFinished == true) | |
{ | |
// ... | |
} | |
// 推荐的写法 | |
private bool isFinished = true; | |
if(isFinished) | |
{ | |
// ... | |
} | |
/*15. 慎用缩写,变量名是一个单词的尽量不要缩写,多单词组成的变量名可适当缩写。*/ | |
/*16. 在类的顶部声明所有的成员变量,静态变量声明在最前面*/ | |
// Correct | |
public class Account | |
{ | |
public static string BankName; | |
public static decimal Reserves; | |
public string Number {get; set;} | |
public DateTime DateOpened {get; set;} | |
public DateTime DateClosed {get; set;} | |
public decimal Balance {get; set;} | |
// Constructor | |
public Account() | |
{ | |
// ... | |
} | |
} | |
/*17. 方法的书写规范 | |
如果一个方法超过25行,就需要考虑是否可以重构和拆分成多个方法。方法命名要见名知意,好的方法名可以省略多余的注释。方法功能尽量单一。*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment