Wednesday, October 16, 2013

WWW web request manager in Unity3d -Part 1

Standard
Hello All:

In this series I will explain how to create a simple web request manager using WWW class provided by Unity3d. This manager can be used as common component for making all web requests in one place.

WWW class provided by Unity3d provides functionality to handle web requests for assets, text etc. You can read more here.While working with WWW class, coroutines are often used. You can learn about coroutines here.
Create a project in Unity3d. Now add a C# script and name it RequestManager. Copy below code in it
 
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
using System.Collections.Specialized;

public class RequestManager : MonoBehaviour
{
    private const int REQUEST_MAX_POST_TIME = 30;
    private Queue
<Request> requestQueue = new Queue<Request>();
    private bool queueIsProcessing = false;
    public static RequestManager Instance;

    public void Awake()
    {
        if (Instance != null)
        {
            Destroy(this.gameObject);
            return;
        }
        DontDestroyOnLoad(this.gameObject);
        Instance = this;
        requestQueue = new Queue<request>();
    }

    public void AddRequest(string data)
    {
        PostRequest(data);
    }

    private void PostRequest(string data)
    {
        Request request = new Request();
        request.RequestTme = Time.time;
        request.Data = data;
        requestQueue.Enqueue(request);
        StartCoroutine(ProcessQueue());
    }

    private IEnumerator ProcessQueue()
    {
        if (queueIsProcessing)
            yield break;

        queueIsProcessing = true;

        while (requestQueue.Count &gt; 0)
        {

            Request request = requestQueue.Peek();
            float postTime = Time.time;
            WWW www = new WWW("http://www.ashwanik.in/blog/blogpost" + request.Data);
            while (!www.isDone)
            {
                if (Time.time - postTime &gt; REQUEST_MAX_POST_TIME)
                    break;
                yield return null;
            }
            Debug.Log("Request finished " + www.text);
            requestQueue.Dequeue();
        }

        queueIsProcessing = false;
    }


    public class Request
    {
        public float RequestTme
        {
            get;
            set;
        }

        public string Data
        {
            get;
            set;
        }

    }

}

RequestManager class maintains a queue to hold request which are added by calling 

 

        RequestManager.Instance.AddRequest

Now we will create a test class to test our request manager. Add a C# script and name it RequestTester. Copy below code in it.
 
       using UnityEngine;
using System.Collections;

public class RequestTester : MonoBehaviour
{
    void OnGUI()
    {
        GUILayout.BeginArea(new Rect((Screen.width - 310) / 2, 200, 310, 1000));
        if (GUILayout.Button("Send Sample Request", GUILayout.Width(300), GUILayout.Height(20)))
        {
            RequestManager.Instance.AddRequest("?data=test");
        }

        GUILayout.EndArea();
    }


}

Attach these two scripts to Main Camera of the scene and save the scene. Now play the scene in the editor. By pressing "Send Sample Request" you can generate requests to test our RequestManager.

You can get the sample project here.

Hope this helps.
Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.

0 comments :

Post a Comment