现在的位置: 首页 > 综合 > 正文

unity3d中两种语言的对比JavaScript vs C#第一节

2013年09月13日 ⁄ 综合 ⁄ 共 2874字 ⁄ 字号 评论关闭

 

在使用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.

抱歉!评论已关闭.