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
0
Question by Bavr · Aug 10, 2019 at 07:06 AM · scripting problemwebrequestasync

Weird problem with Async Task and UnityWebRequest

Hi everyone Currently, I am struggling with a weird problem. I am using UnityWebRequest to retrieve data from an API and I am using async Tasks to do this. This is a basic working task which awaits the SendWebRequest() method:

 public static async Task<T> Request<T>(string url)
 {
     UnityWebRequest request = fetchPrep(url); // function which prepares request for API fetch
     await request.SendWebRequest();
     if (request.isHttpError)
     {
         throw new System.Exception("HTTP ERROR " + request.error + url);
     }

     JsonSerializerSettings setting = jsonSerializerPrep();
     string json = request.downloadHandler.text;

     Debug.Log(json);
     T fetch = JsonConvert.DeserializeObject<T>(json);
     return fetch;
 }

As I said this code works and there are no errors. Now I wanted to experiment with async tasks to improve this library: glTFast
But if I try to implement an async task with an await SendWebRequest() in the class GltfAsset.cs just like in my example, it doesn't work and says:

Assets\GLTFast\GltfAsset.cs(31,13): error CS1061: 'UnityWebRequestAsyncOperation' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'UnityWebRequestAsyncOperation' could be found (are you missing a using directive or an assembly reference?)

Could you explain why it is working in my example? I am using the same "using's" and in my example an own class in my own namespace with no extensions..
Thanks in advance!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sacredgeometry · Aug 10, 2019 at 09:01 AM

The exception tells you what the problem is.

 UnityWebRequestAsyncOperation does not contain an extension method called GetAwaiter.



You cant await the result of SendWebRequest because its not an "async" method i.e. its not using the modern async/ await pattern.

Looking it up it seems to use an event/ callback instead.


see: Unitys example code


There is an even called completed that fires when the request returns. All you need to do is remove the async/ await modifiers and add a method for handling that callback event.


Failing that you could just use the much more mature libraries in .NETs Http namespace to do your http requests. or just yield return the result

Comment
Add comment · Show 3 · 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 Bavr · Aug 21, 2019 at 11:40 AM 0
Share

I know this makes sense. But read the first part of my initial post. The await works for this first code snippet which I posted even tough it shouldn't... It's really weird and I don't have an explanation...

avatar image sacredgeometry Bavr · Aug 21, 2019 at 11:52 AM 0
Share

The await shouldn't work for line 4 as its not an await-able method call.

Edit: See the next comment for clarification

What you could do is wrap it in a task ins$$anonymous$$d.

avatar image sacredgeometry Bavr · Aug 21, 2019 at 11:58 AM 0
Share

Also: Whoops! Sorry for getting the lines mixed up I assumed it was there because that was the only await.

But my answer is relevant just swap out the line I am talking about to line 31 in the GltfAsset.cs file ins$$anonymous$$d

avatar image
0

Answer by K0ST4S · Mar 27, 2020 at 07:34 AM

Async keyword calls GetAwaiter() method by default. You must have an extension method GetAwaiter() for UnityWebRequest or other web class. It does not work in that project because that project does not have reference to your main project, where the extension method is defined.

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 SujitKadam · Jun 19, 2020 at 01:09 PM

please check this: https://gist.github.com/krzys-h/9062552e33dd7bd7fe4a6c12db109a1a

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 Develax · Jul 02, 2021 at 08:24 PM

 public static class UnityWebRequestExtension
 {
     public static TaskAwaiter<UnityWebRequest.Result> GetAwaiter(this UnityWebRequestAsyncOperation reqOp)
     {
         TaskCompletionSource<UnityWebRequest.Result> tsc = new();
         reqOp.completed += asyncOp => tsc.TrySetResult(reqOp.webRequest.result);
 
         if (reqOp.isDone)
             tsc.TrySetResult(reqOp.webRequest.result);
 
         return tsc.Task.GetAwaiter();
     }
 }

Usage:

 string text = null;
 UnityWebRequest req = UnityWebRequest.Get(url);
 UnityWebRequest.Result result = await req.SendWebRequest();
 
 if (result == UnityWebRequest.Result.Success)
     text = req.downloadHandler.text;
 else
     Debug.LogError($"{result}: {req.error}");
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

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

Related Questions

Can you run a coroutine more often than Update? 1 Answer

UnityWebRequest in ScriptedImporter 0 Answers

Help with Scene Changing 0 Answers

Unity keep crashing when loading scene from a couritne (Unity 5.x) 1 Answer

Get data from Server 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