产量和 WWWForm 错误
我一直在尝试转换代码(页面上的第二个示例): http:// /unity3d.com/support/documentation/ScriptReference/WWWForm.html
..到 Unity3D 中的 C#:
void Start ()
{
string dataUrl = "http://www.my-site.com/game/test.php";
string playName = "Player 1";
int score = -1;
// Create a form object for sending high score data to the server
var form = new WWWForm();
// Assuming the perl script manages high scores for different games
form.AddField( "game", "MyGameName" );
// The name of the player submitting the scores
form.AddField( "playerName", playName );
// The score
form.AddField( "score", score );
// Create a download object
WWW downloadW = new WWW( dataUrl, form );
// Wait until the download is done
yield return downloadW;
if(downloadW.error == null) {
print( "Error downloading: " + downloadW.error );
return false;
} else {
// show the highscores
Debug.Log(downloadW.text);
}
}
我收到以下错误:
错误CS1624:
rr2game.Start()'的主体不能是迭代器块,因为
void'不是迭代器接口类型
做了一些阅读后,我尝试将 void Start() 更改为 IEnumerator Start() ..但它说 IEnumerator 未声明..?
如果我注释掉yield命令,错误就会消失,但数据当然不会加载。
请问有人可以帮忙吗? 谢谢。
I have been trying to convert the code at (the 2nd sample on the page): http://unity3d.com/support/documentation/ScriptReference/WWWForm.html
..to C# in Unity3D:
void Start ()
{
string dataUrl = "http://www.my-site.com/game/test.php";
string playName = "Player 1";
int score = -1;
// Create a form object for sending high score data to the server
var form = new WWWForm();
// Assuming the perl script manages high scores for different games
form.AddField( "game", "MyGameName" );
// The name of the player submitting the scores
form.AddField( "playerName", playName );
// The score
form.AddField( "score", score );
// Create a download object
WWW downloadW = new WWW( dataUrl, form );
// Wait until the download is done
yield return downloadW;
if(downloadW.error == null) {
print( "Error downloading: " + downloadW.error );
return false;
} else {
// show the highscores
Debug.Log(downloadW.text);
}
}
I get the following error:
error CS1624: The body of
rr2game.Start()' cannot be an iterator block because
void' is not an iterator interface type
After doing some reading I have tried changing void Start() to IEnumerator Start()
..but it says IEnumerator is is not declared..?
If I comment out the yield command the errors go away, but of course the data doesn't load.
Please can someone help?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要更改
Start()
的返回类型,Start
回调同时支持void
和IEnumerator
,因为它是返回类型。一旦返回类型为
IEnumerator
,您就可以使用yield
关键字。大多数回调允许您返回 IEnumerator,有些则不能返回:Awake、Update、LateUpdate、FixedUpdate、OnGUI、OnEnable、OnDisable、OnDestroy。您需要检查事件回调的文档,看看它是否不支持作为协同例程。
You need to change the return type of
Start()
, theStart
callback supports bothvoid
andIEnumerator
as it's return types.Once the return type is
IEnumerator
you are allowed to use theyield
keyword.Most callbacks let you return
IEnumerator
, some that can't are: Awake, Update, LateUpdate, FixedUpdate, OnGUI, OnEnable, OnDisable, OnDestroy. You will need to check the documentation of the event callback to see if it does not support being a co-routine.yield 不能在 Start() 函数中使用,它需要在自己的线程中调用,而是尝试以下操作:
yield can not be used in the Start() function, it needs to be called within its own thread, instead try this: