Unity async / await: Coroutine's Hot Sister [C# & Unity]

Unity async / await: Coroutine's Hot Sister [C# & Unity]

Tarodev

2 года назад

219,620 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Truth
Truth - 07.09.2023 15:52

Wow , as someone who knows and uses Javascript this is so amazing, definitely reminds me of promises!!! Thank you!! Is this in current unity 2022??

Ответить
f chong
f chong - 27.08.2023 15:46

Excellent tutorial! was wondering how do you cancel a task?

Ответить
Kevin Dario Villalvazo Manjarrez
Kevin Dario Villalvazo Manjarrez - 25.08.2023 22:49

This async thing sucks bad, you find hidden errors everywhere, I expend half a day trying to know why I could not excecute a fucking "Random.Range"! It turns out, you cannot call this and lots of other functions from outside of the main thread!
If you're planning to use async funcs, really really test before if this is going to be useful, else, run!

Ответить
RAR
RAR - 22.08.2023 18:43

i would love to see coroutines hot mom

Ответить
mikrokaulis32 K
mikrokaulis32 K - 21.08.2023 03:59

Clean video. Thanks a lot didnt know this thing even existed less go

Ответить
plankton gunslinger
plankton gunslinger - 20.08.2023 04:35

I don't get it. I didn't everything right, why is my Task doing random errors in different lines of code. I've reviewed everything, and it should work as fine, but nothing is doing what it's supposed to. The Task.WhenAll is making an error and is not even waiting. It's just throwing a null exception which is confusing

Ответить
Pedro D. Cristo
Pedro D. Cristo - 18.08.2023 16:26

So when should I use coroutines instead?

Ответить
fakhar Latif
fakhar Latif - 15.08.2023 10:51

can you link the back ground music

Ответить
Ian Crane
Ian Crane - 14.08.2023 23:55

Nice, I didn't know about GetAwaiter() - that's going to resolve some warnings in projects I worked on prior to exploring gaming. Question: Why are you serializing the fields that you do? I'm glad you mentioned WebGL; I just started my first game and didn't like the coroutine approach at all, and was actually hoping I could use async await instead (which led me here). But, I had already selected WebGL and would have strongly disliked having to re-refactor the code! Thanks for the tutorials, they're a great help!

Ответить
Skippy The magnificent
Skippy The magnificent - 12.08.2023 11:54

Cool Beenz

Ответить
Jacksendary
Jacksendary - 08.08.2023 04:08

A disadvantage af tasks is that they're slightly harder to cancel and aborting should that case arise, versus coroutines which is pretty easy to stop. ofc. it is still possible but it requires a bit more effort. I'm also pretty sure unless recently added that multithreaded work load with parallelfor could cause some issues if it is used to call unity's methods directly. It is a shame it isn't supported on all platforms tho, it is usually a powerful tool for a few specific things for sure.

Ответить
FeverDev
FeverDev - 23.07.2023 05:40

Omg, i was frustrated on why my WebGL game's asyncs were not working. I thought I was a noob (turns out I am still) but now I have realized they don;'t work in webgl so gotta stick with coroutines.

man webgl has many issues.

Ответить
JEAN MESTTI
JEAN MESTTI - 20.07.2023 22:27

For those having problem with the application still running when game is stopped, Ive created this small class that detects when the application is closed,
then use it with your "while ( ) code, like this►► While (!GetCancelToken && "add your logic here" ) { "continue your code normally }

public static class ApplicationQuit {


static CancellationTokenSource cancelToken = new CancellationTokenSource();

[RuntimeInitializeOnLoadMethod]
static void RunOnStart() {
Application.quitting += OnApplicationQuitting;
}

static void OnApplicationQuitting() {

Debug.Log("Game is about to stop or close.");
cancelToken.Cancel();
}
public static bool GetCancelToken() {
return cancelToken.IsCancellationRequested;
}
}

Ответить
JEAN MESTTI
JEAN MESTTI - 18.07.2023 15:58

I've just found pure gold

Ответить
Osama Nsr
Osama Nsr - 17.07.2023 23:04

what about UniTask ?

Ответить
Jarzykk
Jarzykk - 11.07.2023 17:46

Thank you.

Ответить
Darren
Darren - 31.05.2023 23:06

So I run into an issue using async await instead of coroutines where thing continue to happen while in the editor. For example if I use async await to start a countdown for an enemy to explode. It may explode multiple times and it may play sounds while game is not running in the editor...so, wth??

Ответить
Lemmy
Lemmy - 28.05.2023 15:11

ienumerator - hard to spell
async - easy to spell

Ответить
ali khalaj
ali khalaj - 20.05.2023 07:43

i wish you make more advanced tutorials about asyncs <3

Ответить
Roy Gatz
Roy Gatz - 11.05.2023 08:09

For newbie Unity C# community, can't be without you

Ответить
Selçuk Aksoy
Selçuk Aksoy - 10.05.2023 15:11

That was my first time I easily used a hard topic, thank you. I will tell everybody how you meme'd a really good topic(Of course usage too). I will watch every use case video you will release. Hope there will be more meme intros too.

Ответить
Name
Name - 05.05.2023 11:03

Should stay away from async to eliminate garbages.

Ответить
Vimal Naran
Vimal Naran - 25.04.2023 08:11

Nice work covering this subject and displaying the mulitple ways async/await can be used. Even a year+ on after the release of your video, its still a gold nugget!

Ответить
Joseph DiGiovanni
Joseph DiGiovanni - 16.04.2023 16:50

Your example of running functions sequentially is literally the same as coroutines, making the BeginTest function async is the same as calling a base coroutine that calls other coroutines. I see the benefit of async but you spent a bit of time explaining how messy it is with coroutines only to write identical code with async

Ответить
Anthony Brown
Anthony Brown - 08.04.2023 04:13

Heyo great video! I'm creating a game with a chunking system similar to minecraft (except the map is static width&height) so no infinite loading. I've been using coroutines to Load/Save chunks but I was noticing like a 0.005ms delay (so a loss of mouse control during that time), I've added the async way instead and it seems to be gone, is that the right idea? The chunks are now loading while moving the camera I think but its so hard to tell as its so quick anyway.... :D

Ответить
Franco Cougo
Franco Cougo - 07.04.2023 02:39

When I first learned about Coroutines it was ground breaking, the peak of technology for my silly indie brain.
Now I'm finding out about async/await/tasks and it's just wonderful. Truly amazing to see how there's always something new to learn.

Ответить
SPi
SPi - 04.04.2023 23:57

And then you have COD scripting which has simple "wait" and "endon" functions. As simple as it gets.

Ответить
Dave
Dave - 29.03.2023 23:19

Great stuff. I have a question. Do you have in plans maybe another video about async await? Like for example video about cancellation tokens or the difference between C# Taks and UniTaks?

Ответить
Moho Khachai
Moho Khachai - 29.03.2023 16:05

🎉

Ответить
Stamatios Psarras
Stamatios Psarras - 28.03.2023 19:28

not supporting WebGL is a really big disadvantage...Any reason for it?

Ответить
47Mortuus
47Mortuus - 28.03.2023 12:19

No, you monkey, you use the appropriate tool for the job. The job... SYSTEM is Unitys' way of multithreading.
1. Unity already uses the job system internally and reserves threads equal to the amount of logical cores available on the system. If you use another multithreading library (async/await is a library built into the language) you will run into decreased performance due to even more frequent context switches. If the goal of multithreading is performance, using async/await instead of the job system contradicts the original goal of using it in the first place
2. The job system is thread safe, async/await is not. If your goal with this video is to teach some newbie who just wants to build the next WoW killer about "the easy way to do multithreading", then it's both the wrong audience picked and the wrong tool taught, big time.

But I didn't expect much expertise from someone who uses 'var' or the following code "style":
class X { // < this was proven to be less readable
int thisSucks;
// what is this empty line doing here
}


All in all, 2/5

Ответить
John
John - 25.03.2023 09:55

I had this video recommended to me some time ago but I couldn't find it. So that I searched for coroutine hot sister...then realized the wording's bit of a mess xD

Ответить
Proto Bard
Proto Bard - 23.03.2023 01:36

Is anybody else having problems with async/await and Android? My builds silently fail now after change a couple coroutines to asyncs, No problems in the editor or non-mobile versions

Ответить
ubuntu user
ubuntu user - 19.03.2023 10:39

thanks a lot !

Ответить
Ultramelon
Ultramelon - 10.03.2023 03:03

I will definitely have to keep this one in mind! 👍

Ответить
dibaterman
dibaterman - 26.02.2023 08:53

The problem I am getting with this that Coroutines do not have is the when making a Task for an anonymous reference that should only exist once.

For the Coroutine I just needed to make a dictionary, made the key something that identified the reference and ran the coroutine associated with that key after checking if that key was already active.

I can't do that with Tasks, I tried using Func<Task> but that's a not go as well.

Ответить
NameousIncognitus
NameousIncognitus - 18.02.2023 19:25

I think it's worth mentioning that async/await continues to work in the editor even after the game has stopped, unless you set an exit condition that takes this into account. Can lead to unexpected results, especially if moving, adding or removing scene objects, playing sound, etc. occurs inside asynchronous functions.

Ответить
Bagus Tri Widiyanto
Bagus Tri Widiyanto - 13.02.2023 10:22

I have a question, what is the downside of using Async compare to Coroutine ? is it taking a lot of resource ?

Ответить
Nicholas Smith
Nicholas Smith - 10.01.2023 15:21

Found this video awhile ago, but I keep coming back because I forget the snytax or where in my code to implement the async or await functions.
Thank you so much for this video as it really helped me on my projects and will continue to help me on future Unity projects.

Ответить
J S
J S - 09.01.2023 23:45

Thanks for the tutorial. Question, when is it a bad idea to await an async method? If I do it on Update it will spawn a call every frame, so seems like it makes more sense to make use of it on functions that get called less frequently. When can it seriously affect performance?

Ответить
tabooization123
tabooization123 - 19.12.2022 13:06

Nice detailed and simplified tutorial

Ответить
ckwallace
ckwallace - 02.12.2022 09:33

coming from a javascript background and being familiar with Promises (and having trouble wrapping my mind around coroutines) this video is a lifesaver! thank you so much

Ответить
Mayank K (MiKi)
Mayank K (MiKi) - 30.11.2022 09:39

Can you please make advance tutorial for async/await methods? Also Need more examples too!! Thanks for tutorials! It helped a lot.

Ответить
shami
shami - 23.11.2022 14:09

I know you have a background soundtrack. It kind of disturbed me when I heard it and thought where it came from? I don't like background noise.

Ответить
Martin Rossel
Martin Rossel - 20.11.2022 15:29

Make me question why I learned coroutine at all ????

Ответить
Vittorio Chiuselli
Vittorio Chiuselli - 18.11.2022 17:32

Hello, I'm trying to use async/Task to ask the player to click an object, retrieve the GameObject and work with it. There is a video where I can learn about it?

Ответить
José M Lucero
José M Lucero - 15.11.2022 07:44

I found this very usefull, and i can say that it really worths, not only for the time you spent making it, but because we can realize the quality of this production when you read the meticulous observations that other also experienced people make in the comments.

Ответить