纳金网

标题: unity3d中两种语言的对比JavaScript vs C#第一节 [打印本页]

作者: 会飞的鱼    时间: 2011-11-8 15:38
标题: unity3d中两种语言的对比JavaScript vs C#第一节


            
         

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

作者: tc    时间: 2012-2-26 23:27
呵呵,很漂亮啊

作者: tc    时间: 2012-3-20 23:25
很经典,很实用,学习了!

作者: C.R.CAN    时间: 2012-4-8 23:26
我是老实人,我来也!

作者: 晃晃    时间: 2012-5-1 23:18
呵呵,很好,方便罗。

作者: 菜刀吻电线    时间: 2012-5-11 23:20
顶!学习了!阅!

作者: 奇    时间: 2012-6-27 23:20
凡系斑竹滴话要听;凡系朋友滴帖要顶

作者: 奇    时间: 2012-7-1 23:27
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!!

作者: tc    时间: 2012-8-3 01:48
路过、路过、快到鸟,列位请继续...ing

作者: 晃晃    时间: 2012-8-24 23:24
楼主收集的可真全哦

作者: 奇    时间: 2012-11-14 04:06
好铁多多发,感激分享

作者: 晃晃    时间: 2013-2-16 23:39
我就看看,我不说话

作者: rabee    时间: 2014-5-23 09:21
对比这些有用吗?什么熟悉用什么~~
作者: Kadina    时间: 2014-7-2 08:10
Thank you for this share!




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5