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
1
Question by Vovoda · Jul 01, 2018 at 01:39 AM · coroutinestatichttpwebrequestmenuitem

How to make an HTTP request in a menu item ?

So what I'm trying to do is to have a menu command that will send a GET request to an API. The problem I've run into is that menu item requires the function to be static, while making an http request uses coroutines which requires the function to be non-static (from what I've understood and experienced). Here is my (non-working) code :

 public class CreateMaterialExample : MonoBehaviour
 {
     [MenuItem("GameObject/API Request")]
     static void APIRequestFromMenu()
     {
         StartCoroutine("GetRequest", "Parameter");
     }
 
     IEnumerator GetRequest(string parameter)
     {
         UnityWebRequest www = UnityWebRequest.Get("https://www.myurl.com/some/api?parameter="
             + System.Uri.EscapeUriString(parameter));
 
         yield return www.SendWebRequest();
 
         if (www.isNetworkError || www.isHttpError)
         {
             Debug.Log(www.error);
         }
         else
         {
             Debug.Log(www.downloadHandler.text);
         }
     }
 }

The problem here is that Unity doesn't want to hear about "StartCoroutine" since it is in a static method. If I remove static Unity complains about the menu item requiring the function to be static.

I would be happy to hear about a solution that doesn't use coroutines, but in a larger sense I'm curious to know whether it could be possible to use coroutine in menu item functions. I get it if the current implementation makes it impossible, but I can't see a reason why menu item couldn't use asynchronous functions.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Shameness · Jul 01, 2018 at 06:56 AM

extracted from: https://docs.unity3d.com/ScriptReference/WWW.html .

"You start a download in the background by calling WWW(url) which returns a new WWW object.

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)."

So I tested something like this and its works so far:

 public class wexample : MonoBehaviour
 {
 
     static UnityWebRequestAsyncOperation request;
     [MenuItem("GameObject/API Request")]
     static void APIRequestFromMenu()
     {
         UnityWebRequest www = UnityWebRequest.Get("https://www.myurl.com/some/api?parameter="
             + System.Uri.EscapeUriString("parameter"));
 
         request = www.SendWebRequest();
 
         while(!request.isDone){
             print(request.progress);
         }
 
     }
 
 }
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
avatar image
0

Answer by jdnichollsc · Jan 12, 2019 at 08:59 AM

You can use this Open Source plugin to use promises instead of Coroutines https://github.com/proyecto26/RestClient

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

95 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Best way to make melee AI damage player? 2 Answers

Couroutines not working in static class 1 Answer

What the best way to initialize constant static data in a MonoBehavior? 0 Answers

Instantiating a Coroutine? 2 Answers

How do I get into the data returned from a UnityWebRequest ? 2 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