团结GET / POST包装 [英] Unity GET/POST Wrapper

查看:274
本文介绍了团结GET / POST包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Unity3d在C#中的问题。我们的目标是创建一个对象,这样我可以在URL中传递和接收通过 GET 的对象,我会创造将是WWW逻辑的包装数据。我也想一个POST的对象也一样,在那里我可以提供一个网址和一个键 - 值对作为后arguements的词典。 SOOO ......我们最终想是这样的:

This is a Unity3d in C# question. The goal is to create an object such that I can pass in a URL and receive data via GET, an object that I would create the would be a wrapper for the WWW logic. I would also like a 'POST' object too, where I could supply a url and a 'Dictionary' of key-value pairs as the post arguements. Sooo... we ultimately would like something like this:

get_data = GET.request("http://www.someurl.com/somefile.php?somevariable=somevalue");

post_data = POST.request("http://www.someurl.com/somefile.php", post)
// Where post is a Dictionary of key-value pairs of my post arguments. 

要尝试做到这一点,我使用了 WWW 对象。现在,为了给 WWW 对象的时间来下载,我们需要有一个 MonoBehaviour 对象这里面发生的事情, 收益的结果。所以,我得到了这一点,这作品:

To try and accomplish this, I use the WWW object. Now, in order to give the WWW object time to download, we need to have this happening inside a MonoBehaviour object and yield the results. So I got this, which works:

public class main : MonoBehavior
{
    IEnumerator Start()
    {
        WWW www = new WWW("http://www.someurl.com/blah.php?action=awesome_stuff"); 
        yield return www;
        Debug.Log(www.text);
    }
}



我真正想要的是这样的:

What I really want is this:

public class main : MonoBehavior
{
    IEnumerator Start()
    {
        GET request = new GET("http://www.someurl.com/blah.php?action=awesome_stuff"); 
        Debug.Log(request.get_data()); // Where get_data() returns the data (which will be text) from the request.   
    }
}

现在我已经连接到单一主脚本游戏对象在层次结构(称为根)。我是否需要连接到根游戏对象以及在 GET 脚本?我可以这样做动态地从

Now I have the main script attached to the single GameObject in the hierarchy (called root). Do I need to have the GET script attached to the root GameObject as well? Can I do that dynamically from main?

最后,我需要一个解决方案,可以让我轻松地发送 GET POST 请求。

Ultimately, I need a solution that allows me to easily send GET and POST requests.

干杯!

推荐答案

嗯,知道了!

我的问题是一个误解是如何MonoBehaviour和协同程序的工作。该解决方案是非常简单的。

My problem was a misunderstanding of how MonoBehaviour and Coroutines worked. The solution is very simple.

在编辑器中,创建一个空游戏物体。我把它命名为DB。然后附上下面的脚本是:

In the editor, make an empty GameObject. I named it DB. Then attach the following script to it:

using System; 
using UnityEngine;
using System.Collections; 
using System.Collections.Generic; 
class DB : MonoBehaviour
{   

void Start(){}

    public WWW GET(string url)
    {

    WWW www = new WWW (url);
    StartCoroutine (WaitForRequest (www));
    return www; 
    }

    public WWW POST(string url, Dictionary<string,string> post)
    {
    WWWForm form = new WWWForm();
    foreach(KeyValuePair<String,String> post_arg in post)
    {
       form.AddField(post_arg.Key, post_arg.Value);
    }
        WWW www = new WWW(url, form);

        StartCoroutine(WaitForRequest(www));
    return www; 
    }

    private IEnumerator WaitForRequest(WWW www)
    {
        yield return www;

        // check for errors
        if (www.error == null)
        {
            Debug.Log("WWW Ok!: " + www.text);
        } else {
            Debug.Log("WWW Error: "+ www.error);
        }    
    }
}



然后,在你的主脚本启动功能,你可以做到这一点。

Then, in your main script's start function, you can do this!

private DB db; 
void Start () 
{
    db = GameObject.Find("DB").GetComponentInChildren<DB>();
    results = db.GET("http://www.somesite.com/someAPI.php?someaction=AWESOME");
    Debug.Log(results.text);
}



没有测试POST请求,但现在所有的逻辑都包裹起来!发送HTTP请求到你心中的愿望,干杯!

Haven't tested the POST requests, but now all the logic is wrapped up! Send HTTP requests to your hearts desire, cheers!

这篇关于团结GET / POST包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆