Unity3D中JavaScript与C#对比

第一节

     在使用unity3d进行游戏开发或者互动项目制作的时候,有3种编辑语言可供使用,不过用的最多的应该是javascript和c#两种语言,相信很多朋友特别是新手都会很纠结于到底该用二者种的哪一个比较好,下面是老外对unity3d中这两种语言进行的一个整体有效的对比,此为第一章节,主要对比二者在变量,类,函数等常规基础要点上的异同!(个人翻译,水平有限,希望大家谅解,O(∩_∩)O哈哈~) 
下面是正文: 
    这是本系列的第一章,本章主要讨论在Unity3D引擎中使用JavaScript和C#编程是有什么不同。所有在文章里用到的脚本都可以在本系列的最后一章中下载到,那么现在我们就来看看这两种语言之间到底有什么不同。     第 一个也是最容易区分的一点就是声明变量和方法。在JavaScript编程中,不用像C#中一样,必须声明变量或方法类型,因为JavaScript是一 种弱类型语言,在编译时他会选择最合适的类型给予定义。但是C#完全不同,它属于强类型语言,变量或方法一定要在之前定义好。或许看些例子可以让我们更好 的理解这个概念。下面是一个JavaScript的脚本: 

private var cubeTransform;  

在C#中,同样的代码则会是: 

private Transform cubeTransform;  

这对方法同样适用,在C#中,一个方法什么值也没有返回,那么他的返回值为 void 类型,但是在JavaScript中则可以省略。     类 的继承也是不同的。在JavaScript和C#中,方法是隐型并且不可重载,除非方法声明中添加虚拟关键字。不同的是C#只重载那些包含重载关键字的方 法。而JavaScript不需要关键词,只要重载类方法就可继承他们。我们来看一个JavaScript类继承的例子: 

class Weapon extends Item  {       
   //Class members and declarations 
 }  


在C#中,同样的代码则会是: 
public class Weapon : Item  {     
       //Class members and declarations 
} 

 
     这 就是这两种代码的主要区别,实际上他需要定义全部的东西,像执行产出代码,访问GameObject和组件,激光投射等等。还有一些其他的不同点,比如导 入函数库的关键字(在JavaScript中用“Import”,在C#中使用“using”),但是这些声明和关键字的不同就比较容易明白了。     所以,如果读者可以理解这些不同点,那在下面的章节中的解释也会更加容易理解。
最后提一下:在JavaScript中,即使不必要声明变量和方法类型,但是劝你最好还是定义一下。虽说编译器会试图找出你变量的类型,但是如果你没有声明,那就有可能会出现一些问题,比如:
 
//do this:
private var score : int; //instead of this: private var score;  


    虽说每分钟都不停地读代码不是什么问题,但是也不要忘了,现在游戏每秒钟运行60帧,当然Unity3D也不例外,这样的话,当你的JavaScript脚本运行时就会无时无刻不在给Unity3D增加变量类型。(译者:相信这不是你所希望的结果吧...)
    在下一章节中,将会说一下C#与JavaScript在编程时访问GameObject和组件会有什么不同。

  

第二节

     本系列的第二节也会来给你解释一下JavaScript 和C#在Unity3D中编程有什么不同,建议你先看第一部分的内容。在这节中,我会给你解释一下如何访问GameObject和组件,这是一个程序员在编写Unity3D脚本时最常见的一个事。那么现在让我们先创建一个名称为“Pawn”的GameObject,并且给他附上“PawnMover”脚本。 
    使用JavaScript来获取GameObject很简单,你只需要调用Find()静态方法,并把想要的GameObject的名称作为参数:

private var pawnGO:GameObject;   function Awake()  {      
     pawnGO = GameObject.Find("Pawn");  
}  

 用C#也很相似:

using UnityEngine;   
using System.Collections;     
public class PawnGetter : MonoBehaviour {      
    private GameObject pawnGO;         
    void Awake () {          
         pawnGO = GameObject.Find("Pawn");          
    }  
} 

在不考虑两种语言的关键字和格式不同的情况下,代码是完全一样的(第一个代码的第四行和第二个代码的第八行是相同的)。不管代码是强类型还是弱类型,GameObject.Find()方法总是会返回一个GameObject值。 
    现在,让我们看看如何获得一个GameObject上的组件。假设“PawnMover”组件赋给“Pawn”GameObject,让我们来看看如何使用JavaScript获得“PawnMover”组件:

private var pawnGO:GameObject;   
private var pmSC:PawnMover;   
function Awake()  {       
    pawnGO = GameObject.Find("Pawn");          
    pmSC = pawnGO.GetComponent("PawnMover");  
 }

基 本上,要获得“PawnMover”组件,我们所需要做的就是从
“Pawn”GameObject调用GetComponent()方法,并把所需组件的 名称作为参数。除了名称,我们也可以通过组件类型作为参数,但是像上面的例子我们用名字就行了。因为JavaScript是弱类型,返回值为组件,我们不 需要把组件给PawnMover类作为结果。在C#中也是一样的:

using UnityEngine;   
using System.Collections;     
public class PawnGetter : MonoBehaviour  {      
    private GameObject pawnGO;       
    private PawnMover pmSC;        
    void Awake(){       
        pawnGO = GameObject.Find("Pawn");  //returns a CS0266 error    
    pmSC = pawnGO.GetComponent("PawnMover");//<=returns a CS0266 error           
    //this is the right way to do it when using C#             
    pmSC = pawnGO.GetComponent< PawnMover >();   
   }   
} 

 
用 C#就不可能只是调用GetComponent()方法并把该组件的名称作为参数


  
第三节


     这系列第三节,解释JavaScript和C#在Unity3d游戏引擎中编程时有什么不同。建议你最好先去阅读第一和第二节,方便理解这节内容。
 
在第三节中,主要讲解用JavaScript和C#让一个GameObject向前移动有什么不同。现在我们来看一段使用JavaScript让一个GameObject向前移动的代码:
 
public var goTransform : Transform;
private var vel : int = 2;//how fast the game object is being moved 
function Awake()   {
//get this GameObject's Transform
goTransform = this.GetComponent(Transform);  } 
// Update is called once per frame functionUpdate()   {
//moves the containing GameObject forward
goTransform.position.z= goTransform.position.z+ vel; }  


把这个脚本的目的是在每一个更新周期使goTransform的z轴坐标增加来控制让goTransform向前移动的,现在让我们看看用C#编写代码会是什么样的: 
using UnityEngine;  
usingSystem.Collections;  
public classPawnMover : MonoBehaviour { 
public Transform goTransform;  
privateint vel = 2;//how fast the game object is being moved 
void Awake() {  
            //get this GameObject's Transform
goTransform = this.GetComponent<Transform>();   }     
// Update is called once per frame void Update()   {  
//returns a CS1612 error
goTransform.position.z=goTransform.position.z+ vel;//<=returns a CS1612 error //this is the right way to do it when using C#
goTransform.Translate(Vector3.forward * vel);//moves the containing GameObject forward  } }   


这 里我们可以看到,在C#中不能像JavaScript脚本中那样直接改变goTransform的z轴坐标值来移动,这样会产生CS1612error, 因为我们是要改变一个值而不是引用该值。为了避免这个错误,在用C#编写脚本时,我们用方法移动
GameObejct,比如Translate()、 Rotate()、 RotateAround()等等。这些方法都是Transform类得公共成员变量。
这节我希望我已经清楚的解释了两种语言在Untiy3D中编程时有什么不同,不要忘了第四章,我会告诉你JavaScript和C#执行暂停代码有什么不同。
  
Unity3D中JavaScript与C#对比 ---- 第四节

 

   第四节

主要讲JavaScript和C#在Unity3D编程中执行yielding(pausing)有什么区别,在继续之前,强烈建议您阅读第一、第二和第三节的内容。

    yielding pauses代码对于游戏编程是相当有用的,你可以用它更好的控制你游
戏中的事件。无论是用JavaScript或C#都不能简单的中断Update()方法。相信你已经猜到了,我们今天要议论的是这两种语言对此的解决方案有什么不同。我们这节中会给出这种解决方案的常用实例。现在我们来看看JavaScript是如何yield的:  

private vartextS : String;  
private varcounter : float = 0;   functionUpdate ()   {  
//call the function that waits three seconds to execute       WaitThreeSeconds();   }    
//This function waits three seconds before executing   functionWaitThreeSeconds()  {  
//Waits three seconds   yield WaitForSeconds(3); 
//converts the counter to a String and stores its value in textS  
textS = "Three seconds has passed, now I'm counting..."+counter.ToString();  //add one to the counter       ++counter;   }     
function OnGUI()  {  
//The next line displays the first line of text  
GUI.Label(new Rect(20,20,250,20), "I will start counting in 3 seconds.");  //Displays the counter  
GUI.Label(new Rect(20,40,500,20),textS);  }   


     这个代码在屏幕上输出了两个文本,第一行会在开始不久就被渲染出来,第二行只会在暂停3秒后出现,yield代码只会执行一次,然后再正常执行Update()循环(也就是说不会再等三秒在去执行一次WaitThreeSeconds())。有一点要注意,我们知道能在函数内用yield,Update()方法是不能被暂停的。     现在来看看C#代码:  
using UnityEngine; 
using System.Collections;  
public class Yield : MonoBehaviour  {  
privatestring textS; 
private float counter = 0;   voidUpdate ()      {  
//doesn't generate an error but doesn't work either.  WaitThreeSeconds();//<=does nothing  
//start the coroutine named WaitThreeSeconds   StartCoroutine("WaitThreeSeconds");//<=right     }  
//Waits three seconds before executing       IEnumerator WaitThreeSeconds()       {  
//Waits three seconds  
yield return newWaitForSeconds(3); 
//converts the counter to a String and stores its value in textS 
textS = "Three seconds has passed, now I'm counting..." + counter.ToString();  //add one to the counter           ++counter;       }         
void OnGUI()      {  
//The next line displays the first line of text  
GUI.Label(newRect(20,20,250,20), "I will start counting in 3 seconds.");  //Displays the counter  
GUI.Label(new Rect(20,40,500,20),textS);      }   }     


    主要区别就是我们在C#中不能把yield放在函数中使用,需要使用
IEnumerator接口,之所以这样只是因为C#就是这么设计的(关于IEnumerator接口的更多信息可以在
http://www.devarticles.com/c/a/C-Sharp/Interface-IEnumerable-and-IEnumerator-in-C-sharp/2/这里找到)我们不能像调用普通函数那样调用WaitThreeSeconds(),即便那么做也不会有任何反应的。所以我们的解决办法就是想调用协同那样调用WaitThreeSeconds()。(例如14行)
    另外一个区别就是在21行,我们要用yield return new WaitForSeconds(3)来替换 yield WaitForSeconds(3)代码,因为我们用IEnumerator接口需要一个返回值。
    希望这节我解释的足够明白了。下一节我会像向你们展示在JavaScript 和C#中使用Raycast类得区别。

猜你喜欢

转载自blog.csdn.net/dangnianmingyue_gg/article/details/50602832