unity3d中两种语言的对比JavaScript vs C# 第四节
2011-09-08 14:33
429 查看
系列教程的第四部分,主要对比了二种语言中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.
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.
相关文章推荐
- unity3d中两种语言的对比JavaScript vs C# 第五节
- unity3d中两种语言的对比JavaScript vs C#第一节
- Unity3d中两种语言的对比JavaScript vs C#第二节
- unity3d中两种语言的对比JavaScript vs C# 第三节
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- Unity3D中JavaScript与C#对比
- 复杂事件处理两种技术实现手段的对比,规则语言 VS 持续查询语言
- Unity3D中JavaScript与C#对比
- Boo Vs C# 语言对比 6
- 零废话VS2012 MVC3项目中javascript、C#实现图片上传
- C#中string 与stringbuilder的区别(大部分语言都有类似的这两种类型)
- JavaScript图表对比评测:FusionCharts vs HighCharts
- VS Code - Debugger for Chrome调试JavaScript的两种方式
- Unity3d-除了C#还需要学习什么语言?
- TypeScript VS JavaScript 深度对比
- javascript语言两种变量类型及存储方式
- 看书笔记——《Unity3d脚本编程使用C#语言开发跨平台游戏》——U3d游戏脚本中的值类型