在使用unity3d進行遊戲開發或者互動項目製作的時候,有3種編輯語言可供使用,不過用的最多的應該是javascript和c#兩種語言,相信很多朋友特別是新手都會很糾結於到底該用二者種的哪一個比較好,下面是老外對unity3d中這兩種語言進行的一個整體有效對比,此為第一章節,主要對比二者在變數,類,函數等常規基礎要點上的異同!
This is the first post of a series that will discuss some differences betweenJavaScript and C# when programming for Unity3D game engine. A project with all the scripts used in these posts will be available for download in the last post of
the series. So let’s start from the start and see some of the main differences between the two languages.
The first and most easily distinguishable is the notation used to declare variables and methods. When programming in JavaScript, it isn’t necessary to declare the variable or method type whether in C#, it’s a must. That is because JavaScript
is a weakly typed language, meaning that the interpreter chooses the best type to use at compilation-time. C# is a totally different because it is strongly typed, so, the type has to be declared before a variable
or method. Let’s see some examples to better grasp this concept. The following script is a JavaScriptexample:
private var cubeTransform;
And in the C#, the same code would be:
private Transform cubeTransform;
This is also valid for methods. Even if a C# method returns nothing, the void type has to be declared as the return type, which can be omitted in JavaScript.
Class inheritance also works differently. For both JavaScript and C#, methods are implicitly final and can’t be overridden, unless the virtual keyword is added to the method declaration (for the two programming languages).
The difference is that C# only overrides methods that contain theoverride keyword. On the other hand, JavaScript tries to override the class methods as soon as it inherits them, no override keyword needed. So, as an example
of class inheritance for JavaScript, we will have:
class Weapon extends Item
{
//Class members and declarations
}
The same code written in C# will be:
public class Weapon : Item
{
//Class members and declarations
}
That’s the main differences between the two languages and it will practically define all others, such as yielding code execution, accessing GameObjects and components, raycasting, etc. There are other differences such as the keyword used to import libraries
(“import” for JavaScript and “using” for C#) but these declaration and keyword differences are easily figured out.
So, if the reader understood these differences, all the other ones that will be explained on the next posts will make more sense.
One final last thing: even if you don’t have to declare the type of the variables and methods in JavaScript it is recommended that you do. This is due the fact that the compiler tries to figure out the type of variable to use, if it hasn’t been declared,
which may lead to performance issues. e.g.:
//do this:
private var score:int;
//instead of this:
private var score;
This wouldn’t be a issue if we were talking about code that runs every minute, but let’s not forget that games today runs practically at 60 fps, and Unity3D games are no exception. This way, add variable types whenever you can when programming JavaScript for
Unity3D.
In the next post, differences between C# and JavaScript will be shown when programming a script that access other GameObjects and Components. You can read it here:
第二節之物體對象及其組件屬性 GameObjects and Components.