官术网_书友最值得收藏!

Naming your variables properly

Always use meaningful names for storing your variables. If you don't do that, six months down the line, you will be sad. I'm going to exaggerate here a little bit to make a point. I will name a variable as shown in the following code:

public bool theBearMakesBigPottyInTheWoods = true;

That's a descriptive name. In other words, you know what it means by just reading the variable, and so ten years from now when you look at that name, you'll know exactly what it means. Now suppose instead of theBearMakesBigPottyInTheWoods, I had named this variable as shown in the following code:

public bool potty = true;

Sure, you know what potty is, but would you know that it referred to a bear making a big potty in the woods? I know right now you'll understand it because you just wrote it, but six months down the line, after writing hundreds of other scripts for all sorts of different projects, you'll look at that and wonder what potty meant. You'll have to read several lines of code you wrote to try to figure it out.

You may look at the code and wonder who in their right mind would write such a terrible code. So take the time to write a descriptive code that even a stranger can look at and know what you mean. Believe me, in six months or probably less, you will be that stranger.

Begin variable names with lowercase

You should begin a variable name with lowercase because it helps to distinguish between a class name and a variable name in your code. The Component names (class names) begin with a capital letter. For example, it's easy to know that Transform is a class, and transform is a variable.

There are of course exceptions to this general rule, and every programmer has a preferred way to use lowercase, uppercase, and perhaps an underscore to begin a variable name. At the end, you will have to decide upon a naming convention you like. If you read the Unity forums, there are some heated debates on naming variables. In this book, I will show you my preferred way, but you can use whatever is more comfortable for you.

Using multi-word variable names

Let's use the same example again as follows:

public bool theBearMakesBigPottyInTheWoods = true;

You can see that the variable name is actually eight words squished together. Since variable names can be only one word, begin the first word with a lowercase, and then just capitalize the first letter of each additional word. It greatly helps to create descriptive names and still being able to read it. There's a word for this called camelCasing.

Have a go hero – viewing multi-word variables in the Inspector panel

I already mentioned that for public variables, Unity's Inspector will separate each word and capitalize the first word. Go ahead, add the previous statement to LearningScript and see what Unity does with it in the Inspector panel.

主站蜘蛛池模板: 杭锦后旗| 芦山县| 峨边| 确山县| 天等县| 墨脱县| 文昌市| 漠河县| 卓资县| 美姑县| 宁都县| 保亭| 隆回县| 盐城市| 武功县| 平湖市| 甘肃省| 肇东市| 日喀则市| 广东省| 平远县| 敖汉旗| 增城市| 久治县| 北流市| 黎城县| 吴川市| 全州县| 改则县| 娱乐| 定襄县| 丽水市| 清远市| 肥城市| 焦作市| 乐亭县| 和林格尔县| 汉沽区| 苏州市| 红桥区| 湘乡市|