纳金网

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

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


           第三节(让物体移动的代码异同)
         

           系列教程的第三节,此节教程主要通过一段具体的功能代码来比较在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***cution (a.k.a. yielding), which is available here: Part 4 – Yielding.
         

作者: 菜刀吻电线    时间: 2012-2-17 23:28
不错啊 经典

作者: 菜刀吻电线    时间: 2012-4-4 23:21
爱咋咋地!

作者: 菜刀吻电线    时间: 2012-9-26 23:21
读铁系缘分,顶铁系友情

作者: 奇    时间: 2013-2-28 23:32
提醒猪猪,千万不能让你看见

作者: tc    时间: 2013-3-2 23:58
不错不错,收藏了





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