12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 3140|回复: 10
打印 上一主题 下一主题

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

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

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

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


           系列教程的第四部分,主要对比了二种语言中yielding的使用,下面是原文:
         

           This is the 4th post of a series that tries to explain the main differences between JavaScript and C# when programming for unity3d. In this post, some differences between yielding (pausing) code execution in these two programming languages will be pointed out. Before continuing, it is highly recommended that you read the first, second and third other posts of the series.
         

           As explained before, yielding pauses the code execution, being very useful to game programming, as you have a better control when things will happen in your game. Whether using C# or JavaScript in Unity3D, one can’t simply yield the Update() method. There is a workaround for that, but as you might have guessed, they are different for the two programming languages we are discussing. Since these workarounds are often used, they will be the examples presented on this post. Let’s start by seeing how to yield a JavaScript code:
         

           private var textS:String;
         

           private var counter:float = 0;
         

           function Update ()
         

           {
         

           //call the function that waits three seconds to execute
         

           WaitThreeSeconds();
         

           }
         

           //This function waits three seconds before executing
         

           function WaitThreeSeconds()
         

           {
         

           //Waits three seconds
         

           yield WaitForSeconds(3);
         

           //converts the counter to a String and stores its value in textS
         

           textS = "Three seconds has passed, now I'm counting..."+counter.ToString();
         

           //add one to the counter
         

           ++counter;
         

           }
         

           /*This function is responsible for outputting text to the
         

              screen and rendering other GUI elements*/
         

           function OnGUI()
         

           {
         

           //The next line displays the first line of text
         

           GUI.Label(new Rect(20,20,250,20), "I will start counting in 3 seconds.");
         

           //Displays the counter
         

           GUI.Label(new Rect(20,40,500,20),textS);
         

           }
         

           This code outputs two lines of text to the screen. The first one is rendered as soon as the code starts running; the second line only appears after three seconds has passed. The code yields only once, and its execution continues normally inside the Update() loop (meaning it doesn’t wait 3 seconds to execute the next time WaitThreeSeconds() is called). Note that we had to put the code we wanted to yield inside a function, because the Update() method can’t be paused.
         

           This is how the same code is written using C#:
         

           using UnityEngine;
         

           using System.Collections;
         

           public class Yield : MonoBehaviour
         

           {
         

           private string textS;
         

           private float counter = 0;
         

           void Update ()
         

           {
         

           //doesn't generate an error but doesn't work either.
         

           WaitThreeSeconds();//<=does nothing
         

           //start the coroutine named WaitThreeSeconds
         

           StartCoroutine("WaitThreeSeconds");//<=right
         

           }
         

           //Waits three seconds before executing
         

           IEnumerator WaitThreeSeconds()
         

           {
         

           //Waits three seconds
         

           yield return new WaitForSeconds(3);
         

           //converts the counter to a String and stores its value in textS
         

           textS = "Three seconds has passed, now I'm counting..."+counter.ToString();
         

           //add one to the counter
         

           ++counter;
         

           }
         

           /*This function is responsible for outputting text to the
         

           screen and rendering other GUI elements*/
         

           void OnGUI()
         

           {
         

           //The next line displays the first line of text
         

           GUI.Label(new Rect(20,20,250,20), "I will start counting in 3 seconds.");
         

           //Displays the counter
         

           GUI.Label(new Rect(20,40,500,20),textS);
         

           }
         

           }
         

           The main difference here is that we can’t put a yield inside a function using C#. We are forced to use the IEnumerator interface, because that’s just the way C# was designed (more information on the IEnumerator interface can be found here). And we can’t just call WaitThreeSeconds() as a normal function, if we do that, nothing will happen. So, the solution was to call WaitThreeSeconds() as a coroutine (line 14).
         

           Another difference is in line 21, where, instead of writing yield WaitForSeconds(3), we had to write: yield return new WaitForSeconds(3). That is necessary since we are using the IEnumerator interface which requires a return.
         

           That’s it for this post! I hope it was clear enough. For the next post of this series, I will show some differences when using the Raycast class in JavaScript and C#. Read it here: Part 5 – Raycasting.
         

           原文:http://www.41post.com/1855/programming/unity3d-javascript-vs-csharp-4
           

           来源: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%E5%9B%9B%E8%8A%82
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

沙发
发表于 2012-1-19 23:00:47 |只看该作者
年末感慨实在是多,三言两语道不完!最让我揪心的还是你,行李备好了没?火车票买了没?别感动,我只是问问,自己的事情还是要自己做滴!哈哈。
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

板凳
发表于 2012-1-23 23:26:43 |只看该作者
健康是最佳的礼物,知足是最大的财富,信心是最好的品德,关心是最真挚的问候,牵挂是最无私的思念,祝福是最美好的话语。祝你新年快乐!平安幸福!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

地板
发表于 2012-2-28 23:22:14 |只看该作者
真是不错啊
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

5#
发表于 2012-3-11 23:26:20 |只看该作者
很有心,部分已收录自用,谢谢
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

6#
发表于 2012-3-12 23:19:43 |只看该作者
真是不错啊
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

7#
发表于 2012-3-30 23:18:45 |只看该作者
不错哦,谢谢楼主
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

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

8#
发表于 2012-7-12 23:18:38 |只看该作者
不会吧,太恐怖了
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

9#
发表于 2012-7-21 23:27:12 |只看该作者
很经典,很实用,学习了!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

10#
发表于 2012-8-10 00:01:42 |只看该作者
不会吧,太恐怖了
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-1 07:33 , Processed in 0.095784 second(s), 29 queries .

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

© 2008-2019 Narkii Inc.

回顶部