2011-10-20 19:04Unity3d的Javascript入门教程Developer
我在奔跑, 我在狂笑,我还很年轻~
VARIABLES (变量)
Let's think of a variable as acontainer that holds something. The Value you set the variable tobe is what it beco mes.
我们来想想,变量就像一个临时存贮某种东西的容器,你给它设置什么值,它就会变成什么
You can choose to name a variable anything you wish, as long as itcontains no spaces, starts with a letter(prefera bly lower case),contains only letters, numbers, or underscores, and is not areserved keyword.
你可以给你的变量命你想要的名字.以小写字母开头.包括使用字母,数字,下划线.
Use the keyword 'var' to create a variable. Let's call our firstone 'box'.
输入"var"可以创建一个变量,让我们调用我们的第一个叫"box"的变量.
Code:(代码)
var box;
There you go; you've declared your first variable! If you'rewondering about the semicolon at the end, statements (commands) inJavascript must end in a semicolon.
Javascript中在申明一个变量时要以";"结尾.
iPhone programmers, if you declare a variable without setting it toa value, you must state what type the variable is, in this caseString. Common types include String, int, float, boolean, andArray.
如果是IPhone程序员,在声明一个变量时一定要指明变量的类型.在这个案例中是字符串型,一般情况下变量类型包括字符串型,整型,布尔型和阵列.
Note that proper capitalization isnecessary!
var box :String;
Of course, our box is empty, so let's set it to a value by addingthe following line:
当然我们的box是空的,因此我增加以代码给它一个值.
Code:
box ="apple";
Now our box contains a text string (variable type), which happensto be "apple".
现在我们的box包含字符型.它叫"apple".
Note that you can declare your variable and set it to a value inthe same statement:
也可以这样写
Code:
var box ="apple";
But once it's declared, you can't declare itagain.
但是你一旦申明一个变量就不能重复申明同一个变量
You can, however, change it to another value (as long as the newvalue is the same type as the original).
那么你如何改变成另一个值呢?
Code:
box ="orange";
In addition to text strings, variables can hold numbers:
除了字符串,你也可以存贮数字类型
Code:
var number =10;
This is an integer (int), which means whole nodecimal places.
这是整型(用int表示),它是个整数类型,没有小数点.
But we could also do a floating point number (float), which hasdecimal places:
我们也能用浮点型(用float表示)
Code:
var myFloat =10.5;
Variables can also contain booleans. A boolean is simply atrue/false value:
变量也能包括布尔型(用boolean表示), 布尔型只有true/false(真或假)两个值.
Code:
var gameOver =true;
We'll discuss these more later.
稍后我们会有更多的讨论.
Notice that a variable can normally only contain one thing at atime. But we can make a variable that contains m ultiple things, bycreating an array:
注意,通常情况下一个变量只包含一种东西,我们也可以让一个变量包含更多的内容我们就通过创建一个阵列来实现. Code:
var applePie = Array("apple", "brown sugar","butter", "pie crust");
This variable contains everything you need to make an applepie!
这个变量包含了制作一个的所以有东西
If we wanted to view the contents of applePie, we could output itto the Unity console using the Debug comman d.
如果我们想看看苹果派内容,可以通过Debug命令将它们输出到Unity控制台.
Add this line to the code above:
添加这行代码
Code:
On play, the console should display: apple,brownsugar,butter,pie crust
按下播放钮,控制台将显示:apple,brown sugar,butter,piecrust
To access a single element (item) from an array, we can access itthru its index (position in array). This may seem c onfusing, butindexes start at 0. So index 0 is actually the first element in thearray.
我们可以通过它的序号来访问它的元素,这看起来似乎有点混乱,不过由于序号是从0开始的,实际上0是阵列的第一个元素. Code:
var applePie = Array("apple", "brown sugar","butter", "pie crust");
var item1 = applePie[0];
Debug.Log(item1);
You can actually use indexing with Strings as well. The followingcode displays the first character of "hello", thelet ter 'h':
实际上你也可以用索引序号来访问字符.比如下面代码就显示"hello"的第一个字母"h"。
Code:
var myString ="hello";
Debug.Log(myString[0]);
Now lets alter our variables in other ways.
现在我们来改变一下我们的变量
We can add variables:
Code:
var number1 = 10;
var number2 = 11;
var total = number1 + number2;
Debug.Log(total);
If you add an int (integer) to a float (floating point number) theresult becomes a float:
如果我们给一个整型变量增加一个浮点型,结果就会变成一个浮点型变量
Code:
var number1 = 100;
var total = number1 + 1.5;
Debug.Log(total);
Obviously, we can also subtract/divide/multiply ints andfloats.
显然我们也可以用减/除/剩整型和浮点型变量
Less obvious, we can also 'add' strings. This merges themtogether:
可以通过“add”(加)这个字符串把它们结合在一起。
Code:
var string1 ="apple";
var string2 = "cider";
var combo = string1 + string2;
If we add a number to a string the result becomes astring.
如果我们把一个数字加到一个字符串类型上结果也会变成字符串型。
We can also multiply strings.
我们还可以对字符串使用剩的方式,使它倍增。
Code:
var greeting ="howdy";
Debug.Log(greeting * 3);
Unfortunately, we can't divide or subtract strings. There are waysto split strings and remove parts of strings, but t hat is a moreadvanced topic.
不幸的事,我们不能对字符串型进行减或除的操作,有专门对字符串进行分离和删除的方法,当然这是高级话题。
Let's wrap up our discussion on variables with incrementing numbers(changing their value by a set amount). 下面我们来看看怎样让数值递增
First, declare a variable and set it to 1:
首先申明一个变量,设置它的值为了:
Code:
var number =1;
We can increment numbers various ways.
我们能用不同的方式来增加这个数unity3d入门
Code:
number = number +1;
The above adds 1 to our number, and it becomes2.
上面是对原有的数加上1.其结果变成2.
But programmers are lazy, and decided this was too long. So theyabbreviated it to:
可是程序员可都要省事,可能这样定觉得太长了,所以要写短一点
Code:
number +=1;
This is simply shorthand for number = number +1;
可以简单地缩短成number=number+1;
But guess what? Lazy programmers decided even THIS was too long,and shortened it to:
可是你猜怎么着,懒惰的程序员还是觉得它太长了.于是就缩短成这样:
Code:
number++;
Use whichever makes most sense to you, they all do the same thing!But note that if you choose to increment by
a value other than 1,++ won't work. ++ is shorthand for += 1only.
这些方法的结果都是一样的,但值得注意的事增加指定的值"++"是不可以的."++"只表示"+=1"
You can also do the same with subtraction:
同样道理,你也可以写成"--"
Code:
number--;
But for division and multiplication, you must use one of the longerforms:
但是乘除就不能那样了
Code:
number = number/2;
number *= 2;
Note that an asterisk means multiply, a slash means divide.
Enumeration (枚举)
Enum allows you to create a collection of integer based constantvalues. While working with Javascript variables i nstead of creatingindividual variable for constants you must use Javascript enumvariable to store integer based v alues.
Enum(枚举数型)允许你创建一个整数集合.
Javascript enum type variables and their valuesprovide the systematic code pattern as well as readability andund erstanding for the code. You can easily get the integer valueassociated with the named enum type item.
JS的枚举类变量和它们的值提供了代码模式,以便它的可读性和对代码的理解.你可以轻易地一个整数值
Code:
enum myEnum { myName1, myName2, myName3}
Enum is an enumeration type collection that stores the items withcomma separation "," and its corresponding integer value separated with colon ":".
Code:
enum AiState { Sleeping, Idling, Chasing,Dying }
function Update ()
{
varstate :AiState; //first example
var state = AiState.Sleeping;
print (state);
var curState :AiState; // secondexample
curState = AiState.Idling;
switch (curState)
{
case AiState.Sleeping: print ("aiState issleeping"); break;
case AiState.Idling: print ("aiState is idling");break;
default: break;
}
}
Note: The Enumeration should be declared outside a function
注意:枚举类应该在函数外面申明.
IF STATEMENTS (if语句)
If statements are conditional statements. If a condition evaluatesas true, do something.
We can do a comparison of two values by using two equal signs,==
"number == 10" evaluates as true if our number variable equals 10,otherwise it evaluates as false.
if语句是条件语句,如果条件满足为真,执行.我们可以用"=="双等号对两个值进行比较,比如"number==10"当条件为真时变量number等于10,反之为假.
Note: it's important to remember to use two equal signs whencomparing variables/values, but one equal sign wh en setting avariable to a value!
注意.重要的事要记住比较变量与值时用双等号,而不是一个等号.
The following creates a variable and sets it to true, checks to seeif the variable equals true, and if so prints a text 下面创建一个变量,设置为真,检测为真时打印后面的文字.
string to the console:
Code:
var gameStarted =true;
if (gameStarted == true)
Debug.Log("Game hasstarted");
The above is actually redundant, since our variable 'gameStarted'is a boolean. There is no reason to check "if tru e equals true",just check "if true":
上面这些是多此一举,由于我们的变量gameStarted是布尔型,没理由检测是否为"等于'=='"真
Code:
var gameStarted =true;
if (gameStarted)
Debug.Log("Game hasstarted");
If you're wondering why I didn't put a semicolon behind if(gameStarted), it's because technically it is only the fir st halfof the statement. I could have written it like so:
你可能觉得奇怪.为什么我不在if后面放上";"分号呢,那是因为理论上说它只是语句的第一部分.我可以写成这样:
Code:
if (gameStarted) Debug.Log("Game hasstarted");
I could have also written it this way:
还能用这种方法写:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论