查看: 1973|回复: 7
打印 上一主题 下一主题

unity3d中两种语言的对比JavaScript vs C# 第五节(类的使用)

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

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



          系列教程的第五节,也是最后一节,此节教程主要通过类的使用来对比二者的区别。原文如下:
         

          As the end of the year draws close, so is this post series. Here, the differences between JavaScript and C# when casting a ray in unity3d will be pointed out. Don’t forget to read the first, second, third and forth parts of the series for a better general understanding of what is being discussed here.
         


           Let’s start from the basics: What is ray casting? As the name describes, it is basically a program that simulates a ray being cast, much like a laser pointer in real life. It is very useful for game programming, as Raycast classes are programmed to return the distance a ray collided with something (and sometimes, even the name of the object). Unity3D doesn’t have one single Raycast class, instead its functionality is scattered across the Physics, RaycastHit and Ray classes.
           

            
         

           I will explain how to cast a ray in Unity3D with a JavaScript example:
           

            
           

           来源:http://www.unity3d8.com/content/unity3d%E4%B8%AD%E4%B8%A4%E7%A7%8D%E8%AF%AD%E8%A8%80%E7%9A%84%E5%AF%B9%E6%AF%94javascript-vs-c-%E7%AC%AC%E4%BA%94%E8%8A%82%E7%B1%BB%E7%9A%84%E4%BD%BF%E7%94%A8
         

           //Creates a ray object
         

           private var ray : Ray;
           

            
         

           //creates a RaycastHit, to query informarion about the objects that are colliding with the ray
         

           private var hit : RaycastHit = new RaycastHit();
           

            
         

           //Get this GameObject's transform
         

           private var capsTrans:Transform;
           

            
         

           function Awake()
         

           {
         

           //get this Transform
         

           capsTrans = this.GetComponent(Transform);
         

           }
           

            
         

           // Update is called once per frame
         

           function Update ()
         

           {
         

           //recreate the ray every frame
         

           ray = new Ray(capsTrans.position, Vector3.left);
           

            
         

           //Casts a ray to see if something have hit it
         

           if(Physics.Raycast(ray.origin,ray.direction, hit, 10))//cast the ray 10 units in distance
         

           {
         

           //Collision has happened, print the distance and name of the object into the console
         

           Debug.Log(hit.collider.name);
         

           Debug.Log(hit.distance);
         

           }
         

           else
         

           {
         

           //the ray isn't colliding with anything
         

           Debug.Log("none")
         

           }
         

           }
           

            
         

           This is how it works: first, we need to create a Ray object and recreate it every frame (lines 02 and 20). The Ray class stores the ray properties, such as direction the ray is being cast and origin. Then, to obtain information about ray’s collision with other GameObjects, we need a RaycastHit class object, which will return the Collider and Transform from the GameObject that collided with the ray, distance which the collision occurred and some other details.
           

            
         

           Last but not least, we need to call the Raycast() static method from the Physics class (line 23). This is the method that will actually cast the ray. Note that it takes the origin of the ray, a direction to cast the ray, a RaycastHit object and distance as parameters. What this method does is cast the ray to the given distance and store the collision results passed inside the RaycastHit object.
           

            
         

           In the beginning, it may sound a little bit confusing, but after programming it for the first time it will make more sense (additional information here). In C#, the same above example is the following code:
           

            
         

           using UnityEngine;
         

           using System.Collections;
           

            
         

           public class Raycast : MonoBehaviour
         

           {
         

           //Creates a ray object
         

           private Ray ray;
           

            
         

           //creates a RaycastHit, to query informarion about the objects that are colliding with the ray
         

           private RaycastHit hit = new RaycastHit();
           

            
         

           //Get this GameObject's transform
         

           private Transform capsTrans;
           

            
         

           void Awake()
         

           {
         

           //get this Transform
         

           capsTrans = this.GetComponent<Transform>();
         

           }
           

            
         

           // Update is called once per frame
         

           void Update ()
         

           {
         

           //recreate the ray every frame
         

           ray = new Ray(capsTrans.position, Vector3.left);
           

            
         

           //Casts a ray to see if something have hit it
         

           if(Physics.Raycast(ray.origin, ray.direction, out hit, 10))//cast the ray 10 units in distance
         

           {
         

           //Collision has happened, print the distance and name of the object into the console
         

           Debug.Log(hit.collider.name);
         

           Debug.Log(hit.distance);
         

           }
         

           else
         

           {
         

           //the ray isn't colliding with anything
         

           Debug.Log("none")
         

           }
         

           }
         

           }
           

            
         

           When casting rays, the only difference between these two programming languages is at line 28 in the above code. See how we had to pass the hit object with the out keyword? That happens because, in C#, the Raycast() method says it needs the RaycastHit parameter passed as reference and that’s exactly what the out keyword does. So instead of passing the object (as we done in JavaScript), we are passing a reference to it, the place where it is so the Raycast() method can find it (more information here).
           

            
         

           It may not look like much, however one can spend some time trying to figure that out, specially when migrating from JavaScript to C#. And that’s basically it! These are the main differences between those two programming languages. There must be a lot more out there!
           

            
         

           Final Thoughts
         

           JavaScript or C# ? Which one to choose? It depends heavily on a lot of factors, such as previous programming skills, previous game programming experiences, affinity with certain programming languages, just to name of few.
           

            
         

           Nevertheless, people that are beginning to grasp the basic concepts of programming, or just started programming games recently or are new to Unity3D, should stick with JavaScript, because they will have less to worry about. These people will eventually migrate to C#, due to it’s internal classes and s***ctures which can really help to solve complex programming problems.
           

            
         

           For the folks who have some programming baggage with C#, C++ or Java and some experience with game programming in these languages: go directly to C#.
           

            
         

           In the end, this won’t matter after all. Eventually, you will end up learning both programming languages when writing code for Unity3D, meaning that this won’t be an issue. Naturally, you will end up creating scripts in these two languages, and then converting the code to the one the rest of your project is using. One thing is for certain: try not to mix use these two programming languages at the same time. Doing everything with one scripting language will avoid a lot of headaches.
           

            
         

           One last thing: I haven’t forgotten the project with the examples as promised in the first post of the series. Grab it here:
           

            
         


            JSvsCSharp.zip
           
            Read ins***ctions at the README.txt file.
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

沙发
发表于 2012-1-20 23:22:44 |只看该作者
林志玲,娇滴滴,祝你新年一定要好好的啦!小沈阳,笑嘻嘻,祝你新年一定要杠杠的哈!春晚群星,闹哄哄的,祝你新年辞旧迎新开创新纪元!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

板凳
发表于 2012-2-8 23:22:20 |只看该作者
再看一看,再顶楼主
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

地板
发表于 2012-2-23 23:20:48 |只看该作者
爱咋咋地!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

5#
发表于 2012-3-7 23:25:51 |只看该作者
沙发不解释
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

6#
发表于 2012-7-3 23:23:09 |只看该作者
“再次路过……”我造一个-----特别路过
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

7#
发表于 2013-2-18 23:23:04 |只看该作者
我就看看,我不说话
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

8#
发表于 2013-3-16 23:24:35 |只看该作者
发了那么多,我都不知道该用哪个给你回帖了,呵呵
回复

使用道具 举报

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

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

GMT+8, 2025-7-23 04:37 , Processed in 0.067592 second(s), 29 queries .

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

© 2008-2019 Narkii Inc.

回顶部