Unity如何接收http请求?

How does Unity receive http request?

我想接受 http 请求来订购预制移动,那么如何在 unity 3D 中接收 http 请求?

如果您想在 Unity 应用程序中构建 Web 服务。

RESTful-Unity 是一个易于使用的插件。

  1. 定义api路由
RoutingManager routingManager = new RoutingManager();
routingManager.AddRoute(new Route(Route.Type.POST, "/path/to/call", "PrefabInvoke.Move"));
  1. 创建一个 Invoke 来响应请求
namespace RESTfulHTTPServer.src.invoker
{
    public class PrefabInvoke : MonoBehaviour
    {
        public static Response Move(Request request)
        {
            Response response = new Response();
            string responseData = "";
            string json = request.GetPOSTData();
            bool valid = true;

            UnityInvoker.ExecuteOnMainThread.Enqueue (() => {

                    Debug.Log(json);
                    try
                    {
                       //TODO: Parse Json string and do somthing

                        response.SetHTTPStatusCode((int)HttpStatusCode.OK);
                        responseData = "sucess message";
                    }
                    catch (Exception ex)
                    {
                        valid = false;
                        string msg = "failed to deseiralised JSON";
                        responseData = msg;
                    }
                    
            });

            // Wait for the main thread
            while (responseData.Equals("")) {}

            // Filling up the response with data
            if (valid) {

                // 200 - OK
                response.SetContent(responseData);
                response.SetHTTPStatusCode ((int)HttpStatusCode.OK);
                response.SetMimeType (Response.MIME_CONTENT_TYPE_JSON);
            } else {

                // 406 - Not acceptable
                response.SetContent("Somthing wrong");
                response.SetHTTPStatusCode((int) HttpStatusCode.NotAcceptable);
                response.SetMimeType(Response.MIME_CONTENT_TYPE_HTML);
            }

            return response;
        }
    }
}