查看: 1327|回复: 5
打印 上一主题 下一主题

unity3d中两种语言的对比JavaScript vs C# 第三节

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-11-8 15:35:00 |只看该作者 |倒序浏览


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

           系列教程的第三节,此节教程主要通过一段具体的功能代码来比较在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.
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-2-17 23:28:06 |只看该作者
不错啊 经典
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-4-4 23:21:12 |只看该作者
爱咋咋地!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-9-26 23:21:45 |只看该作者
读铁系缘分,顶铁系友情
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2013-2-28 23:32:00 |只看该作者
提醒猪猪,千万不能让你看见
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2013-3-2 23:58:59 |只看该作者
不错不错,收藏了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-24 09:09 , Processed in 0.072186 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部