Search Unity

Do we have to use WebRequest and HttpWebRequest instead of WWW and UnityWebRequest with async/await

Discussion in 'Scripting' started by mahdiii, Nov 28, 2018.

  1. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    Hi. I would like to use async/await patterns for http requests.
    Do I need to use WebRequest and HttpWebRequest .net classes?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    You're welcome to use whatever you want. Just be aware that:

    1. async/await (to my knowledge) won't work on the WebGL platform (someone correct me if this is wrong; I heard System.Threading is a non-starter on WebGL?)

    2. everything you do to Unity objects must be done in the main thread, so if you want to roll your own HTTP stuff, either a) it cannot interoperate with Unity objects, or b) you must also write a marshaling layer to get it over into the main thread, usually by means of a job queue, locking semaphore, etc.

    Good luck!
     
  3. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    I think when we use Task.Run(), it remains in the same thread (Main thread) by default.
    Yes I have already used job queue, it is OK.
    If we use ConfigureAwait(false), we should care about threads.
    Task.Run().ConfigureAwait(false)
    My question was if I can use WWW or UnityWebRequest with async/await?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,529
    Can you use it? Sure.

    Can you 'await' it? Not directly. Unity has not implemented either WWW or UnityWebRequest as an awaitable.

    If you google for it you'll find some people implementing awaiters for UnityWebRequest. Like this one:
    https://gist.github.com/krzys-h/9062552e33dd7bd7fe4a6c12db109a1a

    Though really, the awaiter pattern is pretty straight forward and you should be able to implement it yourself easily.
     
    GuruJeya14 and mahdiii like this.
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Both UnityWebRequest and WWW actually run asynchronously on other thread. When you use them from coroutine and do yield return, you kick them off on another thread and suspend the coroutine until request finishes. Once request finishes, the coroutine is continued and it executes on main thread, so it allows you to safely work with Unity APIs.
     
    Sryall812 likes this.
  6. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    In only WWW and unityWebRequest, when I use yield return, the thread switches to another thread?
    or whenever I use yield return (for example yield return StartCoroutine)?
     
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    yield return suspends the coroutine until "something". If you yield return null, it is suspended until next frame.
    If you yield return WWW or the AsyncOperation returned by UWRs SendWebReuest(), coroutine is suspended until request finishes.
    As for threading, UnityWebRequest (and WWW) always perform request on a different thread, you can simply start them and on upcomming Update() calls check their status.
     
    mahdiii likes this.