unity3d中兩種語言的對比JavaScript vs C#第一節

來源:互聯網
上載者:User

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.