产量和WWWForm错误

| 我一直在尝试将代码转换为(页面上的第二个示例):http://unity3d.com/support/documentation/ScriptReference/WWWForm.html ..to 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:“1ѭvoid\”的主体不是迭代器接口类型 阅读后,我尝试将void Start()更改为IEnumerator Start() ..但它表示未声明IEnumerator。 如果我注释掉yield命令,错误就会消失,但是当然不会加载数据。 请有人帮忙吗? 谢谢。     
已邀请:
        您需要更改
Start()
的返回类型,
Start
回调支持
void
IEnumerator
作为其返回类型。
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
关键字。 大多数回调使您返回return5ѭ,有些则不能:唤醒,更新,LateUpdate,FixedUpdate,OnGUI,OnEnable,OnDisable,OnDestroy。您将需要检查事件回调的文档,以查看它是否不支持作为例程。     
        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);
    }
}
    

要回复问题请先登录注册