产量和 WWWForm 错误

发布于 2024-11-15 14:55:36 字数 1374 浏览 4 评论 0原文

我一直在尝试转换代码(页面上的第二个示例): 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 becausevoid' 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-11-22 14:55:36

您需要更改 Start() 的返回类型,Start 回调同时支持 voidIEnumerator,因为它是返回类型。

IEnumerator 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);
    }
}

一旦返回类型为 IEnumerator,您就可以使用 yield 关键字。

大多数回调允许您返回 IEnumerator,有些则不能返回:Awake、Update、LateUpdate、FixedUpdate、OnGUI、OnEnable、OnDisable、OnDestroy。您需要检查事件回调的文档,看看它是否不支持作为协同例程。

You need to change the return type of Start(), the Start callback supports both void and IEnumerator as it's return types.

IEnumerator 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);
    }
}

Once the return type is IEnumerator you are allowed to use the yield 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.

不寐倦长更 2024-11-22 14:55:36

yield 不能在 Start() 函数中使用,它需要在自己的线程中调用,而是尝试以下操作:

void Start()
{
    StartCoroutine(SaveScore());
}

IEnumerator SaveScore() 
{   
    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(!string.IsNullOrEmpty(downloadW.error)) {
        print( "Error downloading: " + downloadW.error );
    } else {
        // show the highscores
        Debug.Log(downloadW.text);
    }
}

yield can not be used in the Start() function, it needs to be called within its own thread, instead try this:

void Start()
{
    StartCoroutine(SaveScore());
}

IEnumerator SaveScore() 
{   
    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(!string.IsNullOrEmpty(downloadW.error)) {
        print( "Error downloading: " + downloadW.error );
    } else {
        // show the highscores
        Debug.Log(downloadW.text);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文