unity3d中两种语言的对比JavaScript vs C# 第三节
2011-09-08 14:19
302 查看
系列教程的第三节,此节教程主要通过一段具体的功能代码来比较在unity3d中所使用的两种程序语言的区别,下面是原文:
This is the third part of a series that show some of the differences between JavaScript and C# when writing scripts for Unity3D game engine. I suggest that you read the first and second post of the series to better understand what is going on here.
In this third part, I will point out some differences between JavaScript and C# by writing a script that makes a GameObject move forward. So, let’s start with the programming language that will take the smallest number of lines to make a GameObject move,
JavaScript:
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
function Update()
{
//moves the containing GameObject forward
goTransform.position.z = goTransform.position.z + vel;
}
This script moves the attached game object forward. Note that it is possible to access and increment the goTransform’s position z property, every update cycle and that causes the GameObject to move forward. Let’s see how the same code is written using C#:
using UnityEngine;
using System.Collections;
public class PawnMover : MonoBehaviour
{
public Transform goTransform;
private int 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
}
}
Here, C# can’t access the goTransform’s position z property, meaning that it can’t be incremented, like the JavaScript version of the above code. Trying to do it generates a CS1612 error, which means that we are trying to change
a value directly and not a reference to that value. To avoid this error when writing scripts with C#, we have to use methods to move GameObjects, such as Translate(), Rotate(), RotateAround(), etc. All these methods are public members of the Transform class.
That’s it for this post. I hope I have shed some light in the main differences between these two programming languages, when writing scripts for Unity3D game engine. Don’t forget to check 41 Post for the fourth part of this series, where I will show some
differences between JavaScript and C# when pausing code execution (a.k.a. yielding), which is available here: Part 4 – Yielding.
This is the third part of a series that show some of the differences between JavaScript and C# when writing scripts for Unity3D game engine. I suggest that you read the first and second post of the series to better understand what is going on here.
In this third part, I will point out some differences between JavaScript and C# by writing a script that makes a GameObject move forward. So, let’s start with the programming language that will take the smallest number of lines to make a GameObject move,
JavaScript:
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
function Update()
{
//moves the containing GameObject forward
goTransform.position.z = goTransform.position.z + vel;
}
This script moves the attached game object forward. Note that it is possible to access and increment the goTransform’s position z property, every update cycle and that causes the GameObject to move forward. Let’s see how the same code is written using C#:
using UnityEngine;
using System.Collections;
public class PawnMover : MonoBehaviour
{
public Transform goTransform;
private int 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
}
}
Here, C# can’t access the goTransform’s position z property, meaning that it can’t be incremented, like the JavaScript version of the above code. Trying to do it generates a CS1612 error, which means that we are trying to change
a value directly and not a reference to that value. To avoid this error when writing scripts with C#, we have to use methods to move GameObjects, such as Translate(), Rotate(), RotateAround(), etc. All these methods are public members of the Transform class.
That’s it for this post. I hope I have shed some light in the main differences between these two programming languages, when writing scripts for Unity3D game engine. Don’t forget to check 41 Post for the fourth part of this series, where I will show some
differences between JavaScript and C# when pausing code execution (a.k.a. yielding), which is available here: Part 4 – Yielding.
相关文章推荐
- unity3d中两种语言的对比JavaScript vs C# 第五节
- unity3d中两种语言的对比JavaScript vs C#第一节
- Unity3d中两种语言的对比JavaScript vs C#第二节
- unity3d中两种语言的对比JavaScript vs C# 第四节
- Unity3D中JavaScript与C#对比
- 复杂事件处理两种技术实现手段的对比,规则语言 VS 持续查询语言
- Unity3D中JavaScript与C#对比
- Boo Vs C# 语言对比 6
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- JavaScript—基于类 vs 基于原型的语言
- 学习经验谈:Unity3d开发中最佳语言还是C#
- 弱类型语言的优势:C#的委托概念在Javascript中的实现
- 前端和后台对时间数值的增减操作(JavaScript和C#两种方法)
- C#语言学习:变量的声明与初始化的范围(对比C++)
- c#语言与unity3D
- C++与C#对常用数据结构封装的对比(STL vs System.Collections.Generic)[转自柒笑侠]
- 利用ajax实现前台javascript同c#后台MVC下的aspx.cs文件传递数据的两种方法2