Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SneeKeeFahk · Jan 25, 2015 at 06:49 AM · c#www

Having issues with WWW and waiting for response.

Hey All,

I am struggling with doing a simple web request and waiting for the response. My game just locks up and I have to kill it via the task manager. I am very new to Unity but am very familiar with .NET so i think thats helping the frustration which is blinding me to what im assuming is a very simple solution.

Here is what i have so far (after several failed attempts so please excuse my messy code):

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class WebFunctions : MonoBehaviour
 {
 
     private bool hasResponse;
     private WWW responseObject; 
     
     private string currentURI;
     private WWWForm currentForm;
     
     IEnumerator SendGetRequest()
     {
         hasResponse = false;
         WWW w = new WWW(currentURI);
         
         yield return w;
         
         responseObject = w;
         hasResponse = true;
     }
         
     IEnumerator SendPostRequest()
     {
         hasResponse = false;
         WWW w = new WWW(currentURI, currentForm);
             
         yield return w;
                 
         responseObject = w;
         hasResponse = true;
     }
     
     public WWW Post(string URI, WWWForm form)
     {
         WaitForSeconds n;
         currentURI = URI;
         currentForm = form;
         StartCoroutine(SendPostRequest());
         
         while (!hasResponse)
             n = new WaitForSeconds(0.1f);
 
         return responseObject;
     }
 
     public WWW Get(string URI)
     {
         WaitForSeconds n;
         currentURI = URI;
         StartCoroutine(SendGetRequest());
         while (!hasResponse)
             n = new WaitForSeconds(0.1f);
 
         return responseObject;
     }
 }

Any and all help would be appreciated.

EDIT: With the help of Chris i managed to get it to work the way i wanted, here is the solution:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class WebFunctions
 {
     public static WWW Get(string url)
     {
         
         WWW www = new WWW (url);
 
         WaitForSeconds w;
         while (!www.isDone)
             w = new WaitForSeconds(0.1f);
 
         return www; 
     }
     
     public static WWW Post(string url, Dictionary<string,string> post)
     {
         WWWForm form = new WWWForm();
         foreach(var pair in post)
             form.AddField(pair.Key, pair.Value);
 
         WWW www = new WWW(url, form);
 
         WaitForSeconds w;
         while (!www.isDone)
             w = new WaitForSeconds(0.1f);
 
         return www; 
     }
     
 }

And you simply call it like this from your code & it will wait for a response before executing your next line of code:

 var response = WebFunctions.Get("http://www.google.com");
 // right here you can access response.text or any other property
 SomeRandomTextComponent.text = response.text;

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PPSKE · May 03, 2017 at 10:14 AM 0
Share

Your WebFunctions works like a charm. $$anonymous$$y www class also had super slow response. I send post request to the server and have to wait for the response which contains data that I have to use afterward. But, I don't know why yield return is slow in my case. Thank you, man!

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Chris333 · Jan 25, 2015 at 11:36 AM

Hi,

did you already read the docs about it? http://docs.unity3d.com/ScriptReference/WWW.html

There it says:

You can inspect the isDone property to see if the download has completed or yield the download object to automatically wait until it is (without blocking the rest of the game).

You can give your elements, ui's or whatever you using, a default content and after the yield returns the www response set that New content directly in the coroutine.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SneeKeeFahk · Jan 25, 2015 at 10:21 PM 0
Share

Hey, thanks for the reply. I see what you are saying however I am trying to make it a synchronous call so execution will wait for the response. I continued looking into it and I think I might have a solution but I have yet to try. If it does end up working out ill post my solution but if you have any more ideas I would be more than happy to hear them. Thanks.

avatar image SneeKeeFahk · Jan 26, 2015 at 03:15 AM 0
Share

Hey Chris,

Thanks again for the reply, WWW.isDone was what i was looking for. You Rock. I have edited my OP to include my solution.

Thanks again for the help.

avatar image
1

Answer by synchro_suban · Feb 22, 2018 at 05:17 PM

This is what i did in my game

 IEnumerator ressponceCoroutine()
 {
     WWW w = new WWW("Your Api URl");
     yield return new WaitUntil(() => w.bytesDownloaded > 0);
     TextComponent.text = w.text;
 }
 // Call in Start Method
 void Start () 
 {
     StartCoroutine(ressponceCoroutine());
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shineinfosoft17 · Jul 08, 2019 at 11:17 AM 0
Share

@synchro_suban Thanks for your help.

your solution helped me a lot. Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Getting a Code to Run at a Specific Time of Day Even if App is Off. 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges