Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

WWW class

Discussion in 'Scripting' started by Sbach, Feb 17, 2011.

  1. Sbach

    Sbach

    Joined:
    May 18, 2010
    Posts:
    9
    Hello,

    I made those functions that call a php file and return data,
    but i'm unable to make the script wait for the request to end.

    So is there a way to make a www request without Coroutine or yield it ?
    Can we make a function in Csharp that make a www request and return the result (best: available using static call :p) ?

    Thanks.

    Code (csharp):
    1.     public IEnumerator MakeRequest(string HandlerFile ,Hashtable RequestParams)
    2.     {
    3.         WWWForm RequestForm = new WWWForm();
    4.        
    5.         Crypt NewCrypt = new Crypt(key,iv);
    6.        
    7.         foreach(DictionaryEntry Param in RequestParams)
    8.         {
    9.             RequestForm.AddField( Param.Key.ToString(),  NewCrypt.Encrypt(Param.Value.ToString()) );
    10.         }
    11.        
    12.         WWW w = new WWW(CoreUrl + HandlerFile, RequestForm);
    13.         yield return w;
    14.        
    15.         if (w.error != null)
    16.             { RequestedData = null; Debug.Log(w.error); }
    17.         else
    18.         {
    19.             RequestedData = NewCrypt.Decrypt(w.text);
    20.             w.Dispose();
    21.         }
    22.     }
    23.    
    24.    
    25.     public string Request(string Handler ,Hashtable RequestParams)
    26.     {
    27.         if(CoreHandlers.ContainsKey(Handler))
    28.         {
    29.             yield StartCoroutine(MakeRequest(CoreHandlers[Handler], RequestParams));
    30.             return RequestedData;
    31.         }
    32.         else
    33.         {
    34.             print("Error : Unknown Handler");
    35.             return null;
    36.         }
    37.     }
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    For sure, just store the WWW instance to your class and check it on the Update event:

    Code (csharp):
    1. public class MyClass
    2. {
    3.     private WWW Loader;
    4.    
    5.    
    6.     private void Start()
    7.     {
    8.         Loader = new WWW("url");
    9.     }
    10.    
    11.     private void Update()
    12.     {
    13.         if (Loader != null  Loader.isDone)
    14.         {
    15.             //do stuff
    16.            
    17.             Loader = null;
    18.         }
    19.     }
    20. }
    Just wrote that in notepad so it's probably not fully correct, but you should get the idea.
     
  3. Sbach

    Sbach

    Joined:
    May 18, 2010
    Posts:
    9
    I see, it works great but i found an other way with coroutines and callback for my main class.
    However i keep your method in mind for something else in my project.

    Thanks a lot.
     
  4. moonshield

    moonshield

    Joined:
    May 5, 2011
    Posts:
    1
    Could you please share the code with coroutines and callback, thank you