微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

产量和WWWForm错误

如何解决产量和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\",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);
    }
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?