how to write C# helper function for Unity3d WWW class?

i am trying to write a wrapper class/function for www class, this is because the webserver will return compressed and encrypted string

this is my current attempt (plus minus)

public class WWWWrapper
{

public object output;

public IEnumerator getDoc<T>(string id)
{
    WWW www = new WWW(getdocurl + id.ToString());
    yield return www;

    if (string.IsNullOrEmpty(www.text)) yield return null;
        string compressed64 = www.text;
    byte[] compressed = System.Convert.FromBase64String(compressed64);       
    byte[] decompressed = CLZF2.Decompress(compressed);
    string decomps = System.Text.Encoding.UTF8.GetString(decompressed);
    output = JsonReader.Deserialize<T>(decomps);
    yield return null;
}}

and to use it:

yield return StartCoroutine (wr.getDoc<myclass>("E"));       
  myclass m = (myclass)(wr.output);

as u can see this is not a good solution. how do i make it simpler to use?

so that in the end i can write something like that:

myclass mc = wwwwrapper.getdoc<myclass> ("mydocid");

this is difficult due to the nature of yield statement, corourines and the fact that using

while (!www.isdone){}

to wait, is not working on webplayer (does work on pc, android per my testing and unity doc)

Thanks for your help!

Why have you declared your public variable as object? declare it as “T”. Also it’s usually better to use a public property :wink:

I would propably do something like this

public class WWWWrapper<T> : IEnumerator where T : class
{
    private T m_Result = null;
    private WWW m_WWW = null;
    private System.Action<T> m_Callback = null;

    public T Result
    {
        get
        {
            CheckIsDone();
            return m_Result;
        }
    }
    public bool IsDone
    {
        get { return CheckIsDone();}
    }
    
    public WWWWrapper(string id)
    {
        m_WWW = new WWW(getdocurl + id);
    }
    public WWWWrapper(string id, System.Action<T> aCallback)
    {
        m_WWW = new WWW(getdocurl + id);
        m_Callback = aCallback;
    }

    private bool CheckIsDone()
    {
        if (m_WWW == null)
            return true;
        if (m_WWW.isDone)
        {
            if (!string.IsNullOrEmpty(m_WWW.text))
            {
                PostProcess();
            }
            m_WWW = null;
            TriggerCallback();
            return true;
        }
        return false;
    }

    private void PostProcess()
    {
        string compressed64 = m_WWW.text;
        byte[] compressed = System.Convert.FromBase64String(compressed64);
        byte[] decompressed = CLZF2.Decompress(compressed);
        string decomps = System.Text.Encoding.UTF8.GetString(decompressed);
        m_Result = JsonReader.Deserialize<T>(decomps);
    }
    
    private void TriggerCallback()
    {
        if (m_Callback != null)
            m_Callback(m_Result);
    }
    
    #region IEnumerator implementation
    private int m_State = 0;
    public bool MoveNext ()
    {
        m_State++;
        if (m_State == 2)
            CheckIsDone();
        return m_State < 2;
    }
    
    public void Reset ()
    {
        throw new System.NotImplementedException ();
    }
    
    public object Current {
        get {
            return m_WWW;
        }
    }
    #endregion
}

I just wrote that from scratch. Haven’t tested it yet, but it should work :wink:

edit changed the class. Now it compiles at last (except your external dependencies like “getdocurl”, “CLZF2”, “JsonReader”). Still haven’t tested it :wink:

The wrapper can be used like this:

// C#
var www = new WWWWrapper<myclass>("E");
yield return StartCoroutine(www);
myclass C = www.Result;
if (C != null)
{
    //success ;)
}

Another way would be:

// C#
StartCoroutine(new WWWWrapper<myclass>("E", (myclass C) =>{
    if (C != null)
    {
        //success ;)
    }
}));