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
0
Question by techshaman · Jul 28, 2012 at 03:55 PM · coroutinewwwthreadrest

WWW, RESTful Service, and Threading

I have a basic unity scene that should instantiate game objects based on the result of a simple GET of a RESTful web service. I have a very simple example of calling this web service here from unity:

 using UnityEngine;
 using System.Collections;
 
 public class Updater : MonoBehaviour {
 
     void Start () {
         string url = "http://MyRestfulServiceUrl/Service1.svc/someGetMethod?id=1";
         WWW www = new WWW(url);
         StartCoroutine(WaitForRequest(www));
     }
 
     IEnumerator WaitForRequest(WWW www)
     {
         yield return www;
 
         // check for errors
         if (www.error == null)
         {
             Debug.Log("WWW Ok!: " + www.data);
         } else {
             Debug.Log("WWW Error: "+ www.error);
         }    
     }
 
 }



The result of the web service may take a second or 2 to respond. Additionally the response data may be a blob of XML that needs to be parsed - However I do not want these operations to effect the main UI thread. Is using threads to create a background 'call web service and parse response' thread an appropriate solution? Are there better/alternate solution to consider?

PS I am not too familiar with coroutines but my understand is that they are not really asynchronous background threads?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Jul 28, 2012 at 04:22 PM

Coroutines are not threads, they are just routines that execute partially on the main thread and then continue from where they left off later.

You can't use the Unity API from a second thread, but you can certainly process data. I use threads to run ZXing and detect qr codes which works quite well.

Comment
Add comment · Show 4 · 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 whydoidoit · Jul 28, 2012 at 04:23 PM 0
Share

You might use a coroutine to wait for the WWW then run a thread to process the returned data.

avatar image techshaman · Jul 28, 2012 at 04:35 PM 0
Share

So it sounds like I should be able to do the processing of X$$anonymous$$L in a separate thread. The result of the X$$anonymous$$L will update some object values in the scene and then the main unity thread should be able to respond accordingly.

avatar image whydoidoit · Jul 28, 2012 at 04:55 PM 0
Share

Yeah, you'll just need to send the data back to the main thread.

avatar image whydoidoit · Jul 28, 2012 at 05:03 PM 0
Share

On. this post you can see how I use my Loom class to handle this stuff, the class itself is included in the download.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WWW class implemented with threads? 1 Answer

i think www blocks display thread 1 Answer

Using a method to get the string from a download. 3 Answers

How do I use RESTful apis from Unity? 0 Answers

How to imitate yield functionality like WWW class 0 Answers


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