Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
16
Question by pavees · Feb 03, 2010 at 11:43 AM · coroutineserverweboafa

How can I send and receive data to and from a URL, i.e. server side scripts, web services, etc?

How can I send data (eg, a string or integer value) to a server-side script or a web service through C# or Javascript in unity Web player mode.

Can you please give me some details on how to call web services. Me being new to Unity Web application, I dont know even how to call a URL and a parameter with it. So could anyone give a proper way to call Web services.

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

3 Replies

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

Answer by duck · Feb 03, 2010 at 05:24 PM

The normal way of doing this would be to use Get or Post requests via the WWW class or the WWWForm class. If you're making a Get request with parameters added to the end of the url, you'd use code similar to this:

'GET' request in Javascript:

 var url = "http://example.com/script.php?var1=value2&var2=value2";
 var www : WWW = new WWW (url);
 
 // wait for request to complete
 yield www;
 
 // and check for errors
 if (www.error == null)
 {
     // request completed!
 } else {
     // something wrong!
     Debug.Log("WWW Error: "+ www.error);
 }

'GET' request In C#
In C# this is a little more complex because of the way that you have to write out coroutines in full. In this example, the initiation of the request is done in "Start". We then hand the WWW object to a coroutine called "WaitForRequest" which waits for the www request to complete.

 using UnityEngine;
 
 public class GetURL : MonoBehaviour {
 
     void Start () {
         string url = "http://example.com/script.php?var1=value2&var2=value2";
         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);
         }    
     }
 }

And to make a Post request with the same parameters and values, it would be:

'POST' request in Javascript:

 var url = "http://example.com/script.php";
 var form = new WWWForm();
 form.AddField( "var1", "value1" );
 form.AddField( "var2", "value2");
 
 var www = new WWW( url, form );
 
 // wait for request to complete
 yield www;
 
 // and check for errors
 if (www.error == null)
 {
     // request completed!
 } else {
     // something wrong!
     Debug.Log("WWW Error: "+ www.error);
 }

'POST' request in C#
Again, we use exactly the same Coroutine and WWWCompleted function as in the 'get' example. The only difference is that we build the WWWForm object, and use that to create the WWW request before handing it to the coroutine.

 using UnityEngine;
 
 public class PostURL : MonoBehaviour {
 
     void Start () {
 
         string url = "http://example.com/script.php";
 
         WWWForm form = new WWWForm();
         form.AddField("var1", "value1");
         form.AddField("var2", "value2");
         WWW www = new WWW(url, form);
 
         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);
         }    
     }    
 }
Comment
Add comment · Show 12 · 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 dingben · Aug 16, 2010 at 04:02 AM 2
Share

Does the WWW GET/POST approach... pass data back and forth from database to Unity Player without refreshing the page the player is running on? A refresh would be undesired as it would break the user's experience and the game would also restart. Or is this approach only valid for a shot at the database on startup and quitting of the Unity App? . the http://URL... would point to a pageless/codebehind file? never done one of those, not sure how I would do that if it is the way to implement it.

avatar image duck ♦♦ · Aug 31, 2010 at 01:05 PM 1
Share

No, it does not cause a page refresh. The requests go via the browser, but occur "behind the scenes".

avatar image codeZero · Jun 16, 2011 at 04:17 AM 0
Share

i try this code. it work on unity simulator 3.0 and 3.3(trail version). But for unity 3.3 after i build it it didn't work on my android device but 3.0 after i build it work on my android device. For your information im using nexus S and nexus one. is it the bug from Unity?

avatar image techshaman · Jul 27, 2012 at 02:15 PM 0
Share

Does anyone know if this works for iOS/Android builds? I am trying to consume a web service in my Unity3d scene and I am targeting mobile platforms. I'd rather not invest in the liscense yet if there is no way to do this!

avatar image Bunny83 · Jul 27, 2012 at 03:04 PM 0
Share

@tyinus: Sure, it works on all platforms. Only webbuilds have a crossdomain restriction which can be removed by a crossdomain policy.

Show more comments
avatar image
3

Answer by marinl · Apr 09, 2013 at 08:31 PM

I write a tutorial about http using in unity http://unity-tutorials.blogspot.com. Listing 12 - 15. I am using subroutines for http calls to not stop UI rendering.

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 BHS · Dec 19, 2013 at 02:26 AM 0
Share

The JSON script will not compile in Unity 4.2.

avatar image li3ro · Nov 27, 2015 at 08:05 AM 1
Share

any equivalent c# tutorial?

avatar image
0

Answer by gaminggal39 · Nov 12, 2018 at 07:51 AM

Watch this video to solve :https://www.youtube.com/watch?v=FChVgb6EXGk&t=133s

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

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

21 People are following this question.

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

Related Questions

How to access StartCoroutine in a static way 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can i receive data from URL like cookies? 0 Answers

Insert the model from the server system 0 Answers

Trouble with Iterative Coroutine 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