unity3d中两种语言的对比JavaScript vs C# 第三节
来源:互联网 发布:java全景图片拼接 编辑:程序博客网 时间:2024/05/22 07:06
系列教程的第三节,此节教程主要通过一段具体的功能代码来比较在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.
- unity3d中两种语言的对比JavaScript vs C# 第三节
- unity3d中两种语言的对比JavaScript vs C# 第五节
- unity3d中两种语言的对比JavaScript vs C#第一节
- Unity3d中两种语言的对比JavaScript vs C#第二节
- unity3d中两种语言的对比JavaScript vs C# 第四节
- C#的第三节课
- javascript-第三节-数组
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- c语言第三节课
- 第三节:学习的方向以及编程语言
- 第三节Node编程语言的基本语法(一)
- Cocos2d-x的学习 触碰事件老版本和新版本的对比 第三节
- 学习javascript基础知识系列第三节 - ()()用法
- 2014年辛星Javascript解读第三节
- javascript入门基础篇重点 第三节
- 简明 Vim 练级攻略
- 个人收藏站点导航
- Android ndk 开发环境搭建
- 在职场中如果低调做人!
- Android学习启动篇
- unity3d中两种语言的对比JavaScript vs C# 第三节
- 最后一任女朋友应该是这样的
- 剖析程序的内存布局
- ubuntu下的常用命令(1)
- 优秀人才为什么总想跳槽?
- web.xml中listener作用及使用
- 我是你的过客,你却是我的定格......
- 复习MySQL数据库
- SQL SERVER 两张结构一致的table比较